Howto Create Great Presentations using Beamer, Sweave and LaTeX
To achieve this goal, we need use the concept of literate programming, yet another one of Donald Knuth’s contributions to the world. LP “represents a move away from writing programs in the manner and order imposed by the computer, and instead enables programmers to develop programs in the order demanded by the logic and flow of their thoughts”.
So in the case of a technical presentation, the idea is that we:
- write our talk in a loose conversational style, just as we hope to deliver it
- whenever we think we need to illustrate a point, we create the necessary slide
- wherever we need a figure for the slide, we insert code to perform the analysis and create the figure
Sounds like voodoo? Nope it’s straight-forward thanks to LaTeX, beamer and R/Sweave. Here’s the basic workflow (after you’ve installed all the software).
- Create a source file with a .Rnw extension. This should contain only the preamble and body of a LaTeX document. Here’s a minimal working example to download: myfile.
- Write your talk in the body of your Rnw file. Wherever you want to insert a slide, use the
\begin{frame}{Title}{Sub title} \end{frame}environment as described by the beamer documentation. - Wherever you want to perform some R analysis, add an R code block. While Sweave lets you format these blocks in multiple ways, here are two useful templates.A basic code block to perform calculations. The code will not show up in the final document.
[cc]
<>=
x <- 1:100
y <- x + rnorm(100,0,1)
@
[/cc]
A basic code block to show a plot, again without displaying the underlying code.
[cc]
<>=
plot(x,y)
@
[/cc] - Compile your Rnw file using R. Assuming you’ve started R from the same directory as your Rnw file, the syntax is: [cci]Sweave(”myfile.Rnw”)[/cci]
You can change the working directory with [cci]setwd(”path”)[/cci].This will create all the necessary figures and generate a tex file called myfile.tex. You can also do it using GUI.

- Process one of the two following examples with LaTeX to generate either your slides or the lecture notes (e.g. [cci]pdflatex slides[/cci]).Slides ([cci]slides.tex[/cci])
- [cc lang="latex"]
\documentclass[ignorenonframetext]{beamer}
\input{myfile}
[/cc]
Notes ([cci]article.tex[/cci])
[cc lang="latex"]
\documentclass{article}
\usepackage{beamerarticle}
\input{myfile}
[/cc]
The resulting PDF is fully portable as all you need is a reader of some sort. Load it up, press [cci]CTRL+L[/cci] to get full screen mode and away you go!
Download Files for this tutorial, [Credit to James]
Tips:
The sweave.sty file can be found in the C:\your R installation folder\share\texmf\

