Including Mathematica animations in beamer presentations


  1. Create a sequence of images using mathematica
  2. Convert the sequence of images to a movie file
  3. Embed the movie in a beamer presentation

Create a sequence of images using mathematica

We are going to animate a rotating cardioid. Running the following code in mathematica should create 50 frames of the rotation.

(* Rotation Matrix *) rot[r_]:={{Cos[r],-Sin[r]},{Sin[r],Cos[r]}} (* Function that renders a rotated cardioid in a 400x400 image size. For encoding to a movie, we should ensure that both the height and width are divisible by 8. *) g[r_]:=ParametricPlot[rot[r].{Cos[t]*(1 - Cos[t]), Sin[t]*(1 - Cos[t])}, {t, 0, 2 Pi}, AspectRatio -> Automatic, PlotRange -> {{-2, 2}, {-2, 2}}, ImageSize -> {400,400}] (* filename "frame??.jpg" (We need the filename to be padded with zeros like frame01.jpg. There is probably a better way to do this. *) filename[k_]:=If[k < 10,"frame0" <> ToString[k] <> ".jpg", "frame" <> ToString[k] <> ".jpg"] (* Number of frames *) frames=50; (* Save the frames *) Do[Export[filename[k],g[k*2*Pi/frames],"JPEG"],{k,0,frames-1}]

The result should be a sequence of images "frame??.jpg" with k running from 00 up to 49 in our directory.

Using mencoder to encode movies

We will use the open source mencoder to encode our movies. There are other alternatives as well. I believe the non-free quicktime pro also would work.

Installation of mencoder on a Mac

To install mencoder on a Mac, you need to be able to open zip files. The most popular program to do this is StuffitExpander.

Mencoder is free software, and is distributed with the free program ffmpegX. The most current version is available at In the future, you may want to check and download the latest version.

Download the file "" to your desktop. Then double click on it. StufitExpander should unzip it into a folder on your desktop called "ffmpegXbinaries20060307 Folder".

Now, open a terminal and do the following. You will have to enter your password to do the sudo command.

cd ~/Desktop/ffmpegXbinaries20060307\ Folder sudo cp mencoder /usr/bin

You should now be able to execute "mencoder" on the command line (in a terminal). You can now remove the file "" and the directory "ffmpegXbinaries20060307 Folder".

Installation of mencoder on Linux

Typically, mencoder is included with the mplayer installation in linux.

Convert the sequence of images to a movie file

We will encode the jpeg frames into a movie using mencoder. See the mencoder manual. The following must be run in a terminal.

mencoder mf://frame??.jpg -mf w=400:h=400:fps=25:type=jpg -ovc lavc -ffourcc XVID -nosound -o output.avi

You will need to adjust the "w=400:h=400" portion to account for your images' width and height that you generated with mathematica. You should test the generated movie, "output.avi", on a Mac by attempting to view the movie in Quicktime.

Embed the movie in a beamer presentation

We will be following Jens Nöckel's instructions for emebeding a movie in a latex presentation.

Download "movie15.sty" from CTAN's movie15 directory. If you need additional help, you may wish to consult the readme that can be found on the same page.

Also "ifdraft.sty" is needed by "movie15.sty". Download "ifdraft.dtx" from CTAN. Then execute the following.

tex ifdraft.dtx

This will generate our needed file "ifdraft.sty".

Move the movie, "ifdraft.sty", and "movie15.sty" to the same directory, where we will create our latex presentation.

We give the following demonstration for embedding our movie an a beamer presentation. I call this file "movie.tex".

\documentclass{beamer} \mode < presentation > { \usetheme[compress]{Boadilla} } \usepackage{movie15} \title{A movie in a latex presentation} \author{Me} \begin{document} A rotating cardioid is amazing! \begin{center} \begin{figure}[ht] \includemovie[ poster, % shows initial frame before play begins controls, % controls are only available on a Mac (they will not appear on windows.) % (if no controls are used, then you can double click the movie to start it.) repeat, % repeat the movie over and over. text=(Loading movie...) % text shown while loading movie ]{6cm}{6cm}{output.avi} \end{figure} \end{center} \end{document}

Now run pdflatex.

pdflatex movie

This should generate the file "movie.pdf", which contains our embedded movie. In order to open the pdf file to see the movie, you will need to use a recent version of Adobe Reader on either OsX or Windows.

More information on how to use "movie15.sty" can be found in the author's package description.

Return to Pat's Mathematics Website.