saveLatex {animation}R Documentation

Insert animations into a LaTeX document and compile it


Record animation frames and insert them into a LaTeX document with the animate package. Compile the document if an appropriate LaTeX command is provided.


saveLatex(expr, nmax, = "Rplot", ani.opts, centering = TRUE, 
    caption = NULL, label = NULL, pkg.opts = NULL, documentclass = "article", 
    latex.filename = "animation.tex", pdflatex = "pdflatex", install.animate = TRUE, 
    overwrite = TRUE, full.path = FALSE, ...)



an expression to generate animations; use either the animation functions (e.g. brownian.motion()) in this package or a custom expression (e.g. for(i in 1:10) plot(runif(10), ylim = 0:1)).


maximum number of animation frames (if missing and the graphics device is a bitmap device, this number will be automatically calculated); note that we do not have to specify nmax when using PDF devices.

basename of file names of animation frames; see the Note section for a possible adjustment on


options to control the behavior of the animation (passed to the LaTeX macro '\animategraphics'; default to be 'controls,width=\linewidth')


logical: whether to center the graph using the LaTeX environment \begin{center} and \end{center}

caption, label

caption and label for the graphics in the figure environment


global options for the animate package


LaTeX document class; if NULL, the output will not be a complete LaTeX document (only the code to generate the PDF animation will be printed in the console); default to be article, but we can also provide a complete statement like \documentclass[a5paper]{article}


file name of the LaTeX document; if an empty string '', the LaTeX code will be printed in the console and hence not compiled


the command for pdfLaTeX (set to NULL to ignore the compiling)


copy the LaTeX style files ‘animate.sty’ and ‘animfp.sty’? If you have not installed the LaTeX package animate, it suffices just to copy these to files.


whether to overwrite the existing image frames


whether to use the full path (TRUE) or relative path (FALSE) for the animation frames; usually the relative path suffices, but sometimes the images and the LaTeX document might not be in the same directory, so full.path = TRUE could be useful; in the latter case, remember that you should never use spaces in the filenames or paths!


other arguments passed to the graphics device ani.options(''), e.g. ani.height and ani.width


This is actually a wrapper to generate a LaTeX document using R. The document uses the LaTeX package called animate to insert animations into PDF's. When we pass an R expression to this function, the expression will be evaluated and recorded by a grahpics device (typically png and pdf). At last, a LaTeX document will be created and compiled if an appropriate LaTeX command is provided. And the final PDF output will be opened with the PDF viewer set in getOption('pdfviewer') if ani.options('autobrowse') == TRUE.


Invisible NULL


This function will detect if it was called in a Sweave environment – if so, will be automatically adjusted to prefix.string-label, and the LaTeX output will not be a complete document, but rather a single line like


This automatic feature can be useful to Sweave users (but remember to set the Sweave option results=tex). See demo('Sweave_animation') for a complete example.

PDF devices are recommended because of their high quality and usually they are more friendly to LaTeX, but the size of PDF files is often much larger; in this case, we may set the option 'qpdf' or 'pdftk' to compress the PDF graphics output. To set the PDF device, use ani.options( = 'pdf', ani.type = 'pdf')

So far animations created by the LaTeX package animate can only be viewed with Acrobat Reader (Windows) or acroread (Linux). Other PDF viewers may not support JavaScript (in fact the PDF animation is driven by JavaScript). Linux users may need to install acroread and set options(pdfviewer = 'acroread').


Yihui Xie


Examples at

To know more about the animate package, please refer to There are a lot of options can be set in ani.opts and pkg.opts.

See Also

Other utilities: im.convert, saveGIF, saveHTML, saveSWF, saveVideo

[Package animation version 2.5 Index]