43

I wish to display some animations in my beamer presentation. The most trivial way that comes to my mind is to use a gif image. How can I put it in the presentation and what should I use to display it? Is there any other way of doing it?

lovespeed
  • 1,310
  • You could have a look into the beameruserguide Chapter 14, which describes some different methods to do animated stuff. – Benedikt Bauer Sep 25 '12 at 16:38
  • Does using \includegraphics{<file-name.gif>} not work? – Peter Grill Sep 25 '12 at 16:46
  • 1
    @PeterGrill: GIF is not supported. – kiss my armpit Sep 25 '12 at 16:58
  • @garbagecollector: useful to mention the context, here -- when pdftex was being developed, gif format was encumbered with a software patent that applied to its compression method (lzw). there are several free software projects that avoided gif, in that context, and pdftex was one of them. it's a pity, since (as sthiadhi says) motion gif is one of the easiest motion formats to handle. – wasteofspace Sep 25 '12 at 20:21
  • @wasteofspace: Even if gif were supported by pdfTeX, the viewer application would have to support it as well, in particular its animation feature. Only gif creation software using LZW was subject to licensing, not viewing software. Even though, Adobe abstained from supporting animated gif in Acrobat. Perhaps they considered it dispensable. – AlexG Sep 26 '12 at 07:10
  • 1
    The first answer to this question worked for me and is much more straightforward than those below. – Chris Mueller Jul 17 '14 at 19:27
  • Have you found any answer which you can accept? I think there are rather many good ones. – Léo Léopold Hertz 준영 Apr 20 '17 at 07:15

2 Answers2

26

In a single run you will get 4 separate files as follows,

  • a GIF animation
  • a PDF animation
  • a MP4 video
  • a slide that contains a PDF animation and imports a MP4 video as shown in the figure below

enter image description here

Requirements

  • ImageMagick must be installed and its path must be registered to PATH system variable.
  • FFMPEG must be installed and its path must be registered to PATH system variable.

How to compile

The following input file, named as main.tex, must be compiled with pdflatex -shell-escape main. WARNING: If your OS is not Windows, then please adapt the Windows shell command to your OS shell command.

% this filename is main.tex
% compile it with "pdflatex -shell-escape main" (without the quotes)

\documentclass[mathserif]{beamer}

\usepackage{filecontents}

% Create a PDF file that consist of some pages
\begin{filecontents*}{frames.tex}
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\multido{\i=5+5}{72}
{
    \begin{pspicture}[showgrid=false](-2,-2)(2,2)
        \psparametricplot[algebraic,plotpoints=1000,linecolor=red]{0}{\i}{2*sin(7*t*Pi/180)|2*cos(11*t*Pi/180)}
    \end{pspicture}
}
\end{document}
\end{filecontents*}

\immediate\write18{latex frames}
\immediate\write18{dvips frames}
\immediate\write18{ps2pdf frames.ps}
% sometimes you need to disable auto rotate in ps2pdf. Please follow up if you really need it!
% delete auxiliary files generated by the 3 commands above.
\makeatletter
\@for\x:={tex,dvi,ps,log,aux}\do{\immediate\write18{cmd /c del frames.\x}}
\makeatother

% convert to GIF animation
\immediate\write18{convert -delay 5 -loop 0 -density 75 -alpha remove frames.pdf Lissajous.gif}

% convert to MP4
\makeatletter
\immediate\write18{convert -density 600 -alpha remove frames.pdf frames-\@percentchar04d.png}
\immediate\write18{cmd /c if exist Lissajous.mp4 del Lissajous.mp4}
\immediate\write18{ffmpeg -r 5 -i frames-\@percentchar04d.png -vcodec libx264 Lissajous.mp4}
\immediate\write18{cmd /c if exist frames-*.png del frames-*.png}
\makeatother

% convert to a single PDF animation
\begin{filecontents*}{Lissajous.tex}
\documentclass[preview,border=12pt]{standalone}
\usepackage{animate}
\begin{document}
\animategraphics[controls,loop,autoplay,scale=1]{10}{frames}{}{}
\end{document}
\end{filecontents*}

\immediate\write18{pdflatex Lissajous}
% delete auxiliary files generated by the above command.
\makeatletter
\@for\x:={tex,log,aux}\do{\immediate\write18{cmd /c del Lissajous.\x}}
\makeatother


\usepackage{animate,media9}
\begin{document}

