2009-10-18

writing presentations with org-mode and beamer

[Updated:fixed an error in the template] Things have been a bit quiet at Emacs-Fu lately; this is mostly because I have been very busy with work, traveling, traveling for work etc. Emacs-wise, this has been a very intensive time, and I have been able to move more and more task to emacs – both professionally and privately. This is mostly because thing become easier when I can do all my things in one place.

Anyhow, one of the tricks I picked up recently is to write presentations with the combination of org-mode and a LaTeX-package called beamer. The most common tool for doing presentations is Microsoft's Powerpoint program. It gets a lot of criticism, most famously from prof. Tufte in his Powerpoint is Evil essay. Of course, the problem is in misuse of the tool, not so much the tool itself.

Still, I didn't want to use Powerpoint (or Powerpoint-wannabees, like OpenOffice's Impress). For the technically-inclined, the knee-jerk reaction to this kind of problem is to shout 'LaTeX!'. Indeed - the LaTeX text-processing system offers a package called Beamer, which allows you to write presentations with LaTeX. It is quite powerful, and even allows for all kinds of fancy graphical bling (fade-in, fade out etc.); even better, it generates PDF-files, which can be viewed just about anywhere. The various themes and color settings it offers are quite nice, even though they tend to fill only a small corner of the design-universe…

beamer and org-mode

Now, while I am no stranger to LaTeX, especially for writing a quick presentation, it can be a painful to remember the various directives and options. I am not really a daily LaTeX-user, so I tend to forget these things. I am a daily org-mode user though, and org-mode can export to LaTeX (which then, in turn, are translated into PDFs). So why not use org-mode to generate presentations?

It turns out that that is quite easy.

First, we need to define some of the LaTeX-boilerplate, and tell org-mode about it, so we never need to think about it again. Put the following in your .emacs:

;; allow for export=>beamer by placing

