[Tex/LaTex] Animated plotting with LaTex

graphicsplot

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?

Best Answer

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

pdflatex -interaction=batchmode --jobname=%1 "\newcommand\InputFileName{%1-animate}\newcommand\FrameRate{%2}\newcommand\OutputScale{%3}\input{PdfAnimateTemplate}"

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:

enter image description here

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!

enter image description here

\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.

Related Question