forked from yihui/knitr-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
047-tikz-png.Rnw
40 lines (32 loc) · 1.27 KB
/
047-tikz-png.Rnw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\documentclass{article}
% you do not really need this, since png has higher priority to pdf by default
\DeclareGraphicsExtensions{.png,.pdf,.jpeg,.jpg}
\begin{document}
This is an example showing you how to convert PDF figures generated by tikz to PNG via ImageMagick:
<<setup, include=FALSE>>=
library(knitr)
opts_chunk$set(cache=TRUE)
options(width = 60)
knit_hooks$set(tikz2png = function(before, options, envir) {
# use this hook only for dev='tikz' and externalized tikz graphics
if (before || options$dev != 'tikz' || !options$external || options$fig.num == 0) return()
figs = knitr:::all_figs(options, ext = 'pdf') # all figure names
# note the tikz2png option is the extra parameters passed to 'convert'
for (fig in figs) {
system(sprintf('convert %s %s %s', options$tikz2png, fig, sub('\\.pdf$', '.png', fig)))
}
})
@
The plot in this chunk is converted to PNG:
<<test-a, dev='tikz', fig.width=4.4, fig.height=3.3, tikz2png='-density 300'>>=
(x=rnorm(20))
par(mar = c(4.5, 4, .1, .1))
hist(x, main='', xlab='$x$ (how the fonts look like here?)',
ylab='$\\hat{f}(x) = \\frac{1}{nh}\\sum_{i=1}^n \\cdots$')
@
This chunk uses the PDF device, and it is not converted:
<<test-b, dev='pdf', fig.height=3>>=
par(mar = c(4.5, 4, .1, .1))
plot(x, pch = 19)
@
\end{document}