# Animations in LaTeX

I am a new user of LaTeX and this site. I have seen that many answers in this site have animations. However I am not able to produce any of these animations. For example, I asked a question and a got this answer, but when I compile this with pdflatex, I get this pdf which does not have any animation. What am I doing wrong?

-
Basically the PDF is giving you something like individual frames of the animation - one per page. To actually make them animate you need something which supports that. The animations people post tend to be .gifs created from the pdf output, I assume. beamer can also create animated effects by quickly displaying one image after another e.g. including the first pdf/other image on slide one, the second on slide two and so on. beamer also has some other tricks up its sleeve in conjunction with packages such as tikz etc. which can gradually change colouring, for example, to simulate movement. –  cfr Jan 4 '14 at 4:40
you can find some details in plotting the sequence x_n using tikz or How to convert pstricks animation to GIF file? for example –  cmhughes Jan 4 '14 at 4:44
@cfr Ok, So is there no way to directly produce animations in PDFs? –  Kartik Jan 4 '14 at 4:49
@Kartrik I am not sure but I think it depends on the capabilities of your pdf viewer. So you can create animations using beamer, for example. You could use tikz or whatever to create the different slides in situ or you could include different frames from external files. But whether that will appear animated will, I think, depend on the viewer you use. (Obviously this is true for gif, too - your browser has to be capable - but I think it is more unusual for pdf viewers to support that kind of animation than for browsers to support animated gifs.) –  cfr Jan 4 '14 at 4:57
You can convert that pdf file in to a gif file using imagemagick. convert -delay 30 -loop 0 -density 200 -alpha remove yourfile.pdf yourfile.gif. I usually have a batch file for this job. –  Harish Kumar Jan 4 '14 at 5:53

Since the OP asks for creating an animated PDF using the animate package without the need to have the animation frames in a separate (PDF) file, the tikzpicture environment can be directly put into an animateinline environment:

\documentclass{article}
\usepackage{animate}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{A}{%
\symbol{F}{\pgflsystemstep=0.6\pgflsystemstep\pgflsystemdrawforward}
\rule{A->F[+A][-A]}
}

\begin{document}
\begin{animateinline}[controls,autoplay,loop]{2}
\multiframe{8}{n=1+1}{
\begin{tikzpicture}[scale=10,rotate=90]
\draw (-.1,-.2) rectangle (.4,0.2);
\draw [blue,opacity=0.5,line width=0.1cm,line cap=round]
l-system [l-system={A,axiom=A,order=\n,angle=45,step=0.25cm}];
\end{tikzpicture}
}
\end{animateinline}
\end{document}

-
THIS was what I wanted. All the other answers gave GIFs –  Kartik Jan 9 '14 at 11:22
This is nice. I found it works also if one replaces \documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone} with just \documentclass{article} to allow more pages in the document. And one can then make animations on different pages and not just one page as the case is with standalone style. –  Nasser Jan 9 '14 at 22:26
@Nasser: Yep, animate is fully doc-class ignorant. Don't know why people keep putting it in relation to the preview and standalone packages/classes. –  AlexG Jan 10 '14 at 7:38

There are two things here,

1. to produce a gif file (which we do here normally, in this site for uploading).
2. to have the animation inside the pdf file.

For first, my take will be imagemagick. Install imagemagick and ensure that convert.exe is in system path. Produce your pdf file (as you have shown in the link in OP). Then issue this command from within the same folder using command prompt.

convert -delay 10 -loop 0 -density 400 -alpha remove lsystems.pdf lsystems.gif


Change the parameters as needed. For details refer to imagemagick's documentation. Ususally, I prefer a batch file for repeated use. Save the contents below in to a file named mygifbatch.bat inside the same folder as your pdf file.

@ECHO ON
cls

REM convert to animated gif

CD /D %~dp0
mkdir gifs
SET Program="convert.exe"
for %%A in (*.pdf) do %Program%  -delay 30 -loop 0 -density 200 -alpha remove %%A gifs/%%~nA.gif
Pause


To convert, double click on mygifbatch.bat file and it will convert all pdf files in the current folder in to gif files inside a sub-directory gifs.

Now the second. You can use animate package as in the following code:

\documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}

\usepackage{filecontents}
%% This is your file to be animated
\begin{filecontents*}{lsystems.tex}
\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}\PreviewEnvironment{tikzpicture}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{A}{
\symbol{F}{\pgflsystemstep=0.6\pgflsystemstep\pgflsystemdrawforward}
\rule{A->F[+A][-A]}
}
\begin{document}
\foreach \n in {1,...,8} {
\begin{tikzpicture}[scale=10,rotate=90]
\draw (-.1,-.2) rectangle (.4,0.2);
\draw
[blue,opacity=0.5,line width=0.1cm,line cap=round]
l-system [l-system={A,axiom=A
,order=\n,angle=45,step=0.25cm}];
\end{tikzpicture}
}

