How to include a PSTricks animation into a beamer?

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{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).

-

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}

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

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[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}

-
As for 1(b), you could also improve readability of the host document source by moving the animateinline code into an external file and \input{...} it where needed. – AlexG Jul 27 '12 at 11:26
But that is problem 1(a) ;) – AlexG Jul 27 '12 at 11:32
As for compilation time, there is still the draft option available for intermediate compilations. – AlexG Jul 27 '12 at 11:36
Thank your for your answer. Works beautifully. – Mathusalem Jul 27 '12 at 11:48
I did click the arrow, but don't have sufficient reputation to upvote. Seems I spoke too fast about it working beautifully. If I compile Simple.tex with Xelatex, it will spit out 36 images beside one another, and not 36 different pages.I tried putting a \newpage at the end of the newcommand, but it will just do the same (1 page) vertically. When compiling it gives me a warning : LaTeX Warning : Unused global option(s): [pstricks]. – Mathusalem Jul 27 '12 at 12:09

The verbatim code in the animate documentation are minimal working examples that can be compiled as they are. You cannot embed a pdf with an animation into another document. Just insert the \begin{animateinline}...\end{animateinline} stuff into your beamer document source.

-
Thank you for your answer. Inserting the animate code directly into the beamer works fine. However, when doing that the following problem comes up : \begin{tabular}{p{0.5\textwidth}p{0.5\texwidth}} Some text.... & \begin{animateinline}....\end{animateinline} \end{tabular} The text will be below the bottom of the animation window, as opposed to simply next to it. What is the problem here ? – Mathusalem Jul 27 '12 at 8:45
@Mathusalem: The animation box behaves exactly like an embedded graphics, that is, its base line is aligned with the base line of the surrounding text. Maybe the animation box is too large to fit into the table cell? Could you please edit your (compilable) example code above to show the problem? – AlexG Jul 27 '12 at 8:58
Why does \animategraphics produce an error when the file extension is explicitly specified? – kiss my armpit Jul 29 '12 at 8:37
@HiggsBoson In case of a numbered sequence, the file number would sit between the base name, shared by all files, and the extension. Maybe I'll add an option to enforce a specific file extension. – AlexG Jul 30 '12 at 6:47