Including movies in LaTeX beamer

All technological gimmicks are allowed in today’s fierce scientific presentation arms race. Even videos! This is of course almost unheard of in the world of mathematicians who like to stick with blackboards and view OHP’s as useless snobbery. But the times, they are a-changin’, as some famous 1960’s guy put it.

It’s quite easy to create a pdf file with an embedded video file. To show you how, let me first construct a simple movie using Matlab:

% Number of frames
Nframes = 60;

% Define angles, one for each frame
phi = linspace(0,pi,Nframes);

% Construct 2D evaluation points
[x,y] = meshgrid(linspace(-2*pi,2*pi,256));

% Loop over angles
for iii = 1:Nframes
  % Evaluate exponential function
  plotfun = exp(i*(cos(phi(iii))*x+sin(phi(iii))*y));
  % Plot imaginary part of the exponential (i.e. sine)
  figure(1); clf
  imagesc(real(plotfun))
  % Axis settings
  axis square
  axis off
  % save image to file in .png format. 
  eval([‘print -dpng sineframe’, num2str(iii,’%03d’), ‘.png’])
end

The line above with ‘%03d’ is formatting ensuring that each filename has the number in the form 001, 002, … as this is sometimes important for correct alphabetical order.

It is, of course, possible to use Matlab to create the video file. However, I prefer to use Photoshop Extended for that purpose. Regardless of the software, this is how the video looks like: sineframes

You can add the video to a beamer slide with the following LaTeX code:

\documentclass[graphics]{beamer}
\usepackage{movie15}
\begin{document}
\begin{frame}{This is a movie showing a rotating wave}
\begin{picture}(320,300)
\put(0,80){\includemovie[poster, text={\small(Loading sineframes.avi)}]{9cm}{6cm}{sineframes.avi}}
\put(90,260){Rotating sine}
\end{picture}
\end{frame}
\end{document}

Note that we use the picture environment for allowing precise positioning using coordinates. Here is the pdf file: Sinemovietalk

Now sometimes my computer refuses to show the movie in the pdf slide, especially when giving a talk in a conference room. I guess the communication between my computer and the projector has some problems, or the issue might be related to resolution settings. To avoid such problems I developed a low-tech but extremely robust way of showing videos using pdf files produced with LaTeX beamer. Namely, the video frames are simply slides in the presentation!

The above video has 60 frames, and it is quite typical for videos to have far more frames than that. It’s rather tedious to write the beamer frames by hand in such cases, so why don’t employ Matlab for that? The following lines output LaTeX code:

for iii = 1:Nframes
    disp(‘\begin{frame}{}’)
    disp(‘\begin{picture}(320,260)’)
    disp([‘\put(-20,-30){\includegraphics[width=12.4cm]{sineframe’, num2str(iii,’%03d’), ‘.png}}’])
    disp(‘\put(-19.8,244.4){\Large \bf This is a movie showing a rotating wave}’)
    disp(‘\end{picture}’)
    disp(‘\end{frame}’)
    disp(‘ ‘)
end

Now copy and paste the above to Matlab, run it, and then copy and paste the output to a LaTeX file. The result looks like this (with many slides removed):

\documentclass[graphics]{beamer}
\begin{document}

\begin{frame}{}
\begin{picture}(320,260)
\put(-20,-30){\includegraphics[width=12.4cm]{sineframe001.png}}
\put(-19.8,244.4){\Large \bf This is a movie showing a rotating wave}
\end{picture}
\end{frame}

% TEXT REMOVED FOR BREVITY

\begin{frame}{}
\begin{picture}(320,260)
\put(-20,-30){\includegraphics[width=12.4cm]{sineframe060.png}}
\put(-19.8,244.4){\Large \bf This is a movie showing a rotating wave}
\end{picture}
\end{frame}

\end{document}

Here is the resulting talk, ready to be shown in any seminar room: Sinemovietalk_noanim

About Samuli Siltanen

I am an applied mathematician interested in medical imaging. For example, I study probing the human body using X-rays or harmless electrical currents, and using computers and mathematical methods to form three-dimensional reconstructions of patients from the data so collected. In my free time I like to train with kettlebells and to photograph landscapes and animals.
This entry was posted in Beamer tricks, LaTeX tricks, Matlab examples. Bookmark the permalink.

1 Response to Including movies in LaTeX beamer

  1. Xianghua says:

    Very very appreciate, finally I successed in embeding video. Is this can only embded .avi video and mpg?

Leave a Reply

Your email address will not be published. Required fields are marked *