7

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}}};
 \addtocounter{tmpA}{1}
}
\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}}};
 \addtocounter{tmpA}{1}
}
\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}
0

1 Answer 1

7
+50

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .