Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
It’s true. I like to do my work in R and write using LaTeX (well, I prefer to use org-mode for less formal writing and/or if I don’t have to typeset a lot of math). I haven’t done a lot of LaTeX’ing or Sweaving in the last year since 1) I’ve been collaborating with scientists (stuck using Word) and 2) my simulations in R have been a little overwhelming to keep in one file a la literate programming. I have a feeling I’ll be going back to LaTeX soon since I have to write up my dissertation (and lectures if I end up at an academic institution, **crosses finger**).
Subconsciously I’ve always wanted a tighter integration between R and LaTeX. Sweave did a fantastic job bringing R to LaTeX, greatly improving my workflow and jogging my memory when I revisit projects (just look at the one file consisting of documentation/writing and code). Despite R’s outstanding capabilities in creating publishable plots, I always felt it needed work in the realm of typesetting math. Sure it supported mathematical expressions. I used them a few times, but whenever I included the generated plot in a LaTeX document, the figure appeared out of place. I’ve explored Paul Murrell’s solution by embedding Computer Modern Font into the R-generated plot; UPDATE 10/23/2010 I also explored the psfrag in this post. The required effort probably outweighs the cost in most situation in my opinion (I haven’t done it in a real life scenario). I also tried to create a simplistic plot and overlay LaTeX code afterwards; again, haven’t done much with this, although I expect this will come in useful when I have to write over a pdf file that I do not have access to the source code.
I’ve also explored how to draw in LaTeX using the Picture package and XY package. I didn’t do much with it after the exploration because I didn’t know the syntax well and because drawing in R, Google Docs, or OpenOffice suffices 99.9% of the time. I prefer to draw in R or LaTeX to have reproducible code.
I was recently introduced to tikzDevice by this post via the R blogosphere. What it does is translate an R plot to TikZ. That is, instead of creating the plot device via pdf()
, you do it with tikz()
. This creates a .tex
file via three modes (well four but I don’t think I’ll use the barebones mode):
- Just tikz code so you can use the
include{}
command in LaTeX (default). - Tikz code surrounded by the document skeleton so the
tex
file can be compiled (standAlone=TRUE
). - Console output mode where the code are sent to
stdout
for use with Sweave (console=TRUE
). UPDATE 10/23/2010: use this with pgfSweave; builds on cacheSweave and Sweave.
Read the vignette; it’s fairly complete. The authors claim that the software is still in Beta stage (they’re still testing certain interface features), but my initial testing shows that it is ready for prime time, at least for my usage.
If you want the results in jpeg/png for use with the internet or Word documents, you can always convert the pdf to another format via the convert command.
Here is my example for the standalone (2) case:
## look at vignette for examples and how to's library(tikzDevice) f1 <- "tikzDevice_Ex1.tex" tikz(file=, standAlone=TRUE) set.seed(100) n <- 100 x <- rnorm(100) y <- 2*x + rnorm(n) fit <- lm(y ~ x) plot(x, y, xlab="x", ylab="y", main="$E[Y] = \beta_0 + \beta_1 \times x$") dev.off() system(paste("rubber --pdf", f1)) system("convert tikzDevice_Ex1.pdf tikzDevice_Ex1.png") system("gnome-open tikzDevice_Ex1.png")
Note I make use of the rubber command. Feel free to replace it with pdflatex
.
UPDATE 10/23/2010: Make use of pgfSweave with this!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.