Adding Animations to LaTeX

3 minute read

I recently gave a departmental seminar of some of my most recent doctoral research here at the University of Saskatchewan. As a part of that presentation, to emphasize how a particular variation of sum rule worked, I included this animation in my presentation:

Gaussian sum rule demonstration

My presentation was built using LaTeX (using the presentation package beamer), and I created a lot of the figures and diagrams using either Inkscape (for diagrams) or Mathematica (for the plots).

I played around for a while with different strategies to animate within LaTeX, and this is a summary of what I finally came up with after some hours of searching and experimenting. I generate the frames of my animation using Mathematica. I then use the \animategraphics command coming from the LaTeX package animate to animate the frames when compiling my beamer presentation.

First, to produce the figure, I used a toy spectral function defined by a sum of Gaussians in Mathematica

spectralfunction[x_] := Exp[-(x - 3)^2] + Exp[-((x - 7)^2/2)]/Sqrt[2] + Exp[-((x - 11)^2/3)]/Sqrt[3]

For my purposes, I was interested in plotting this toy spectral function alongside a Gaussian function, as well as the product of the Gaussian function with the toy spectral function. The code for the animation within Mathematica would look something like this (I’ve eliminated some normalization factors from my own plot displayed above that shouldn’t change how the code works):

Animate[
    Plot[{spectralfunction[x], Exp[-(s - x)^2/(4)], spectralfunction[x]Exp[-(s - x)^2/(4)]}, {x, 0, 15},
      PlotRange -> Full,
      Ticks -> None,
      ImageSize -> Large,
      PlotLegends -> Placed[{"\!\(\*SuperscriptBox[\(\[Rho]\), \(had\)]\)", "Gaussian kernel", "GSR"}, Below],
      AxesLabel -> {"t", ""}
      ],
    {s, 0, 14, 0.1}
    ]

To make this animation using the \animategraphics LaTeX environment, we need to export this animation to a series of still frames. To do this, instead of plotting the above animation above, I instead created a Table holding each frame of the animation (essentially switching out the Animate command for a Table command):

frameTable = Table[
        Plot[{spectralfunction[x], Exp[-(s - x)^2/(4)], spectralfunction[x]Exp[-(s - x)^2/(4)]}, {x, 0, 15},
            PlotRange -> Full,
            Ticks -> None,
            ImageSize -> Large,
            PlotLegends -> Placed[{"\!\(\*SuperscriptBox[\(\[Rho]\), \(had\)]\)", "Gaussian kernel", "GSR"}, Below],
            AxesLabel -> {"t", ""}
            ],
      {s, 0, 14, 0.1}
      ];

Note that suppressing the output is useful here if you’re dealing with a large number of images. To export these images, I iterate through each element of frameTable, exporting to the filename animation-GSRfit_{FRAMENO}.png, where {FRAMENO} ranged from 001 to 141. This will be important in order to use the animate package.

Table[
  Export[
      FileNameJoin[{StringJoin["animation-GSRfit_", StringTake[ToString[NumberForm[i, 2, NumberPadding -> {"0", ""}]], -3], ".png"]}],
      table[[i]],
      "PNG"],
    {i, Length[table]}
    ]

I’ve coded in number padding into the filenaming convention above such that all files will have the same number of digits in their naming convention. In this case, my frame numbering runs from 001 to 141.

Finally, in LaTeX (using the beamer package), I used this block of code to render all the images into an animated figures

\begin{frame}{Gaussian Sum Rules}
\centering
\animategraphics[autoplay,loop,height=5cm]{30}{animation-GSRfit_}{001}{141}
\end{frame}

The frame environment is an element of beamer; the \animategraphics command takes the following syntax:

\animategraphics[options]{frames per second}{filename without frame number}{first frame number}{last frame number}

My figures were saved with the filename animation-GSRfit_{FRAMENO}, where {FRAMENO} ranged from 001 to 141, indicating the beginning and end of our animation that we would indicate here. Many more options can be explored in the manual for the animate package.

One note: I have noticed that not all PDF viewing programs have been able to handle these animations; the only reliable options I’ve found has been Adobe Acrobat Reader, though I’m sure there’s others.

I have to note that there are tools within Mathematica to export GIFs and video files. There are discussions around whether this is worthwhile; I personally like the control that exporting individual frames gives me. For the 140 frames I generated for this simple plot, it took a few minutes on my Lenovo X220 laptop (Intel® Core™ i5-2540M CPU @ 2.60GHz × 4), and the total size of all the files produced was only 1.5 MB. I don’t often use animations in my presentations, so it is possible I’d rethink this strategy if I were generating these figures more often.

No webmentions were found.