# Animated plotting with LaTex?

I am not a LaTeX pro, that's why I don't know if the following is possible and how I could do it.

I have a graphic (a plot, jpg) and it shows a time series. I know in PowerPoint it is possible that if one comes to the slide in the presentation, the time series curve is drawn, so not just showing up/appearing, but it is slowly drawn as a curve, so one could see that the computer actually draws the line, as a human would do if he draws the line. Does anyone know how I could do this?

-
why not: use the beamer package and its methods for incremental slide creation. Then, configure your favority LaTeX plot tool such that it draws the first N points for slide 1, the first N+k for slide 2, the first N+2k for slide 3 etc. Commonly used plot tools are pgfplots.sourceforge.net/pgfplots.pdf or pstricks (although I do not know if pstricks works together with beamer). Try it - if you run into problems, you can ask here for details. – Christian Feuersänger May 19 '12 at 9:00
BTW: welcome on TeX.SE! – Christian Feuersänger May 19 '12 at 9:01
thanks for your help, but, I use R, I have a time series and what do I have to do now? I do not know how to do this in R? Thanks for your help – studenteconomics1 May 19 '12 at 9:21
Have a look at the animate package on ctan – Yiannis Lazarides May 19 '12 at 11:07
It seems as if you feel uncomfortable with your decision (?) to use LaTeX. If you want to use LaTeX then you should have good starting points here on this site (export your timeseries data from R, learn how to plot it in LaTeX, learn beamer, learn the combination). If you have the impression that the cost/gain ratio is not as good as you hoped then you should reconsider why you dismissed PowerPoint in the first place ("would take me 5 minutes" is a strong argument). Why did you, by the way? – Christian Feuersänger May 19 '12 at 20:17

In this answer, I will explain how to make animations in both PDF and GIF format:

Make sure you have installed the following applications and set the PATH system variable to their locations.

1. TeXLive or MikTeX.
2. GhostScript. You can install either the 32 or 64 bit version. The corresponding application names for 32 bit and 64 bit are gswin32c.exe and gswin64c.exe repectively.
3. ImageMagick. It is required to create GIF animations. You can install either the 32 or 64 bit version.

The first two sections will guide you to create some supporting files that will simplify your workflow. The last section will show how to make use of the supporting files to create animations.

## Creating A Multi Purpose Batch File for GIF Animation

Create a batch file named GifAnimate.bat as follows. It is recommended to save it in a separate directory such that any other projects can make use of it. Don't forget to set the PATH to its location.

rem this is GifAnimate.bat

rem %1 : filename (without extension) of a PDF file having pages to be animated.

rem %2 : delay between frames (in 1/100 miliseconds).

rem %3 : density of the GIF output. The higher it is the bigger output.

echo off

rem remove the previous GIF animation if any.
del %1.gif

convert -delay %2 -loop 0 -density %3 %1.pdf %1.gif


## Creating A Multi Purpose Batch File and Template for PDF Animation

Create a batch file named PdfAnimate.bat as follows. It is recommended to save it in a separate directory such that any other projects can make use of it. Don't forget to set the PATH to its location.

rem this is PdfAnimate.bat

rem %1 : filename (without extension) of a PDF file having pages to be animated.

rem %2 : frame rate (in frames per second).

rem %3 : scale of the GIF output. The higher it is the bigger output.

echo off

rename %1.pdf %1-animate.pdf


del %1-animate.pdf


Create a template file named PdfAnimateTemplate.tex as follows. It is recommended to save it in your local TDS such that any other projects can make use of it.

% this is PdfAnimateTemplate.tex
\documentclass[preview,border=12pt]{standalone}
\usepackage{animate}
\begin{document}
\animategraphics[autoplay,loop,scale=\OutputScale]{\FrameRate}{\InputFileName}{}{}
\end{document}


## Creating GIF and PDF Animations

Create a LaTeX input file named test.tex as follows:

\documentclass[pstricks,border=12pt]{standalone}

\usepackage{pst-plot}

\psset{plotpoints=3000,algebraic}

\begin{document}
\multido{\r=-3.5+0.5}{15}
{
\begin{pspicture}(-4,-2)(4,2)
\psaxes[arrows=->,linewidth=0.2pt](0,0)(-3.5,-1.5)(3.5,1.5)[$x$,0][$y$,90]
\psplot[linecolor=red]{-3.5}{\r}{sin(3*x)}
\end{pspicture}
}
\end{document}


First compile it with either LaTeX or XeLaTeX.

With LaTeX is as follows (do one after another):

latex test.tex
dvips test.dvi
ps2pdf test.ps


With XeLaTeX is as follows:

xelatex test.tex


You will have a PDF output named test.pdf. It consists of 15 pages.

To get the corresponding GIF animation, execute GifAnimate.bat test 20 250, we will get the following output:

To get the corresponding PDF animation, execute PdfAnimate.bat test 25 3, we will get a PDF animation (cannot be shown here).

## Miscellaneous

Responding to Yiannis Lazarides' comment below. I am lazy to animate Batman as the curve has many discontinuities. Instead, I present another example. Hopefully the following graph is more exciting!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-solides3d}

\psset{viewpoint=50 20 10 rtp2xyz,Decran=50,linewidth=0.5\pslinewidth}

\begin{document}
\multido{\rx=-0.5+0.05,\ry=-0.45+0.05,\i=1+1}{20}
{
\begin{pspicture}(-0.5,-0.5)(0.5,0.5)
\rput(0,0){%
\begin{pspicture*}(-0.5,-0.5)(0.5,0.5)
\defFunction[algebraic]{helicespherique}(t)
{0.5*cos(10*t)*cos(t)}
{0.5*sin(10*t)*cos(t)}
{0.5*sin(t)}
\ifnum\i=1
\psSolid
[
object=sphere,
linewidth=0.1pt,
linecolor=red,
resolution=720,
action=draw*,
ngrid=9 9,
r=0.5
]\fi
\psSolid
[
object=courbe,
linewidth=0.05pt,
linecolor=blue,
resolution=720,
range=pi \rx\space mul \ry\space pi mul,
function=helicespherique,
r=0.01
]
\end{pspicture*}}
\end{pspicture}
}
\end{document}


## Responding to the given comment

If you want to animate test.pdf (consists of 15 pages) in your PDF document, use the following template and compile with pdflatex.

\documentclass{article}
\usepackage{animate}
\begin{document}
% your other contents go here
\animategraphics[autoplay,loop,scale=<OutputScale>]{<FrameRate>}{test}{}{}
% your other contents go here
\end{document}


where

• <OutputScale> is a real number representing the scaling factor.
• <FrameRate> is an integer representing frames per second.

Of course, < and > are not the part of the syntax.

-
Great example! Can you use a more exciting curve? Maybe the batman? – Yiannis Lazarides May 20 '12 at 15:23
Excellent! Thanks for all the effort. – Yiannis Lazarides May 20 '12 at 19:14
Full disclaimer: I haven't actually tried your solution but I'm being nit picky anyway. In your first .bat file, there is a space before each file ending (del %1. gif) but in the second there isn't (del %1-animate.pdf). Just double checking that this is intentional. – ThomasH Jun 3 '13 at 13:41
How has the example to be modified, if I want do include more animations to the final pdf? – Peter Ebelsberger Jul 19 '13 at 23:19
@PeterEbelsberger: Sorry for replying late because I did not notice your comment. See my last edit at the bottom. – kiss my armpit Oct 21 '13 at 8:03

Your best bet is to use the animate package. Have a look for an example on page 14 of its manual. You will need to use Acrobat Reader to view it properly.

-