Debian Codegraph @

demo/pretty-lab.R

raw
1 ## Original from David Seifert (ETHZ Basel, c/o Beerenwinkel)
2 ## https://github.com/mmaechler/sfsmisc/pull/2
3
4 ### Example showing how eaxis() / pretty10exp() lab.type = "latex"
5 ### can be used together with LaTeX package "tikz" and
6 stopifnot(require("tikzDevice"))
7 ### to produce LaTeX math labels
8 require("sfsmisc")
9
10 x <- (-3:10) * 10^10
11 y <- abs(x / 1e9)
12
13 (t.file <- tempfile("tikz-eaxis", fileext = ".tex"))
14 tikz(file = t.file, standAlone=TRUE)
15 plot(x, y, axes=FALSE, type = "b")
16 eaxis(1, at=x, lab.type="latex")
17 eaxis(2, lab.type="latex")
18 dev.off()# i.e. finish and close file 't.file'
19
20 ## Now add two lines to (the preamble of the latex file
21 ## such that all axis tick labels are in latex math if requested by lab.type="latex".
22 ## {Note : "\" (backslash) must be doubled in R strings}
23 helvet.lns <- c("\\renewcommand{\\familydefault}{\\sfdefault}",
24 "\\usepackage{helvet}")
25 str(ll <- readLines(t.file))
26 writeLines(c(ll[1:4], "", "%% Added from R (pkg 'sfsmisc', demo 'pretty-lab'):",
27 helvet.lns, "", ll[-(1:5)]), t.file)
28
29 ## Produce PDF from LaTeX
30 system(paste(paste0("pdflatex -output-directory=", dirname(t.file)),
31 t.file))
32
33 ## and view it
34 if(file.exists(p.file <- sub("tex$", "pdf", t.file)) && interactive())
35 system(paste(getOption("pdfviewer"), p.file), wait=FALSE)