LaTeX and Sweave

December 1, 2009

(This article was first published on Jason Bryer » R, and kindly contributed to R-bloggers)

LaTeX is a typesetting language that is known for its beautiful mathematical formulas. It is similar to HTML in that you write your document in plain text and process (or compile) it to create a postscript or PDF file. You will need to download a LaTeX processor for your platform. On Windows, TeXnicCenter ( is very good and on Mac TeXShop ( is very good. Both these applications provide a text editor in addition to the LaTeX processors. Here are a list of excellent resources for learning LaTeX (note that many of these are in psarelated/LaTeX folder on Dropbox):

Sweave allows for the embedding of R code directly in your LaTeX documents. The best resource is the Sweave User Manual ( In short, simply place the following in your LaTeX file:

2 + 2

There is a Sweave function in R. Issuing the Sweave(file.choose()) command will first prompt for the file (by convention use Rnw file extension), execute all the R code and generate a tex file. This file can then be run through your typical LaTeX processor or editor. Once you go through the Sweave manual, I recommend printing page 13 which lists all the options that can be issued to Sweave.

To create presentations, the Beamer class is what is generally used. The LaTeX beamer class homepage has several exmaples ( The Beamer manual is included on Dropbox or when you download the classes (I do believe Beamer is built into both TeXnicCenter and TeXShop).

Laslty, I have included a few of my own files on Dropbox for a recent paper and presentation I’ve done. I used Sweave for the paper (see Bryer.LocatingStudents.Paper.Rnw). I also included the generated LaTeX file (Bryer.LocatingStudents.Paper.tex) as well as the final PDF version. For the presenation, I used Beamer but since I was working from the paper, I simply cut and paste sections from the generated tex file to Bryer.LocationStudents.Presentation.tex. Note the first two lines of this file:


The percent character is a comment line in LaTeX so in order to create the handout version I simply commented out the second line instead of the first. Both versions are included on Dropbox for your review.

To leave a comment for the author, please follow the link and comment on their blog: Jason Bryer » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)