;; #+LaTeX_CLASS: beamer in org files
(unless (boundp 'org-export-latex-classes)
  (setq org-export-latex-classes nil))
(add-to-list 'org-export-latex-classes
  ;; beamer class, for presentations
  '("beamer"
     "\\documentclass[11pt]{beamer}\n
      \\mode<{{{beamermode}}}>\n
      \\usetheme{{{{beamertheme}}}}\n
      \\usecolortheme{{{{beamercolortheme}}}}\n
      \\beamertemplateballitem\n
      \\setbeameroption{show notes}
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{hyperref}\n
      \\usepackage{color}
      \\usepackage{listings}
      \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
  frame=single,
  basicstyle=\\small,
  showspaces=false,showstringspaces=false,
  showtabs=false,
  keywordstyle=\\color{blue}\\bfseries,
  commentstyle=\\color{red},
  }\n
      \\usepackage{verbatim}\n
      \\institute{{{{beamerinstitute}}}}\n          
       \\subject{{{{beamersubject}}}}\n"

     ("\\section{%s}" . "\\section*{%s}")
     
     ("\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}"
       "\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}")))

  ;; letter class, for formal letters

  (add-to-list 'org-export-latex-classes

  '("letter"
     "\\documentclass[11pt]{letter}\n
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{color}"
     
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

This is based on the template by Thomas S. Dye on the org-mode mailing list. You can of course add other packages to it with \usepackage. In my version, I have add the Listings-package for including syntax-highlighted snippets of source code in my presentations. Importantly, I added the [fragile] options to the frame-settings, otherwise you cannot include such source code fragments without LaTeX complaining in various unhelpful ways.

Note, you can customize the way the Listings package works by changing the template above; or by setting the options in the org-file; this involves some 'raw LaTeX' though. It might make sense to define some macros for that.

Now, we can easily make a presentation in org-mode; simply start the file with something like:

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes
#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

Of course, you can change these parameters; for example, you might want to change the BEAMERTHEME into Madrid or Warsaw, or … – see the Beamer User Guide (PDF).

After having set up these things, you can write presentations in the familiar org-mode mark-up.

including source code

As mentioned before, you can use the listings-package to include source code snippets in your presentation. You'd write this something like:

#+BEGIN_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}

#+END_LaTeX

In other words, we include some 'raw' LaTeX to do this. Now, the org-mode-way of doing this, would be to use something like

#+BEGIN_SRC c
  /* code */
#+END_SRC

as discussed before. This works well when exporting to HTML, but at this moment this will simply translate into a verbatim environment in LaTeX - so we use lstlisting to get some syntax-highlighting.

including pictures

Of course, the full arsenal of org-mode tools is available as well, for example Ditaa, as discussed before. Ditaa is now shipped as part of org-mode, and you can use it to create picture which are then included in your presentation. Very nice.

For including existing images (PNGs, JPGs etc.), it's probably easiest to put use some raw LaTeX for that, e.g., something like

#+LaTeX:\includegraphics{/some/path/emacs.png}

putting it together

Now, let's put it all together. Below is an example presentation. Assuming you have everything installed (ie., LaTeX with the listings package, a fairly recent org-mode, ditaa), you create your presentation.org and then press C-c C-e d and your presentation (presentation.pdf) is generated and automatically shown in your PDF-viewer. Easy!

The intermediate files (such as presentation.tex) are there as well, so you can check them if something went wrong.

I have uploaded the resulting PDF to Slideshare, so you can see what it looks like. However, the Slideshare-converted version is extremely blurry, unlike the crisp PDF I actually created. I'd be happy to upload the file somewhere else if someone can point me to a good place, thanks!

So hopefully this shows that you can quite easily make presentations with org-mode, with some help from Beamer, LaTeX etc. Beamer actually provides a lot more, such as funky slide-transitions and other tricks – but the things here should give you a good starting point.

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes

#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

* My presentation

** Overview

   1. Introduction

   2. Main part

   3. Questions
    
    
** Some interesting stuff
    
*** an important point
    
    - subpoint a
      
    - subpoint b

** Graphics

*** a picture

#+begin_ditaa blue.png -r -S
+---------+
| cBLU    |
|    +----+
|    |cPNK|
+----+----+
#+end_ditaa

*** another picture
#+LaTeX:\includegraphics{emacs.png}

** More interesting stuff

*** some C++ code
#+begin_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}
#+end_LaTeX

*** and some Python...

#+begin_LaTeX
\begin{lstlisting}[language=python]
for i in range(1,10):
        print i, "hello, world!"
\end{lstlisting}
#+end_LaTeX

34 comments:

vinhdizzo said...

what if u have a long list (***)...does this method automatically split the list into multiple slides and append /cont'd/ to the slide title?

djcb said...

vinhdizzo: it doesn't -- in fact, it cuts items off after the 16th or (but of course you could make the font smaller).

It seems not a big practical problem though - bullet lists of more than 10 items are not a very good idea anyway.

vinhdizzo said...

djcb: i see. yes long list aren't too practical, but the great thing about latex and org-mode is you don't have to worry about formatting and layout, yet with beamer, u have to gauge the number of bullets on a slide. it'd be nice if i don't have to worry about this and have it decide how many bullets go on a slide and continue to the next if there isn't any more room.

Anonymous said...

Nice- I've been working all day on a presentation with beamer and org-mode, and now I find this. Even though I'm a heavy latex user, I must say that this way of doing things is *much* more efficient.

Regarding long slides: maybe you can try the allowframebreaks option, which will give things like:
\begin{frame}[allowframebreaks]{the title}
I don't know if there are any real disadvantages to setting this for all frames- I've only used this option for frames containing a list of references.

One more thing that I do differently is to define frame titles to be level 3 headings in org export, not level 2; this works well for large presentations in which having more structure is important. The downside is that I need to always have level 2 headings, which is awkward for short sections.

djcb said...

@vinhdizzo: fair enough; beamer is a bit 'special' in this regard. You might want to try Prosper instead -- you'd have to change the template a bit for that, but it should be straightforward (I suppose...)

@Anonymous: hope it's useful. About allowframebreaks -- I had to remove them or for some reason there as a vertical bar next to the level-2 headings for some reason. I couldn't quite figure out why; it might something with my setup.

J. Aaron Farr said...

There's a Google Group dedicated to various slideshow creation tools. Might want to share this with them:

http://groups.google.com/group/webslideshow

vinhdizzo said...

@djcb i've tried prosper before i did beamer. beamer way superior and more documented and supported...goes directly to pdf.

Qi Qi said...

ConText is another alternative. It can do presentation pretty well.

Not sure whether it can work with Org-mode.

Anonymous said...

I followed this post, and found 2 things I needed to work around:
1. the default org package does not have ditaa, but the newest org version (6.31a) has this tool and integrated pretty well
2. For the 3rd slice (Overview):
** Overview

1. Introduction

2. Main part

3. Questions

djcb's pdf has
(1)
(2)
(3)
, but my slice turn out to be
(1)
(1)
(1)

Have any idea what I should do on this problem? Thanks!

a said...

Nice post, will definitely give it a shot next time I have to write a presentation (hopefully a long time from now).

djcb said...

@zhangda: regarding the numbering: the numbers are already in the
org-buffer. org-mode does the auto-numbering for you when you type something
like
1. Foo
and the press M-Enter.

Anonymous said...

djcb:
Thanks for the reply. My situation is, I noticed in your pdf online the (1) (2) (3) are itemized by icons showing correct number; in my case, the are also itemized by icons, but all the 3 icons are (1) (1) (1). I tried
** Overview
*** Introduction
*** Main part
*** Questions
then it works. But I looked into your code, your code is

** Overview
1. Introduction
2. Main part
3. Questions

Any suggestions?

Qi Qi said...

It seems to interference with C-c C-e d.
It said article class was not defined in org-export-latex-classes.

Qi Qi said...

An alternative way is to define a hook function and hook it to latex mode.

Mark Scala said...

very nice post, thank you. i've been looking for this for a while now. But how can one include overlays? I usually design my slides to be revealed one line at a time. Is this possible?

djcb said...

@Mark Scala: definitely that is possible; Beamer has support for that -- you
will need to go down to the Beamer/LaTeX level though
(using #+begin/end_LaTeX/#+begin/end_LaTeX). It be nice if it were a bit
easier using 'normal' org-mode though...

Anonymous said...

Is it me or the macros aren't being transfered from the .org file into the .tex file?

The BEAMERINSTITUTION macro for instance, when changed, doesn't transfer over to the .tex file.

Unknown said...

@anonymous: No it's not only you, the #+MACRO settings are ignored.

I'm running emacs 23.1 with org-version 6.21b

The only thing that helped was changing the .el file.

Unknown said...

hmm the #+MACRO: settings are taken from a different post in the org-mode mailinglist, but I couldn't make these setting work yet.

djcb said...

@Anonymous, @LanX: I fixed that now. The problem here was that because I use org-mode for writing these blog-posts as well, org-mode 'helpfully' applied to MACROs to my very output as well! D'oh!

Unknown said...

@djcb: Not with me, did you test that it's fixed?

Took your new code but the "{{{DIRECTIVES}}}" don't get expanded, in the LaTeX-templates, just check C-c C-e L to see yourself.

And I couldn't find a notion of a #+MACRO: directive in org-mode's info files... maybe some el code for substitiution of variables in templates is missing ?!?

Maybe I should better ask at the mailing list?

Unknown said...

OK I was able too find MACRO documented in the online version 6.33 of org-modes help file.

Seems that the org-mode (6.21b) bundled with the newest emacs 23.1.50.1 is already too old ...

djcb said...

@LanX: I do have a fairly new org-mode, but I think this should work with 6.21b. I justed tested once more, at it al works fine here, it seems.

Anonymous said...

Lanx, djcb,

Got it working.

The key is to pick up a later version of org-mode.

Here are the details: When using an older version of org-mode (version 6.14) (with djcb's corrected .emacs snippet) it still wasn't working quite right - the title page was coming up blank (save the date). None of the MACRO parameters were being passed to the .tex file. Apparently 6.14 org to tex translation function doesn't pick up MACROs.

So I picked up the latest 6.33c version of org-mode and everything seems to be working now!

Thanks.

Cheers!

Unknown said...

@djcb: what does "C-h v org-version" give you?

I grepped my org-mode-info file for MACRO...no hit!

djcb said...

@LanX: 6.33a. I think MACRO was added in February of 2009 -- not sure in what release it debuted. When I find the exact version, I'll update the page.

BTW, MACRO is documented on the org-mode website.

Unknown said...

Hi

>BTW, MACRO is documented on the org-mode website.

I know, that's what I said. (November 15, 2009 6:28 AM)

I checked it with 6.31 from a Emacs CVS trunk installation and it worked now. 8)

Seems like the emacs-snapshot installation for ubuntu is still too old.

(Please note: both emacs installations claim same version, but the org-mode bundled is different:

GNU Emacs 23.1.50.1 (i486-pc-linux-gnu, GTK+ Version 2.14.4)
of 2009-08-01 on leucaena, modified by Debian

vs

GNU Emacs 23.1.50.1 (i686-pc-linux-gnu, GTK+ Version 2.14.4)
of 2009-10-13
)


Thanks
Rolf

Rajarshi Tiwari said...

Thanks a lot friend :-)
Your blog has been of quite a help to me, and I learned various things too.

Rajarshi

jwhendy said...

How do your ditaa images come out? Mine don't look so hot in pdf... they look a bit grainy/pixely. Is there a way to increase the quality? I didn't see any options on the ditaa site for doing so.

Just wondered how yours turn out?

djcb said...

@Hendy: I just tried some of the sample images,and they seem to come out just fine... but that does not help you much.

To debug this, it's probably best to look at smaller steps -- is the problem in the image creation (you could try ditaa 'by hand', without org-mode), or maybe the way LaTeX includes the image?

Anonymous said...

@Hendy: At least with Ditaa 0.9, using the scale option helps. The image will have the same size but a higher resolution in the PDF:

#+begin_ditaa blue.png -r -S -s 2.5
...

jwhendy said...

Anonymous: Thanks a ton. That's fantastic and works like a charm.

jwhendy said...

Although... I don't understand why the -s option doesn't scale my image larger? From the ditaa website, the syntax section lists '-s' as "-s,--scale A natural number that determines the size of the rendered image. The units are fractions of the default size (2.5 renders 1.5 times bigger than the default)."

Does that must mean in terms of image quality? What if I did want my image to actually be 1.5 the size (width/height) larger than the default?

Anonymous said...

Recent org-mode has integrated support for exporting to Beamer. See the tutorial at http://orgmode.org/worg/org-tutorials/org-beamer/tutorial.html
It has some nice features like support for columns environment.