# Strange error when combining Beamer, Animate, Tikz and Listings

I'm working on some slides where I show how code is actually executed in Java. Therefore I'm working with the animate package to generate animations.

Without going into details, I've written a macro to help me generate an image for the animation. These images are generated by Tikz:

\newcommand{\methodexec}[4]{\begin{tikzpicture}[yscale=-1]
\draw (0,0) rectangle (10,5);
\begin{scope}[xshift=0cm,yshift=0cm]
\node[anchor=north west] (C) at (0,0) {\begin{minipage}{3in}\lstinputlisting[linebackgroundcolor={\ifthenelse{\value{lstnumber}=#2}{\color{green}}{}}]{programs/#1}\end{minipage}};
\end{scope}
\begin{scope}[yscale=-1,yshift=-5 cm]
\setcounter{tmpA}{1}
\foreach \n in {#3} {
\coordinate (X) at (8.5,0.5*\arabic{tmpA});
\node[pointerbox] (box\n) at (X) {};
\node[pointerdot] (ptr\n) at (X) {};
\node[anchor=west] (\n) at (box\n.east) {\small{\texttt{\n}}};
}
\end{scope}
\begin{scope}[xshift=0.25,yshift=2.5 cm]
#4
\end{scope}
\end{tikzpicture}}

An example of this macro is \methodexec{code.java}{1}{}{}

This all works fine except when I integrate these pictures into animate:

\begin{animateinline}{1}
\methodexec{code.java}{1}{}{}\newframe
\methodexec{code.java}{2}{}{}
\end{animateinline}

The compiler compiles the images correcly but reports some errors:

<a0,fr0> (./programs/code.java) ! Argument of \l@lstlisting has an extra }. <inserted text> \par l.11 }\newframe Runaway argument? ! Paragraph ended before \l@lstlisting was complete. <to be read again> \par l.11 }\newframe

Of course you can argue that I could ignore the errors. However I assume that there is something wrong with my code and furthermore Kile has the annoying behavior to open the .vrb file of the frame when an error is reported (I compile such frames in fragile mode).

EDIT: as MWE:

in test.tex:

\documentclass[handout]{beamer}
\usepackage{tikz,listings,lstlinebgrd}
\usepackage[controls]{animate}
\usetikzlibrary{calc,shapes,fit}
\lstset{language=Java,basicstyle=\tiny\ttfamily,breaklines=true}
\tikzset{pointer/.style={->,thick,black},pointerdot/.style={fill=black,circle,inner sep=0pt,minimum size=0.125cm},pointerbox/.style={draw=black,rectangle,minimum size=0.25 cm}}
\newcounter{tmpA}
\newcommand{\methodexec}[4]{\begin{tikzpicture}[yscale=-1]
\draw (0,0) rectangle (10,5);
\begin{scope}[xshift=0cm,yshift=0cm]
\node[anchor=north west] (C) at (0,0) {\begin{minipage}{3in}\lstinputlisting[linebackgroundcolor={\ifthenelse{\value{lstnumber}=#2}{\color{green}}{}}]{programs/#1}\end{minipage}};
\end{scope}
\begin{scope}[yscale=-1,yshift=-5 cm]
\setcounter{tmpA}{1}
\foreach \n in {#3} {
\coordinate (X) at (8.5,0.5*\arabic{tmpA});
\node[pointerbox] (box\n) at (X) {};
\node[pointerdot] (ptr\n) at (X) {};
\node[anchor=west] (\n) at (box\n.east) {\small{\texttt{\n}}};
}
\end{scope}
\begin{scope}[xshift=0.25,yshift=2.5 cm]
#4
\end{scope}
\end{tikzpicture}}
\begin{document}
\begin{frame}[fragile]{Some title}
\begin{animateinline}{1}
\methodexec{code.java}{1}{}{}\newframe
\methodexec{code.java}{2}{}{}\newframe
\methodexec{code.java}{3}{}{}
\end{animateinline}
\end{frame}
\end{document}

And code.java (in the programs directory):

foo
bar
baz

Any ideas? A workaround (like for instance a variant of the listings package) is helpful as well.

EDIT2: It clearly has something to do with the listings package, because using minted solved the problem. But that doesn't explain why listings actually produces this error.

John Wickerson adds: I made a more minimal MWE, which rules out TikZ as the culprit. Beamer, Animate and Listings all remain suspects.

\documentclass{beamer}
\usepackage{listings}
\usepackage{animate}
\usepackage{filecontents}
\begin{filecontents*}{code.java}
foo
bar
baz
\end{filecontents*}
\begin{document}
\begin{frame}[fragile]{Some title}
\begin{animateinline}{1}
\lstinputlisting{code.java}
\end{animateinline}
\end{frame}
\end{document}
-
A MWE would’ve been nice. You probably need to use the fragile option for the frames, though. –  Qrrbrbirlbel Mar 30 '13 at 21:07
A member for 2 years, 3 months should get accustomed to provide MWE. –  kiss my armpit Mar 30 '13 at 21:10
I'm using the fragile mode already. I'm working at a MWE at the moment. –  CommuSoft Mar 30 '13 at 21:11
I think that's about the most minimal working example I can generate. Better? –  CommuSoft Mar 30 '13 at 21:18

This error only occurs if the document is typeset with pdflatex or lualatex, while xelatex, latex+dvips+ps2pdf work smoothly.

animate puts the animation frame content first into a box, using the LaTeX \savebox command. The filled box is then distilled into a PDF Form XObject using the command \pdfxform from pdfTeX. The last step fails, if the box contains listings-related material.

A minimal test case for pdftex and lualatex, without animate involved, is

\documentclass{beamer}
\usepackage{listings}

\usepackage{filecontents}
\begin{filecontents*}{code.java}
foo
bar
baz
\end{filecontents*}

\newsavebox\listing

\begin{document}
\begin{frame}[fragile]{Some title}

\savebox\listing{\lstinputlisting{code.java}}
\immediate\pdfxform\listing%

\end{frame}
\end{document}

Other engines don't use \pdfxform and therefore don't display this error.

As a workaround, I suggest to produce a separate PDF with the animation frames on individual pages, using the preview or standalone packages. This multipage PDF can then be animated using the \animategraphics command.

-