texblog

The animate package

The animate package allows to include JavaScript driven animations into a pdf created with LaTeX. This can be particularly useful for beamer presentations. The biggest caveat upfront: the animations are only supported by some pdf readers (AcrobatReader, PDF-XChange, acroread, and Foxit Reader).

Using ImageMagick to convert an animated gif into several png files

The animate package uses individual images and combines them into the animation in the pdf. If you are starting out with an already animated graphic file (e.g. some animated gif) that you want to include in your LaTeX document, you will first need to convert it into individual images. For this you can use ImageMagick, which is a pretty useful command line tool (but unfortunately a little painful to install on a Mac). The following code line will convert the myAnimation.gif into myAnimation-0.png … myAnimation-9.png, assuming that myAnimation.gif consists of 10 frames.

convert -coalesce myAnimation.gif myAnimation.png

The animate command

The animate command \animategraphics allows to combine several static images into one animation.

\animategraphics[options]{frame rate}{file basename}{first}{last}
Options

The most relevant options are probably

More options are: label, type, poster, every, autopause, autoresume, palindrome, step, height, totalheight, keepaspectratio, scale, bb, viewport, trim, buttonsize, buttonfg, buttonalpha, draft, final, nomouse, method, measure. You can find details about them in the animate documentation.

Frame rate

The frame rate is the number of frames per second (FPS) and can also be smaller than 1. To show a frame for 2 seconds, you would use 0.5 FPS. 30 FPS is apparently a typical value in video encoding and higher rates than that are not reasonable.

The files

The file basename is the leftmost part of the file names of the individual images to be included. The files have to be consecutively numbered. first and last are the numbers of the first and last file to be included in the animation. If first is greater than last, the ordering will be reversed. Many different filetypes are supported (see the animate documentation for a complete list) and animate is compatible with graphicspath{} from the graphicx package. If the file is a multipage pdf, first and last are interpreted as the (zero-based) range of pages to be included as individual frames.

The animate environment

The animate package also allows to create animations from LaTeX typeset material with the environment animateinline.

\begin{animateinline}[options]{frame rate} ... typeset material ...
\newframe[frame rate]
... typeset material ...
\newframe*[frame rate]
... typeset material ...
\newframe
\multiframe{number of frames}{[variables]}{
... repeated (parameterized) material ... }
\end{animateinline}

Example


Below is an example of a tex file with two animations using the animate command and the animate environment. The visualization is an animated screen capture of the output pdf. You can view the pdf output here. The images to be included are named Temp-0.png … Temp-4.png.

\documentclass{beamer}
\usepackage{animate}

\begin{document}
  \begin{frame}{Relationship between ozone and temperature by month}
    \animategraphics[loop,autoplay,width=\linewidth]{0.8}{Temp-}{0}{4}
  \end{frame}

  \begin{frame}{A math problem}
    $4 + 5 = $ \begin{animateinline}[loop,autoplay]{0.5} $?$
    \newframe[1]
    $9$
    \end{animateinline}
  \end{frame}
\end{document}
Exit mobile version