\begin{frame}[t]{Lissajous in action}
\begin{columns}[T]
%=============
\begin{column}{0.5\textwidth}
\begin{block}{PDF Animation}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{10}{frames}{}{}
\end{block}
\end{column}
%=============
\begin{column}{0.5\textwidth}
\begin{block}{MP4}
\includemedia[
    activate=onclick,
    width=\linewidth,height=\linewidth,
    addresource=Lissajous.mp4,
    flashvars={%
        source=Lissajous.mp4%same path as in addresource!
        &autoPlay=true%optional configuration
        &loop=true%variables
    }
]{}{VPlayer.swf}
\end{block}
\end{column}
%=============
\end{columns}
\end{frame}
\end{document}

Notes:

The auxiliary file named frames.pdf must be removed manually because I cannot remove it from within main.tex. If you know how to do this, let me know!

15

the same without using an external file (the pdf is here: http://perce.de/temp/anim0.pdf):

\documentclass[mathserif]{beamer}
\usepackage{pst-plot,animate}
\begin{document}

\begin{frame}[t]{Lissajous in action}
\begin{columns}[T]
\begin{column}{0.5\textwidth}
\begin{animateinline}[%
  width=0.9\linewidth,
  begin={\begin{pspicture}(-2.1,-2.1)(2.1,2.1)},
  end={\end{pspicture}},
  controls, %palindrome, %autoplay
]{3}
\multiframe{72}{iA=50+50,iB=5+5}{
  \psset{algebraic,plotpoints=\iA,linecolor=red}
  \parametricplot{0}{\iB}{2*sin(7*t*Pi/180)|2*cos(11*t*Pi/180)}
}
\end{animateinline}
\end{column}
\begin{column}{0.5\textwidth}
\begin{align}
    x(t)&=2\sin(7t)\\
    y(t)&=2\cos(11t)    
\end{align}
\end{column}
\end{columns}
\end{frame}

\end{document}

enter image description here

  • I downloaded the pdf and viewed it in evince but the animation doesn't work. Do I need any other pdf viewer for this? – lovespeed Sep 25 '12 at 18:02
  • I suppose that it works only with acrobat –  Sep 25 '12 at 18:06
  • Apparently, pst-plot has already imported multido. – kiss my armpit Sep 25 '12 at 18:50
  • I do not need it anyway ... –  Sep 25 '12 at 18:59
  • Why did you use \whiledo, may I edit your code to use \multiframe? It is much more compact, giving the same result. – AlexG Sep 26 '12 at 08:00
  • @AlexG: copy and paste from an old example. Feel free to edit ... –  Sep 26 '12 at 08:02
  • I prefer \animategraphics to \animateinline because: (1) I can reuse the PDF file containing the pages to animate for creating another animation, a video and a series of PNG images. (2) for an unknown reason, I sometimes get an error when using \animateinline with PSTricks plus the preview package. – kiss my armpit Sep 26 '12 at 08:14
  • @ガベージコレクタ: animateinline is much more easier to use. –  Sep 26 '12 at 08:37
  • @Herbert Yes; only Adobe Reader is abel to reader PostScript animations properly (as far as I know). – Svend Tveskæg Mar 17 '13 at 16:32
  • +1. Thanks @Herbert. Very nice solution. However, I cannot typeset it in my Texshop. Both Latex and pdflatexxmk complain about undefined control sequence inside. XeLatex renders but there's only the first frame, so no animation at all. Do you have any idea why it's like that? How can I render it in TexShop? Thanks. – chepukha May 01 '13 at 18:07
  • Also, if I have several images needed to combine into an animation sequence in one slide instead of spread out in several slides, can I use pstricks for that? I like the play button and that everything is in one slide. Please note that I don't have a function to be plotted. Any suggestion? – chepukha May 01 '13 at 18:15
  • 2
    @chepukha: run the example with xelatex. It does not work with pdflatex. If you want to use several images then have a look into the documentation of animate, it has some examples. –  May 01 '13 at 18:30
  • I got it. Thank you very much. The reason was that I tested it right within TexShop, where certainly the animation won't run. Also, animate is very easy to use. Thanks for the hint. – chepukha May 01 '13 at 19:35
  • I'm getting ! No room for a new \dimen . when trying to compile it. Solved by adding \usepackage{etex} to the preamble. – liori Feb 07 '16 at 20:10
  • The link to the PDF is now dead. Can a new PDF and link be added? – James May 04 '20 at 13:40