# How to use autoplay (within animate package) together with a pause at the end of every playing?

I am trying to include the following animated gif:

into beamer document and I am using animate package.

When I follow this instructions: http://www.geogebratube.org/forum/viewtopic.php?f=20&t=25986 the resulting picture differs from the original in that way, that there is no stagnation in the movement at the end of one loop. The required stagnation is probably made by repeating one frame within some period. However when one decomposes the original gif according the instructions into frames, then there is no repeated frame (which causes the unwanted smooth motion in the end). What I am looking for is some way how to adjust autoplaying that will go from the first frame 0 to the last (let's say 22nd) and then, before it skips to the beginning again, it will repeat the last (the 22nd) frame for some while. Thank you for you advice.

-

The easiest thing is to use the animateinline environment and then "manually" include the images, and lastly make the last image have a slow frame rate.

However, I would like to show you a couple of possibilities with the animate package.

Throughout this the distinction between image and frame is not enforced. Please ask if in doubt.

## Animation problems

Often including an animation in a file creates huge pdf's which is often not desirable.

One way to circumvent this is to re-use the images whenever they have something in common. Thus for plots, some of the stuff in an animation that will be constant are:

1. Axis lines
2. Tick descriptions
3. Legends
4. Titles
5. Common data points
6. etc.

All these are stored in each frame unless one carefully reuses them. As already hinted, this can greatly reduce the pdf file size (especially if a lot of the frames can be re-used, or combination thereof).

The feature known as timelines for the animate package can handle these things.
The timeline has the capability of reusing several images without taking more space.

In the below examples I will demonstrate this.

## animateinline

The animateinline environment is the manual way of including images, instead of using \animategraphics. It also provides with some nice features to control the animation:

1. \newframe parses the current frame and goes to the next frame
2. \newframe* parses the current frame and goes to the next frame, however, an instantaneous pause is inserted in the frame.
3. \newframe[<frame rate>] parses the current frame and goes to the next frame, however, the frame rate for the remaining animation is set to <frame rate> (notice that it is reset when it loops)
4. combination of 2. and 3. are allowed!

\begin{animateinline}[controls,loop]{5}
\multiframe{5}{i=0+1}{%
\includegraphics{fig\i.pdf}
}
\newframe
\includegraphics{fig4.pdf}
\newframe[1]
\includegraphics{fig4.pdf}
\end{animateinline}


If you did not include the last picture three times you would also experience a hang in between the 4th and 5th frame. Thus you need to include it three times. Try and experiment and see for yourself.

## Timelines, the greatness of animations

This is probably where animate has a great advantage of use compared to out of the box animation creations. As already mentioned you are allowed to handle transparencies and combine however you wish each frame.

The basic idea is a timeline file which holds information about when and how each frame is utilized.

Each line is made out of three columns, separated by a :. The three columns holds the following information:

1. Supply an asterisk (*) or nothing (* for pause)
2. Supply an integer (f) or nothing (f for frame rate)
3. Supply a frame formatting string
• Each format is comma (,) separated (the left-most format is the bottom one in the current frame)
• A simple integer format (i) tells animate to use frame i, frames start from count 0 (i is the same as ix1)
• A format as ixn, where i and n are integers (x is actually x). i tells which frame to use and n tells for how many frames it is going to stay on the animation. Thus: 2x2 will tell the animation to insert frame 2 and stay for a total of 2 frames. If n=0 it will be there for the remaining part of the animation.
• A c which clears the transparency stack. This is useful for removing any ix0 frames inserted.

For examples see animate.

## Example frames

Here is some code to generate several frames of interest.

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz,pgfplots}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}

% As one would normally do it...
\tikzsetfigurename{fig}
\foreach \x in {1,2,...,5} {
\begin{tikzpicture}
\path[use as bounding box] (-.4,-.4) rectangle (5.1,4.3);
\begin{axis}[small,xmin=-1,xmax=6,ymin=-1,ymax=6]
\end{axis}
\end{tikzpicture}
}