\end{document}
\end{filecontents*}
%
\immediate\write18{pdflatex lsystems}

%% convert to GIF animation. Uncomment following line to have a gif animation in the same folder.
%\immediate\write18{convert -delay 10 -loop 0 -density 400 -alpha remove lsystems.pdf lsystems.gif}
%%

\usepackage{animate}
\begin{document}
\begin{preview}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{2}{lsystems}{}{}
\end{preview}
\end{document}


Here I used filecontents to write your .tex file and pdflatexed it from within the main document. Hence you will need to use --shell-escape while compiling. If you already have the pdf file, you don't need lines 3--32. The pdf should be viewed using adobe reader. For more details refer to animate documentation (texdoc animate or at to texdoc.net).

There is a nice arara rule written by Chris (cmhughes) which makes production of gif from pdf easy. Install the cool tool arara from Paulo's github repository. It is included in texlive. But you have to install it by yourself if you are a miktex user. Now save the contents of cmhughes code in to a file by name animate.yaml. Put this file in some folder, say C:\Users\<your name>\AppData\Roaming\Arara\rules (for windows). Then create a file araraconfig.yaml in your home directory (C:\Users\<your name> usually) with the following contents:

!config
paths:


% arara: pdflatex
% arara: animate: {density: 200, delay: 20}


Sample code (lsystems.tex):

% arara: pdflatex
% arara: animate: {density: 200, delay: 20}
\documentclass[tikz]{standalone}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{A}{
\symbol{F}{\pgflsystemstep=0.6\pgflsystemstep\pgflsystemdrawforward}
\rule{A->F[+A][-A]}
}
\begin{document}
\foreach \n in {1,...,8} {
\begin{tikzpicture}[scale=10,rotate=90]
\draw (-.1,-.2) rectangle (.4,0.2);
\draw
[blue,opacity=0.5,line width=0.1cm,line cap=round]
l-system [l-system={A,axiom=A
,order=\n,angle=45,step=0.25cm}];
\end{tikzpicture}
}
%
\end{document}


Compile lsystems.tex with - arara lsystems.tex. arara can also be integrated with many editors. For details, refer to beautiful arara manual.

-
Thanks. The second one was what I wanted –  Kartik Jan 4 '14 at 11:40
I am very familiar with this idea. :-) But I think preview environment is no longer needed to sandwich \animategraphics because by default standalone does the job for us. Try to remove the preview environment and you will see a miracle. :-) –  stalking isn't tolerated Jan 4 '14 at 19:27
Hehehe. Thanks. –  stalking isn't tolerated Jan 4 '14 at 22:42

Another way to get .gif files, in addition to @Harish answer, is GIMP. This is my usual procedure and it is useful for uploading in websites: you find an example (of an external web page with respect to TeX.SX) in http://cfiandra.github.io/Sa-TikZ/. The example contains an animation for presentations: it is done with TikZ (code below). Indeed, you can create animations in Beamer without the animate package.

For demonstration purposes I will exploit my solution from Draw a closed liquid-droped shaped curve with TikZ: the file is named liquid_shape.tex.

Let's now open the file liquid_shape.pdf with GIMP and import all pages:

Typing CTRL+SHIFT+E it is possible to export the image; change the file extension to .gif:

and export; automatically GIMP recognizes you want to create an animation and it shows you a window like this:

Select Like animation (in italian Come animazione) and possibly customize the delay between overlays (I selected 1200 ms).

Export and you will have liquid_shape.gif.

-

I am not certain how useful this is at this point. However, here is a slightly elaborated version of my comment together with an example of an animation created with beamer. Unlike the other examples, this one is extremely simple.

Basically the PDF linked to in the question consists of individual frames which could be used to make up an animation, with one frame per page. To actually make them animate you need something which supports that. The animations people post tend to be .gifs created from the pdf output. A couple of ways of producing those have been mentioned in the other answers.

beamer can also create animated effects by quickly displaying one image after another. One way to do that is to include different frames on different slides and to tell beamer to animate them. \includegraphics is overlay-specification aware, for example. Essentially, you include the first frame on the first slide, the second on the second slide and so on.

beamer also has some other tricks up its sleeve in conjunction with packages such as tikz etc. These can create animated effects just using the TeX code. For example, you can gradually change colouring to simulate movement. The following example fades from one text to another. I used a version of this in a presentation to convey the idea that the second text was explaining the first. I've just altered the texts here to something TeX-related. When you click on the first slide, the text fades out and is replaced by the text on the final slide which fades in. As I say, this is nothing like the sophisticated animations other people post - it is just about as simple as an animation could be, I suppose. But it can be quite effective for all that. I wanted the focus to be on the content of the text, after all - a really impressive animation would just serve to distract from the point being made. (Obviously this doesn't really apply with the dummy text I've used here but think about trying to convey the meaning of a key concept or the nature of a discipline.)

\documentclass{beamer}
\usepackage{fixltx2e}
\usepackage{texnames}

% lliwiau arbennig
\setbeamertemplate{background canvas}[default]
\setbeamercolor{background canvas}{bg=black}
\setbeamercolor{normal text}{fg=blue!40!white,bg=black}
\setbeamercolor{quote}{fg=blue!40!white,bg=black}

%\title[]{}
\title{Animation with Beamer}
\subtitle{An Extremely Simple Example}
\date{}
\subject{}  % PDF cat. only

\begin{document}

\begin{Form}

\setbeamertemplate{background}{}

\newcount\aopaqueness
\newcount\qopaqueness
\begin{frame}[plain]
\animate<2-19>
\animatevalue<1-10>{\aopaqueness}{100}{0}
\animatevalue<11-20>{\qopaqueness}{0}{100}
\begin{center}
\begin{overprint}[.65\paperwidth]
\onslide<1-10>
\begin{colormixin}{\the\aopaqueness!averagebackgroundcolor}
\mbox{ }\vspace*{\fill}\\
\begin{center}
\huge\bfseries
pdf\LaTeX\\[.5em]
with beamer and friends
\end{center}
\vspace*{\fill}\mbox{ }\\
\end{colormixin}
\onslide<11-20>
\begin{colormixin}{\the\qopaqueness!averagebackgroundcolor}
\begin{quote}
\usefont{T1}{pzc}{m}{n}\huge
Beautiful typography\dots\\[.5em]
for paper and screen.\\[.5em]
\end{quote}
{\hspace*{\fill}\small \emph{Presenting without Tears}, A.\ Typesetter}
\end{colormixin}
\end{overprint}
\end{center}
\end{frame}

\end{Form}

\end{document}


EDIT: Thanks to a helpful comment, I have captured the animation as displayed in acroread. Although this is a gif, the animation itself is in the PDF. The gif is just recording the screen shot. Although the recording seems to lose this, the animation is actually triggered by clicking on the first slide. However, for some reason the recording loses the picture of the pointer shown in acroread when actually interacting with the PDF, so you'll just have to imagine that bit. Also, in the PDF, the animation only plays once as you would expect for something intended to convey the relationship between two pieces of text.

EDIT: Worked fine in preview but I only see the last, still frame now I've posted it. No idea why...

-
may be this meta thread helps to get the feel meta.tex.stackexchange.com/questions/2868/… –  texenthusiast Jan 4 '14 at 19:29
@texenthusiast Thanks. This worked perfectly when I edited it and previewed the result. Now I've posted it, though, it only shows the final frame and I've no idea why. –  cfr Jan 4 '14 at 23:30
Wait - why does it suddenly work now? Very confused! –  cfr Jan 4 '14 at 23:38

You can create .gif animations by the follwing steps:

1. Create a PDF with multiple pages. Every page is one part of the animation. The easiest way to do this might be the beamer document class
2. Use pdfcrop and imagemagicks convert to create the animation.

## For Linux users

To make step 2 trivial, I use the following Makefile for every projects:

SOURCE = bellman-ford-algorithm
DELAY = 80
DENSITY = 300
WIDTH = 512

make:
pdflatex $(SOURCE).tex -output-format=pdf make clean clean: rm -rf$(TARGET) *.class *.html *.log *.aux *.toc *.snm *.out *.nav

gif:
pdfcrop $(SOURCE).pdf convert -verbose -delay$(DELAY) -loop 0 -density $(DENSITY)$(SOURCE)-crop.pdf $(SOURCE).gif make clean animatedGif: make pdfcrop$(SOURCE).pdf
convert -verbose -delay $(DELAY) -loop 0 -density$(DENSITY) $(SOURCE)-crop.pdf$(SOURCE).gif
make clean

transparentGif:
convert $(SOURCE).pdf -transparent white result.gif make clean png: make make svg inkscape$(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png

svg:
make
#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
pdf2svg $(SOURCE).pdf$(SOURCE).svg
# Necessary, as pdf2svg does not always create valid svgs:
inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
rsvg-convert -a -w $(WIDTH) -f svg$(SOURCE).svg -o $(SOURCE)2.svg inkscape$(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg rm$(SOURCE)2.svg

1. Just save it as Makefile (be aware of the fact that Makefiles have tabs, not four spaces! So copy-and-paste might not work)
2. Replace the first 4 variables according to your needs
3. Type make animatedGif in the shell of your choice (most might use bash, but zsh with the oh-my-zsh plugin offers tabbed autocompletion for Makefiles :-))

# Examples

## Eulerian path

See my article "How to visualize Graph algorithms" for more information how this was created:

## Cholesky decomposition

Source: LaTeX-examples

-
This is a great answer! I am trying to write it for OSX here apple.stackexchange.com/questions/128400/… –  Masi Apr 22 '14 at 17:57