Literate programming for talks: Beamer, Sweave and LaTeX

August 25th, 2009 by james

The summer conference season is slowly drawing to a close and we can all put our feet up, right? After all, the papers are done and presented and it’s a least a couple of months before organizers want your abstracts for next year. But before you kick back and relax, it’s worth pausing for a second to reflect on how things have gone and what you might want to do differently next year.

I gave a couple of talks this summer and while they went well, I wasn’t happy with the behind-the-scenes workflow. Some problems:

  • Creating figures in one piece of software, exporting to the right image format, inserting them into PowerPoint, discovering that you made a mistake, re-doing the figures…
  • Trying to shoe-horn a nice story about the results into 10 tight slides
  • PowerPoint, PowerPoint, PowerPoint – having to use someone else’s PowerPoint template, ugly text and math, big files, etc.

A lot of these issues can be fixed using tips here on the site. Outlining your talks for example helps get away from the staccato style of PowerPoint and as commenters have pointed out here, there are lots of ways to mix slides and narrative in one source file. But I want to go step further and show how you can combine narrative, slides and content (i.e. figure creation) in one file.

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).

  1. 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.Rnw.
  2. 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.
  3. 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.

    <>=
    x <- 1:100
    y <- x + rnorm(100,0,1)
    @

    A basic code block to show a plot, again without displaying the underlying code.

    <>=
    plot(x,y)
    @

  4. Compile your Rnw file using R. Assuming you’ve started R from the same directory as your Rnw file, the syntax is:

    Sweave("myfile.Rnw")

    You can change the working directory with setwd("path").

    This will create all the necessary figures and generate a tex file called myfile.tex.

  5. Process one of the two following examples with LaTeX to generate either your slides or the lecture notes (e.g. pdflatex slides).

    Slides (slides.tex)

    \documentclass[ignorenonframetext]{beamer}
    \input{myfile}

    Notes (article.tex)

    \documentclass{article}
    \usepackage{beamerarticle}
    \input{myfile}

That’s it. I’ve used this process to prepare my presentation for an upcoming event and it’s much easier than the old PowerPoint/R shuffle. The resulting PDF is fully portable as all you need is a reader of some sort. Load it up, press CTRL+L to get full screen mode and away you go!


As a private emailer pointed out, there are a few other beamer tricks that might be worth mentioning:

  • With a bit of practice, you can style slides however you want. But there are also some presets: e.g. in the slides.tex file above, insert the package declaration \usepackage{beamerthemesplit} to get a blue format with a title of contents/breadcrumb trail.
  • The fragile option and \footnotesize commands can be useful for formatting verbatim text output. For example:

    \begin{frame}[fragile]{Results of linear regression}
    \footnotesize
    <>=
    summary(lm)
    @
    \end{frame}

Thanks to Patrick from Berlin for those tips.

If you enjoyed this post, make sure you !


One Response to “Literate programming for talks: Beamer, Sweave and LaTeX”

  1. Mario Pineda-KrchNo Gravatar Says:

    R + Sweave + Beamer is the ultimate threesome! For this summer’s meeting season I embraced R + Beamer (see for example here). Now I am writing all my code/documentation in Sweave, and the next (logical) step is to mesh the tree together.

    After this enlightening summer I would not touch PowerPoint with a ten foot pole.

Leave a Reply