[Tex/LaTex] How to include a PSTricks animation into a beamer

animatebeamerpstricks

I'm trying to create an animation to include into a beamer. I figured (naively I suppose) a way to do that is to create a Pdf with animate package, and then \includegraphics it into my beamer. Running on Mac Os X 10.5.8 with Texlive.

Here is what I'd like to animate :

\documentclass{article} 
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}
\usepackage{animate} 
\newcommand{\rotatingarrow}[1]{

\begin{pspicture}(0,0)(10,10)% 

\psaxes(0,0)(9,9)
\rput(6,1){\Huge{Temps = #1 s}}
\rput{#1}(4,4){

\psline[linecolor=blue,arrowsize=5pt]{->}(0,0)(2,1)\rput{#1}(2.3,1.3){\Huge{$\textcolor{blue}{\vec{v}}$}}
\psline[linecolor=red,arrowsize=5pt]{->}(2,1)(1,3)\rput{#1}(1.3,3.3){\Huge{$\textcolor{red}{\vec{a}}$}}
}

\end{pspicture}% 
} 
\begin{document} 
\begin{center} 
\begin{animateinline}[poster=last,loop,autoplay,width=5cm,height=5cm]{12}% 
\multiframe{36}{iAngle=0+10}{% 

\rotatingarrow{\iAngle}% 
}% 
\end{animateinline}% 
\end{center} 
\end{document}

Then compiling with LaTeX.

When I open it with Adobe, it does what I want. However, the animation isn't cropped. Then, pdfcrop seems to kill the file. After running pdfcrop on it, it'll be simply blank.

My question is : How do I proceed to have a cropped animation like I see in every tutorial ? How to include that animation into a Beamer (I suppose my idea is wrong).

Best Answer

What makes a PDF with working animations no longer work?

There are 3 cases that will make working animations in a PDF file NO longer work. For simplicity, let animation.pdf be the PDF file with working animations.

  1. We compress animation.pdf with gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=animation-compressed.pdf animation.pdf.
  2. We crop animation.pdf with pdfcrop animation.pdf animation-cropped.pdf.
  3. We import animation.pdf from within a LaTeX input file by using \includegraphics{animation.pdf}.


\animateinline versus \animategraphics

Recall that we cannot embed a PDF with animation into another document as what AlexG said, we have two options to solve your problem. Just choose one of the following.

  1. Use animateinline environment to sandwich your animation codes, and put the environment into your host input file (which is an input file with beamer document class in your case). However this approach has 3 drawbacks: (a) you cannot use pdflatex to compile your host input file when the animation codes in PSTricks language. (b) your host input file contains both text and codes so it become long, crowded, hard-to-understand, difficult to maintain, etc, (c) compilation will take longer time because the animation codes get recompiled whenever you make any changes. That is why I suggest you to do the second option below.
  2. Use \animategraphics instead of animateinline. \animategraphics allows you to import any PDF with one or more pages to be animated.

The theory is enough. Now the following will explain the second option (using \animategraphics). There are 2 steps as follows:


Solution

  1. In my opinion, it will be better if you cut the animation code from the host input file (presentation with beamer in your case ) and paste the animation code into a single, TeXable input file. TeX the animation input file to produce a PDF output. Later, you can make use of this PDF to create many other projects such as GIF animation, PDF animation (in your case), PNG images, etc. This separation method also make your host input file neater, cleaner, readable, understandable, etc.

    Let Simple.tex be the TeXable animation input file as follows:

    %Simple.tex  using standalone document class
    
    \documentclass[pstricks,border=0pt]{standalone}
    \usepackage{pstricks-add}
    
    \newcommand\RotatingArrow[1]{%
        \begin{pspicture}(-1,-1)(10,11)
            \psframe*[linecolor=cyan,opacity=0.3](-1,-1)(10,11)
            \psset{arrows=->}
            \psaxes(0,0)(-0.5,-0.5)(9,10)[$x$,0][$y$,90]
            \rput(4.5,0.6){\Huge{Temps = #1 s}}
            \rput{#1}(4.5,5.5){%
                \psset{arrowsize=5pt}
                \pnode(2,0){A}
                \pnode(2,3){B}
                \psline[linecolor=blue](A)
                \uput[0]{-#1}(A){\Huge{$\textcolor{blue}{\vec{a}}$}}
                \psline[linecolor=red](A)(B)
                \uput[90]{-#1}(B){\Huge{$\textcolor{red}{\vec{v}}$}}
            }
    \end{pspicture}} 
    
    \begin{document} 
        \multido{\i=0+10}{36}{\RotatingArrow{\i}}
    \end{document}
    

    For those who are unlucky to use standalone document class, the last resort is to use preview as follows. Note that standalone also uses preview internally.

    \documentclass{article}
    \usepackage{pstricks-add}
    
    
    \usepackage[active,tightpage]{preview}
    \PreviewBorder=0pt
    \PreviewEnvironment{pspicture}
    
    
    \newcommand\RotatingArrow[1]{%
        \begin{pspicture}(-1,-1)(10,11)
            \psframe*[linecolor=cyan,opacity=0.3](-1,-1)(10,11)
            \psset{arrows=->}
            \psaxes(0,0)(-0.5,-0.5)(9,10)[$x$,0][$y$,90]
            \rput(4.5,0.6){\Huge{Temps = #1 s}}
            \rput{#1}(4.5,5.5){%
                \psset{arrowsize=5pt}
                \pnode(2,0){A}
                \pnode(2,3){B}
                \psline[linecolor=blue](A)
                \uput[0]{-#1}(A){\Huge{$\textcolor{blue}{\vec{a}}$}}
                \psline[linecolor=red](A)(B)
                \uput[90]{-#1}(B){\Huge{$\textcolor{red}{\vec{v}}$}}}
    \end{pspicture}}
    
    \begin{document}
        \multido{\i=0+10}{36}{\RotatingArrow{\i}}
    \end{document}
    

    TeX it with either xelatex or latex->dvips->ps2pdf to get a PDF file. In our example, it will contain 36 pages to create a simple animation as follows.

    enter image description here

  2. Now, you create the beamer as follows.

    \documentclass{beamer}
    \usepackage{animate}
    
    \begin{document}
    \begin{frame}{Circular motion in action}
    \animategraphics[autoplay,loop,scale=0.5]{10}{Simple}{}{} % WARNING: the filename must not include its extension!
    \end{frame}
    \end{document}
    

    TeX it with either pdflatex (recommended) or xelatex. And the following image shows your presentation with a working animation.

The latest edit

We can also combine both input files into single input file as follows. Compile it with pdflatex --shell-escape.

\documentclass{beamer}
\usepackage{filecontents}

%====================== BEGIN FILE CONTENTS ==========================
\begin{filecontents*}{dummy.tex}
\documentclass{article}
\usepackage{pstricks-add}

\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt
\PreviewEnvironment{pspicture}

\newcommand\RotatingArrow[1]{%
    \begin{pspicture}(-1,-1)(10,11)
        \psframe*[linecolor=cyan,opacity=0.3](-1,-1)(10,11)
        \psset{arrows=->}
        \psaxes(0,0)(-0.5,-0.5)(9,10)[$x$,0][$y$,90]
        \rput(4.5,0.6){\Huge{Temps = #1 s}}
        \rput{#1}(4.5,5.5){%
            \psset{arrowsize=5pt}
            \pnode(2,0){A}
            \pnode(2,3){B}
            \psline[linecolor=blue](A)
            \uput[0]{-#1}(A){\Huge{$\textcolor{blue}{\vec{a}}$}}
            \psline[linecolor=red](A)(B)
            \uput[90]{-#1}(B){\Huge{$\textcolor{red}{\vec{v}}$}}}
\end{pspicture}}

\begin{document}
    \multido{\i=0+10}{36}{\RotatingArrow{\i}}
\end{document}
\end{filecontents*}
%====================== END FILE CONTENTS =========================

\usepackage{animate}
\begin{document}
\immediate\write18{latex dummy}
\immediate\write18{dvips dummy}
\immediate\write18{ps2pdf dummy.ps}
\begin{frame}{Circular motion in action}
    \animategraphics[controls,scale=0.5]{10}{dummy}{}{}
\end{frame}
\end{document}
Related Question