12

I couldn't find code examples that were simple enough to help me start using animate package. I would like to ask you to provide some of your own examples that would be simple enough to build upon.

It would help me the most if you follow these guidelines:

  • two animation examples are needed
  • both examples should use standalone class and as less functionality not related to animate and tikz packages as possible
  • animation in each example should have no more than 4 frames (for simplicity's sake)
  • one animation should advance to next frame only when mouse button is clicked
  • another animation should advance through frames automatically but each frame should pause (or appear) for different number of seconds than other frames

It would also help if you breakdown the structure of a text file used by animate to store frame data and what options are possible on each frame and how to use them (package's documentation seems to lack information on this).

19

Update 3: Split the answer into two parts to shorten it as requested by @ArtificialStupidity here.

Update 2: Added images produced with the new export option added by @AlexG in his answer here

First answer: use of the \animategraphics command

Entirely realized with the animate and tikz packages.

The animate package has a large number of options that give it great power. For example you can run the animation in one direction then in the other (palindrome), step by step (step), with control buttons (controls), looping infinitely (loop) and many others that you can discover on its manual.

animation-decoupage-parallelogramme

  • It shows that the area of a parallelogram is equal to that of a rectangle by cutting and resizing.
  • It has 198 images, more than the 4 maximum requested. This provides a convenient method for generating the timeline file.
  • Its speed varies: it increases then decreases, its last image remains visible longer.
  • It explains the \animategraphics command and the very powerful timeline option.

To start

I wrote a file that builds the animation with tikz in standalone class. This tex file creates the 198 images of the animation called parallelogramme.pdf.

\documentclass[tikz]{standalone}
\usepackage{animate}
\usepackage{fontawesome}
\begin{document}

\foreach \y  in {0,.2,...,3}{% cut out the parallelogram
    \begin{tikzpicture}
        \useasboundingbox (-2.5,-.5) rectangle (4,4);
        \fill[green!40](-1,0)--(-1,3)--(3,3)--(2,0)--cycle; 
        \fill[green!40](-2,0)--(-1,3)--(-1,0)--cycle;
        \draw[dashed](-1,0)--(-1,\y);
    \end{tikzpicture}
 }   

    \begin{tikzpicture}% circle the cut out in dotted lines
        \useasboundingbox (-2.5,-.5) rectangle (4,4);
        \fill[green!40](-1,0)--(-1,3)--(3,3)--(2,0)--cycle;
        \fill[green!40](-2,0)--(-1,3)--(-1,0)--cycle;
        \draw[densely dotted](-1,0)--(-1,3)--(-2,0)--cycle;
    \end{tikzpicture}

 \foreach \iangle in {180,179,...,0}{% move the triangle
    \begin{tikzpicture}
        \useasboundingbox (-2.5,-.5) rectangle (4,4);
        \fill[green!40](-1,0)--(-1,3)--(3,3)--(2,0)--cycle;

        \fill[green,opacity=.4] (\iangle:20mm and 8mm)--([shift={(1,3)}]\iangle:20mm and 8mm)--([shift={(1,0)}]\iangle:20mm and 8mm)--cycle;
        \node at ([shift={(.6,.2)}]\iangle:20mm and 8mm)[black]{\faHandPointerO};
        \draw[densely dotted,thin](-1,3)--(-2,0)--(-1,0)--cycle;
        \ifthenelse {\iangle=0}{\draw[densely dotted,fill=green!40](2,0)--(3,0)--(3,3)--cycle;
                            \node at ([shift={(.6,.2)}]0:20mm and 8mm)[black]{\faHandPointerO};}{}      
     \end{tikzpicture}
    }


\end{document}

You will notice that the standalone documentclass is written with the dedicated tikz option that creates a single pdf page for each tikz graphic: documentclass[tikz]{standalone} and not

\documentclass{standalone}
\usepackage{tikz}

because this last way of doing it creates a single standalone file with all tikz graphics, it looks like this:

standalone-without-tikz-option

or gives an error if there are too many tikz graphics:

Dimension too large.

Create an image in gif format?

If you want to create an.gif image, you can use the Imagemagick software by opening a command line in the folder containing the series of pdf images you just created.

To get the .gif image set at the beginning, I copied and pasted this command: see @nox explanation here: https://tex.stackexchange.com/a/443304/138900

To vary the scrolling speeds, I chose :

 convert -density 100 -loop 0 -background white -alpha remove -delay 100 parallelogramme.pdf[0] -delay 10 parallelogramme.pdf[1-16] -delay 8 para-un-pdf.pdf[17-40] -delay 4 parallelogramme.pdf[41-196] -delay 300 parallelogramme.pdf[197] parallelogramme.gif

Make the pdf animation:

To make a pdf animation with the animate package, we could use the command \animategraphics and without any other option, we lose the speed variation (here 30 frames per second):

[![animation-without-speed-variation][6]][6]

\documentclass[tikz]{standalone}
\usepackage{animate}

\begin{document}
\animategraphics{30}{para-un-pdf}{}{}
\end{document}

Change the speed with the timeline option:

To vary the speeds, either you increase the number of frames, but this makes the file and compilation heavier, or you use the timeline option.

The power of the timeline file:

The timeline file describes and composes each image of the animation, then:

  • Each line compose a single image. So there are as many lines as there are images.
  • Each pdf pages are considered transparencies. An image (a frame) is now a stack of different pages (transparencies) of the pdf.
  • It allows the same transparency to be reused several times at different points in the animation.
  • It can also speeds up, slows down or stops animation.
  • The first page of the pdf is transparency number 0.
  • the second is numbered 1, etc.

Each line of the timeline file is composed as follows:

[*]:[<frame rate>]:[<transparencies>][:<JavaScript>]
  • The first element [*] is either * or empty. If there is * then the animation stops at that image.
  • The second element [<frame rate>] is either empty or indicates the number of frames per second.
  • The third element [<transparencies>] indicates the stacking of transparencies.
  • I refer you to the package manual for the [:<JavaScript>] option and much more explanations.

For example, if the first 5 lines of the timeline file are:

::0x0,1x18
:10:3
*::4
::5
::6
  • ::0x0,1x18 : then the transparency 0 is copied on all the following images; above it is the transparent 1 repeated 18 times (line 0 to 17);
  • :10:3 : the second image is composed of the transparent 3 (added to the existing stack of transparencies) with a speed of 10 frame per second;
  • *::4 the fourth transparency is added to the stack that will make the third image and the animation stops on this image.
  • etc.

If you want to modify the background image during animation, you will have to replace commas (,) by semicolons (;)that will create overlay layers. See manual for more details.

Generate the timeline file:

To avoid having to write manually a 198 lines timeline file, we use the LaTeX (or TeX ?) \write command. This tex file creates the timeline file called agencement.txt:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{multido}
\usepackage{ifthen}

\newwrite\Fichier 
\immediate\openout\Fichier=agencement.txt
\immediate\write\Fichier{:2:0}
\immediate\write\Fichier{:10:1}
\multido{\ix=2+1}{14}%
{%
  \immediate\write\Fichier{::\ix}%
}
\immediate\write\Fichier{:1:16}
\immediate\write\Fichier{:12.5:17}
\multido{\ix=18+1}{23}{%
\immediate\write\Fichier{::\ix}
}
\immediate\write\Fichier{:25:41}
\multido{\ix=42+1}{155}{%
\immediate\write\Fichier{::\ix}
}
\immediate\write\Fichier{:.3:197}
\immediate\closeout\Fichier% Don't forget to close the file

\begin{document}
Timeline file created
\end{document}

This tex file generates the animation on pdf called parallelogramme-animated.pdf. It use the previously created parallelogramme.pdf and the timeline file agencement.txt:

\documentclass[tikz]{standalone}
\usepackage{animate}

\begin{document}

\animategraphics[loop,timeline=agencement.txt]{30}{parallelogramme}{}{}

\end{document}

Write a stack of transparencies?

This lightens considerably the compilation and the pdf animation. The very large number of actions allowed by this timeline file complicates its writing.

I do it these time. I rewrote the parallelogramme.tex file to make it generate not images, but differents transparencies called parallelogramme-bis.tex

\documentclass[tikz]{standalone}
\usepackage{animate}
\usepackage{fontawesome}
\begin{document}

\begin{tikzpicture}% remaining cut - transparent 0
    \useasboundingbox (-2.5,-.5) rectangle (4,4);
    \fill[green!40](-1,0)--(-1,3)--(3,3)--(2,0)--cycle;
\end{tikzpicture}

\begin{tikzpicture}% triangle cut out green- transparent 1
    \useasboundingbox (-2.5,-.5) rectangle (4,4);
    \fill[green!40](-2,0)--(-1,3)--(-1,0)--cycle;
\end{tikzpicture}

\begin{tikzpicture}% circle the cutout in dotted lines - transparent 2
    \useasboundingbox (-2.5,-.5) rectangle (4,4);
    \draw[densely dotted](-1,0)--(-1,3)--(-2,0)--cycle;
\end{tikzpicture}

\foreach \y  in {0,.2,...,3}{% cut out the parallelogram - transparent 3-18
    \begin{tikzpicture}
        \useasboundingbox (-2.5,-.5) rectangle (4,4);       
        \draw[dashed](-1,0)--(-1,\y);
    \end{tikzpicture}
 }   
\foreach \iangle in {179,...,0}{% move parallelogram - transparent 19-199
    \begin{tikzpicture}
        \useasboundingbox (-2.5,-.5) rectangle (4,4);
        \fill[green,opacity=.4] (\iangle:20mm and 8mm)--([shift={(1,3)}]\iangle:20mm and 8mm)--([shift={(1,0)}]\iangle:20mm and 8mm)--cycle;
        \node at ([shift={(.6,.2)}]\iangle:20mm and 8mm)[black]{\faHandPointerO};
        \ifthenelse {\iangle=0}{\draw[densely dotted,fill=green!40](2,0)--(3,0)--(3,3)--cycle;
                            \node at ([shift={(.6,.2)}]0:20mm and 8mm)[black]{\faHandPointerO};}{}      
     \end{tikzpicture}
    }
\end{document}

We can create the timeline file called agencement-bis.txtwith LaTeX:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{multido}
\usepackage{ifthen}

\newwrite\Fichier 
\immediate\openout\Fichier=agencement-bis.txt

\immediate\write\Fichier{::0x0,1x18}
\immediate\write\Fichier{:10:3}
\multido{\ix=4+1}{14}%
{%
  \immediate\write\Fichier{::\ix}%
}
\immediate\write\Fichier{:12.5:2x0}
\multido{\ix=18+1}{180}%
{%
  \ifthenelse {\ix=17}{\immediate\write\Fichier{:12.5:\ix}}{
        \ifthenelse {\ix=40}{\immediate\write\Fichier{:25:\ix}}{
                                \immediate\write\Fichier{::\ix}}
                                }
}
\immediate\write\Fichier{:.3:198}
\immediate\closeout\Fichier% always close the file
\begin{document}

timeline file created.

\end{document}

The final animation is created with the \animategraphics from the previously created parallelogramme-bis.pdf and the timeline agencement-bis.txt

\documentclass[tikz]{standalone}
\usepackage{animate}
\begin{document}
\animategraphics[loop,timeline=agencement-bis.txt]{30}{parallelogramme-bis}{}{}
\end{document}

animation-decoupage-parallelogramme

Translated with www.DeepL.com/Translator

  • 2
    Thank you for this nice tutorial, in particular for the timeline part! – AlexG Aug 10 '18 at 11:26
  • 2
    @AlexG I love all your interactive packages (animate, ocgx, media9) with which I make beautiful beamer presentations that my students admire, thanks to you! – AndréC Aug 10 '18 at 11:44
  • 1
    Thank you! (It's ocgx2; ocgx is by P. Gaborit.) – AlexG Aug 10 '18 at 11:53
8

The following two MWEs should give you a general idea on how you could use the animate package. In order to successfully compile these examples, you will need four images called example_1 to example_4 in the same directory as your .tex file.

1: Animation proceeds to the next transparency only when clicking on the mouse button: (Please note the step option)

\documentclass{standalone}
\usepackage{graphicx}
\usepackage[step]{animate}

\begin{document}
\animategraphics[width=\linewidth]{12}{example_}{1}{4}%
\end{document}

2: Animation automatically proceeds to the next transparency with different framerate for each step: (Note: For a more in-depth explanation of a timeline please refer to the animate manual)

\documentclass{standalone}
\usepackage{graphicx}
\usepackage{animate}
\usepackage{filecontents}

\begin{filecontents}{mytimeline.txt}
:0.5:0   %  1/0.5*1s=2s
:0.2:1  
:10:2  
:1:3    
\end{filecontents}

\begin{document}
\animategraphics[timeline=mytimeline.txt,width=\linewidth]{12}{example_}{1}{4}%
\end{document}
  • 1
    For the last frame to be shown for 1 s (1 frame/s), one could add the loop option. – AlexG Nov 18 '17 at 16:30
5

Update: Split the previous answer into two parts to shorten it as requested by @ArtificialStupidity here.

Second answer: use of the animateinline environment

ove-animation

  • It illustrates the different stages of construction with the ruler and the compass of an ove;
  • it illustrates the notion of stacking layers of transparencies that allow the screen background to be modified without hiding the previously stacked transparencies.
  • it explains the use of the animateinline environment.
  • although the gif animation above does not show a pause, the pdf animation below generates one for each image.

Create images or transparencies?

During a ruler and compass construction, the figure is constructed successively with small drawings that are added to each other. So creating transparencies that stack one on top of the other naturally reproduces this way of building.

The animation has 7 images made with the following 9 transparencies:

transparencies1

transparencies2

The first 7 transparencies represent the stages of construction, the last 2 are the backgrounds used to illustrate the underlay of transparencies.

One background is green, the other is the pattern pattern=dots. When the latter pattern is stacked, the backgrounds below it remain visible. The green background hides all the backgrounds below him.

Create an image in gif format?

It is now possible thanks to the magnificent update published on August 22, 2018 and this without needing as before to rewrite all the code. To do this, simply add the export option either to the standalone package or to the animate package:

\documentclass{standalone} \usepackage[export]{animate}

or \documentclass[export]{standalone} \usepackage{animate}

This produces a pdf file consisting of a series of individual pages that can be easily converted to gif with, for example, Imagemagick as shown above.

The images produced with the export option are the following 7:

image-1-4

image-5-7

The animateinline environment

  • it allows to group in a single file the creation of each of the transparents as well as their animation and as long as to make the generation of the timeline file.
  • it has two commands \newframe and \multiframe which allow to create, either images independent of each other, or transparencies whose stacking will form images.
  • it allows to factor the start and end code of each tikzpicture environment which is repeated with each creation of frames thanks to the begin and end options.

The power of the timeline file:

The timeline file describes and composes each image of the animation, then:

  • Each line compose a single image. So there are as many lines as there are images.
  • Each pdf pages are considered transparencies. An image (a frame) is now a stack of different pages (transparencies) of the pdf.
  • It allows the same transparency to be reused several times at different points in the animation.
  • It can also speeds up, slows down or stops animation.
  • The first page of the pdf is transparency number 0.
  • the second is numbered 1, etc.

Each line of the timeline file is composed as follows:

[*]:[<frame rate>]:[<transparencies>][:<JavaScript>]
  • The first element [*] is either * or empty. If there is * then the animation stops at that image.
  • The second element [<frame rate>] is either empty or indicates the number of frames per second.
  • The third element [<transparencies>] indicates the stacking of transparencies.
  • I refer you to the package manual for the [:<JavaScript>] option and much more explanations.

For example, if the first 5 lines of the timeline file are:

::0x0,1x18
:10:3
*::4
::5
::6
  • ::0x0,1x18 : then the transparency 0 is copied on all the following images; above it is the transparent 1 repeated 18 times (line 0 to 17);
  • :10:3 : the second image is composed of the transparent 3 (added to the existing stack of transparencies) with a speed of 10 frame per second;
  • *::4 the fourth transparency is added to the stack that will make the third image and the animation stops on this image.
  • etc.

If you want to modify the background image during animation, you will have to replace commas (,) by semicolons (;)that will create overlay layers. See manual for more details.

The begin and end options:

For example, below the opening is always composed of the code

begin{tikzpicture}
    \useasboundingbox (-2.5,-2.5) rectangle (4,2.5);

we created a \Debut command that will write these lines automatically to each new frame created with a \newframe or multiframe command:

Newcommand{\Debut}{% Systematic start of drawing
\begin{tikzpicture}
\useasboundingbox (-2.5,-2.5) rectangle (4,2.5);}

Similarly, for closing the tikz environment, with the following command:

Newcommand{\Fin}{\end{tikzpicture} }

Animation code

    \documentclass{standalone}
    \usepackage{tikz}
    \usetikzlibrary{patterns}
    \usepackage{animate}

    % creation of the ove.txt timeline file
    \newwrite\Fichier 
    \immediate\openout\Fichier=ove.txt
    \immediate\write\Fichier{:.5:7x0;0x0}% the dots background is stacked first in all images
    \immediate\write\Fichier{::8;1x0}%   the green background is opaque and hides the dot background
    \immediate\write\Fichier{::;2x0}%   nothing covers the background of the transparency 0 which is therefore visible again
    \immediate\write\Fichier{::c;4x0}%  the letter c deletes all transparencies that have been added in the stack
    \immediate\write\Fichier{::8x2;3x0}% the green background will be visible twice, so until the next image
    \immediate\write\Fichier{::7x1;5x0}%  the dots background is visible only once, so writing x1 is useless
    \immediate\write\Fichier{::;6} %the stack is empty and no background is added, so there is no visible background
    \immediate\closeout\Fichier% always close the file

    \newcommand{\Debut}{% Systematic start of drawing
    \begin{tikzpicture}
    \useasboundingbox (-2.5,-2.5) rectangle (4,2.5);}
    \newcommand{\Fin}{\end{tikzpicture} }% Systematic end of drawing
    \begin{document}
    % Step through the animation one frame at a time per mouse-click. The <frame rate> argument will be ignored.
    \begin{animateinline}[autoplay,step,begin={\Debut},end={\Fin},timeline=ove.txt]{.5}
    % perpendicular straight - transparent 0
        \draw[thick] (-2.5,0)--(4,0);
        \draw[thick] (0,-2.5)--(0,2.5);
    \newframe% circle - transparent 1
        \draw[thick] (0,0) circle (2cm);
    \newframe%  half-line 1 - transparent 2
        \draw[thick] (0,-2)--(3,1);
    \newframe%  half-line 2 - transparent 3
        \draw[thick] (0,2)--(3,-1);
    \newframe%  arc 1 - transparent 4
        \draw[thick](0,2)arc[start angle=90,end angle=45,radius=4cm];
    \newframe%  arc 2 - transparent 5
        \draw[thick](0,-2)arc[start angle=-90,end angle=-45,radius=4cm];
    \newframe%  arc 3 - transparent 6
        \draw[thick]([xshift=2cm]45:1.171573cm)arc[start angle=45,end angle=-45,radius=1.171573cm];
    \newframe% dots screen background - transparent 7
        \fill[pattern=dots] (-2.5,-2.5) rectangle (4,2.5);
    \newframe% green screen background - transparent 8
        \fill[green!30] (-2.5,-2.5) rectangle (4,2.5);              
    \end{animateinline}

    \end{document}

Write a sequence of images?

The file that creates the same sequence of images called ove.pdf is:

\documentclass[tikz]{standalone}
\usepackage{animate}
\usetikzlibrary{patterns}
\tikzset{every path/.style=thick}
\begin{document}

\begin{tikzpicture}% perpendiculars - 1
    \fill[pattern=dots] (-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
\end{tikzpicture}

\begin{tikzpicture}% circle - image 2
    \fill[green!30] (-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
\end{tikzpicture}

\begin{tikzpicture}%  half-line 1 - image 3
    \fill[pattern=dots](-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
    \draw (0,-2)--(3,1);
\end{tikzpicture}

\begin{tikzpicture}%  arc 1 - image 4
    \useasboundingbox (-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
    \draw (0,-2)--(3,1);
    \draw(0,2)arc[start angle=90,end angle=45,radius=4cm];
\end{tikzpicture}

\begin{tikzpicture}%  half line 2 - image 5
    \fill[green!30] (-2.5,-2.5) rectangle (4,2.5);
    \fill[pattern=dots](-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
    \draw (0,-2)--(3,1);
    \draw(0,2)arc[start angle=90,end angle=45,radius=4cm];
    \draw (0,2)--(3,-1);
\end{tikzpicture}

\begin{tikzpicture}%   arc 2 - image 6
    \useasboundingbox (-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
    \draw (0,-2)--(3,1);
    \draw(0,2)arc[start angle=90,end angle=45,radius=4cm];
    \draw (0,2)--(3,-1);
    \draw(0,-2)arc[start angle=-90,end angle=-45,radius=4cm];
\end{tikzpicture}

\begin{tikzpicture}%  arc 3 - image 7
    \useasboundingbox (-2.5,-2.5) rectangle (4,2.5);
    \draw (-2.5,0)--(4,0);
    \draw (0,-2.5)--(0,2.5);
    \draw (0,0) circle (2cm);
    \draw (0,-2)--(3,1);
    \draw (0,2)--(3,-1);
    \draw(0,2)arc[start angle=90,end angle=45,radius=4cm];
    \draw(0,-2)arc[start angle=-90,end angle=-45,radius=4cm];
    \draw([xshift=2cm]45:1.171573cm)arc[start angle=45,end angle=-45,radius=1.171573cm];
\end{tikzpicture}
\end{document}

The file that creates the animation from the ove.pdf file containing the images is:

\documentclass[tikz]{standalone}
\usepackage{animate}

\begin{document}
\animategraphics[autoplay,step]{1}{ove}{}{}
\end{document}

Weight of the animation pdf:

  • By stacking transparencies created in the standalone class with the animateinline environment, the pdf animation weighs on my computer 11172 bytes .
  • When the animation is a succession of images created with the \animategraphics command in the standalone class, the animation weighs 18539 bytes.

The creation of transparencies is 40% lighter in this case than a succession of images.

I hope I have said what is essential so that everyone can do the same. I hope too I have been clear, if not, say so, I will try to explain better.

Translated with www.DeepL.com/Translator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.