% Utilizing the transparency features of animate
% First we need a plot of the background
% This should also include any labels inserted
\tikzsetnextfilename{background}
\begin{tikzpicture}
\path[use as bounding box] (-.4,-.4) rectangle (5.1,4.3);
\begin{axis}[small,xmin=-1,xmax=6,ymin=-1,ymax=6]
% empty
\end{axis}
\end{tikzpicture}

% Then all the fill, notice that we limit to only the "changing"
% data in this regard
\tikzsetfigurename{fig_no_axis}
\foreach \x in {1,2,...,5} {
\begin{tikzpicture}
\path[use as bounding box] (-.4,-.4) rectangle (5.1,4.3);
\begin{axis}[small,xmin=-1,xmax=6,ymin=-1,ymax=6,hide axis]
\end{axis}
\end{tikzpicture}
}
\end{document}


This will create several files in the current folder.

## Size decrease

Lets compare the images created in the previous section (on my pc that is):

1. fig<0-4>.pdf are 18kb (all information)
2. background.pdf is 17kb (only axis descriptions and axis lines)
3. fig_no_axis<0-4>.pdf are 1.4kb (only data points)

Ok, so already here you can infer what I mean about size reduction. It should be noted that these images does not contain many data points. Thus the size decrease is larger than would be expected for large data sets, however, if the number of frames are very large, it could have an impact on large data sets as well. Also creating animations from pdf's generally can utilize similar stuff (so the size change for pdf images are minimal).

### Notice to have transparency on images

When using transparency on pictures (that is 0x0,1,2) it is necessary that the images contain an alpha-channel, that is: transparency. I would recommend using png images.

## Animation through timelines

Here are two codes which produces the same animation:

\animategraphics[controls,loop]{5}{fig}{0}{4}


and using the constant background with the timeline:

% \usepackage{filecontents}
\begin{filecontents}{smallsize.tml}
::0x0,1
::2
::3
::4
::5
\end{filecontents}
\begin{animateinline}[controls,loop,timeline=smallsize.tml]{5}
\includegraphics{background.pdf}
\newframe
\multiframe{5}{i=0+1}{%
\includegraphics{fig_no_axis\i.pdf}
}
\end{animateinline}


Notice that we use the 0th frame for the entire animation, and also have the 1st image on the first frame (0x0,1).

Inserting a pause as you requested for the last frame can be done like this:

% \usepackage{filecontents}
\begin{filecontents}{smallsizepause.tml}
::0x0,1
::2
::3
::4
::5
:1:5
\end{filecontents}
\begin{animateinline}[controls,loop,timeline=smallsizepause.tml]{5}
\includegraphics{background.pdf}
\newframe
\multiframe{5}{i=0+1}{%
\includegraphics{fig_no_axis\i.pdf}
}
\end{animateinline}


Notice that now it is one more image long, with a changed frame rate for the last image. Through this, you can fine tune the frame rate, pauses etc. in the animation.

Notice that it could have been made shorter by having background.pdf=fig_no_axes0.pdf and fig_no_axes<0-4>.pdf=fig_no_axes<1-5>.pdf and then used \animategraphics[timeline=<name>.tml]{5}{fig_no_axes}{0}{5}.

-
Thank you for this great tutorial on the timeline feature of animate! – AlexG Sep 19 '12 at 7:16
@AlexG you are welcome, the timeline feature is highly underrated! – zeroth Sep 19 '12 at 7:25
I always wonder whether the timeline option description in the doc is clear enough or too long to be read. – AlexG Sep 19 '12 at 8:08
@AlexG I like your (I am guessing you are Alexander Grahn?) explanation with the tables, the examples are great. I think that they best describe their use. With such a complicated feature, examples speak a lot more than words! To my taste I would probably cut down text and add short examples which built up to a complete transparency show. The text can be difficult to grasp at points, albeit I have no idea how to make it clearer (do not take offence of this!). – zeroth Sep 19 '12 at 8:16
Thanks a lot for your comments! Unfortunately, inventing examples is quite time consuming. It would be nice if users contribute examples to the texample.net site. – AlexG Sep 19 '12 at 9:30