PDF (letter size)
PDF (legal size)

Animating Mathematica graphics inside PDF using LATEX animate package

Nasser M. Abbasi

April 23, 2018   Compiled on April 23, 2018 at 10:49pm

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 easily LATEXanimate package. These examples used TEX Live distribution to compile the LATEXfile to pdf. Any other LATEXdistribution 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. The animation will not run if the PDF file is opened inside the browser using the browser buildin PDF reader. These do not yet support animations inside PDF at this time.

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

    pict

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

    \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 LATEXcode 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 more purposes.

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

    Compile the LATEXfile 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 LATEXfile. The animation can be played using the controls that are build into the package.

    pict

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

    pict

  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

    pict

    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