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
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.
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
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
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.
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
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
The resulting m.pdf contains these images as an animation
The speed of animation can also be adjusted using the controls shown.