HOME

PDF (letter size)

Animating Mathematica graphics inside PDF using LaTeX animate package

Nasser M. Abbasi

April 23, 2018   Compiled on January 30, 2024 at 2:01am

The goal is to embed in a pdf file an animation made up of a sequence of images generated using Mathematica and to be able to use live controls inside the PDF to control the play of the animation.

This can be done using LaTeX animate package. These examples used TeX Live distribution to compile the LaTeX file to pdf. Any other LaTeX distribution such as Mike TeXcan also be used.

The steps are simple and illustrated here using two examples. The first example uses the output of Table[Plot[...]] to generate the plots used as the image frames, and the second uses the output of the Manipulate command.

Any other Mathematica command that can generate sequence of images in that can be exported into sequence of video frame images can be used for animation with this method.

The two PDF files created from these examples are

  1. f.pdf
  2. m.pdf

Important note The PDF file has to be opened using an Adobe PDF reader (or a compatible pdf reader). For example, the animation will not run if the PDF file is opened inside the browser using the browser buildin PDF reader. Browsers do not yet support animations inside PDF at the time this note was written.

1 example using Table(Plot...)

  1. Start Mathematica and create a new notebook File->New notebook and save it to the folder where the animation images will be saved to and type the following Mathematica commands

     SetDirectory[NotebookDirectory[]]; 
     f = Table[Plot[Sin[a + x], {x, 0, 10}], {a, 10}]; 
     Export["f.png", f, "VideoFrames"];

    The above will generate 10 .png images in the same folder where the notebook is saved

  2. Create a text file say f.tex in the same folder using an editor with the following LaTeX code.

    \documentclass{article} 
    \usepackage{animate} 
    \usepackage{graphicx} 
    \begin{document} 
    \begin{center} 
      \animategraphics[controls,loop,width=3in]{3}{f}{1}{10} 
    \end{center} 
    \end{document}

    The count of the frames (10 in this example) used in the above LaTeX code should match the number of frame images created by Mathematica. Hence the count starts from 1 to 10. Any other values within this range can also be used.

    The {3} in the above, is the frame rate. I found that a value around 3 to 6 works best for most purposes.

    The documenation for the package animate contains more information as well description of other options that can be used when creating the animation in PDF.

    Compile the LaTeX  file to PDF using either

     pdflatex f.tex

    or

    lualatex f.tex
  3. f.pdf now contains these images as animation. The animation will be embeded at the place where the above animate command in the LaTeX  file. The animation can be played using the controls that are build into the package.

2 example using Manipulate output

  1. The output of Manipulate can also be saved and played in the PDF file.

    m = Manipulate[Plot3D[Sin[x y + a], {x, 0, 6}, {y, 0, 6}], {a, 0, 4}]; 
    Export["m.png", m, "VideoFrames"]

    The Export command will create 60 frames

  2. Create a text file say m.tex in the same folder using an editor that contains the following

    \documentclass{article} 
    \usepackage{animate} 
    \usepackage{graphicx} 
    \begin{document} 
    \begin{center} 
      \animategraphics[controls,loop,width=4in]{5}{m}{1}{60} 
    \end{center} 
    \end{document}

    Compile the above file to a PDF file

     pdflatex m.tex
  3. The resulting m.pdf contains these images as an animation

    The speed of animation can also be adjusted using the controls shown.

3 references

  1. the Latex animate package for reference only. Included in TeX Live
  2. getting TeX Live on Linux
  3. Mike TeX for windows