2

I'm used to prepare tikz graphics with preview or standalone packages to crop them. Now I've started to experiment with animations and wanted to do the same. My first test was with Alexander Grahn's answer to Animating Protocols, but as soon as I include

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

in its preamble, pdflatex complains with

! Package animate Error: Contents of first frame must not have zero width.

I've found some examples prepared with preview but all of them use \animategraphics command instead of animateinline environment with \multiframe command.

So my first question is, has any sense to do what I want? It is producing an animated clipped graphic to be included in another file. "No, it hasn't" can be a good answer but if it has, what am I doing wrong?

Here you have the code to test it. I've also tested adding a \usebaoundingbox but without any succes.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{animate}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\begin{document}
\begin{frame}
\begin{animateinline}[loop]{10}
  \multiframe{10}{rPos=0.1+0.1}{
    \begin{tikzpicture}[every node/.style={draw}]
     %\useasboundingbox (-2,-2) rectangle (7,2);
     \node (s) {sender};
     \node (r) at (5,0) {receiver};
     \path (s) -- (r) node[pos=\rPos,coordinate] (p) {};
     \draw[->] (s) -- (p);
    \end{tikzpicture}
  }
\end{animateinline}
\end{frame}

\end{document}
Ignasi
  • 136,588
  • Did you tried it with a normal document class? IIRC beamer doesn't work nicely with preview as well. – Martin Scharrer Aug 05 '11 at 09:59
  • @Ignasi: As Martin said in his answer and his comment to KF Leong, there isn't much point in using preview with animate other than getting a tightly cropped PDF around the animation. You cannot \includegraphics or \includepdf it into another document, because the PDF annotations, which provide interactivity, get lost in this process. They have to be re-built in every new PDF document. Therefore standalone is probably the better choice. – AlexG Aug 05 '11 at 12:00
  • @Alexander: I tested preview with animate because I use it with tikz graphics but now I'll try with standalone. By the way, including a newframe (with a tikzpicture) before the multiframe it's also a possible workarround. – Ignasi Aug 08 '11 at 09:39

2 Answers2

3

Change the \PreviewEnvironment option to animateinline.

  • 1
    Indeed, changing \PreviewEnvironment{tikzpicture} to \PreviewEnvironment{animateinline} does it. It also works with a different class as beamer, but you will get other fonts by default. Note that the animation will be lost if you include that PDF as image in another document. – Martin Scharrer Aug 05 '11 at 11:00
3

Here a solution which uses the standalone class. As said, animations will be lost if you include that PDF as image in another document. However, you could just \input the file below after loading the standalone package in the main document.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\begin{document}
\begin{animateinline}[loop]{10}
  \multiframe{10}{rPos=0.1+0.1}{
    \begin{tikzpicture}[every node/.style={draw}]
     %\useasboundingbox (-2,-2) rectangle (7,2);
     \node (s) {sender};
     \node (r) at (5,0) {receiver};
     \path (s) -- (r) node[pos=\rPos,coordinate] (p) {};
     \draw[->] (s) -- (p);
    \end{tikzpicture}
  }
\end{animateinline}
\end{document}
Martin Scharrer
  • 262,582
  • 1
    Thanks, @Martin, for the pointer to the standalone class and package (+1). It allows for combining self-contained documents into a main one without (re)moving the preambles, right? – AlexG Aug 05 '11 at 11:47
  • @Alex: Yes, exactly. It can also copy the sub-preambles to the main one if the subpreambles option is used with the package. This requires one additional compiler run. – Martin Scharrer Aug 05 '11 at 12:24
  • Neither xelatex nor latex-dvips-ps2pdf can compile it. ps2pdf produced error message: `Error: /undefined in pgfo Operand stack:

    Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1910 1 3 %oparray_pop 1909 1 3 %oparray_pop 1893` to be continued...

    – Display Name Aug 06 '11 at 03:49
  • This code also does not work with PSTricks and animate. – Display Name Aug 07 '11 at 12:06
  • @xport: Try the new v1.0 of standalone which is much better for XeLaTeX. – Martin Scharrer Dec 21 '11 at 16:22
  • Compiling your code with either xelatex or pdflatex and then compressing the resulting PDF with ghostscript (gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf) will make the animation failed. – kiss my armpit Mar 06 '12 at 10:28
  • @DamienWalters: Looks like Ghostscript removes the animation part. I don't think you can do much about that from the LaTeX side. – Martin Scharrer Mar 06 '12 at 10:43
  • latex followed by dvips followed by gswin64c -r10000 -dCompatibilityLevel=1.5 -dAutoRotatePages=/None -dPDFSETTINGS=/prepress -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf input.ps does not need compression and the animation still works as expected. – kiss my armpit Mar 06 '12 at 11:06
  • @DamienWalters: Sorry, is this meant as information only, or are you asking me for help? – Martin Scharrer Mar 06 '12 at 11:08
  • It is just for information that animation no longer works when I compile your code with either xelatex or pdflatex followed by a compression with ghostscript. But it is not the case when I compile with a combo sequence latex-dvips-ghostscript. Thanks. – kiss my armpit Mar 06 '12 at 11:18