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.

- 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
To build an animation, we create each frame one by one exactly like we do to make a movie.
To do this, we create a loop that at each iteration builds a single image with the tikzpicture environment.
In the example that follows the iteration is done with a loop
\foreach \y in {0,0.2,...,3} % generates 16 frames 0; 0.2; 0.4; 0.6; etc
{\begin{tikzpicture}
....% image creation
\end{tikzpicture}
}% end of foreach
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}% <- each tikzpicture environment creates a different image.
and not
\documentclass{standalone}% <- all tikzpicture environments form a single image
\usepackage{tikz}
because this last way of doing it creates a single standalone file with all tikz graphics, it looks like this:

or gives an error if there are too many tikz graphics:
Dimension too large.
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;
% (\iangle:20mm and 8mm) is a "canvar polar" with 2 différents radius (ellipse)
\fill[green,opacity=.4,shift={(\iangle:20mm and 8mm)}](0,0)--(1,3)--(1,0)--cycle;
\node at ([shift={(.6,.2)}]\iangle:20mm and 8mm)[black]{\faHandPointerO};% hand symbol from fontawesome
\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}
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.txt
with 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}
