3

I am trying to achieve some complicated animations with the animate and python packages. I can use the python environment within an animate environment for single frames, for instance:

\documentclass{letter}

\usepackage{animate}
\usepackage{python}

\begin{document}

Below changing numbers are displayed after clicking:

\begin{animateinline}[loop]{1}

\begin{python}
print("\LaTeX 0")
\end{python}

\newframe

\begin{python}
print("\LaTeX 1")
\end{python}

\newframe

\begin{python}
print("\LaTeX 2")
\end{python}

\end{animateinline}

\end{document}

works fine when compiled with pdflatex --shell-escape file.tex. However

\documentclass{letter}

\usepackage{animate}
\usepackage{python}

\begin{document}

Below changing numbers are displayed after clicking: 

\begin{animateinline}[loop]{1}
\multiframe{3}{i=0+1}{
\begin{python}
print("\LaTeX \i")
\end{python}
}
\end{animateinline}


\end{document}

where I have used the multiframe command to loop through \i, does not work.

If instead of using python I just type the text directly both options give the same result, which indicates the python environment is the cause of the issue.

Why is the python environment incompatible with multiframe and how can I fix it?

1 Answer 1

3

The python environment deals with verbatim content. Putting this sort of environment inside a command argument generally causes problems. A solution may exist, but it would probably be complex and involve a lot of catcode trickery.

Another option is to use the pythontex package, which I created to provide more functionality than the python package provides. pythontex provides a \pyc command that executes Python code inline. It usually works fine within command arguments, as long as # and % characters aren't needed. I've provided an example below.

Using pythontex involves a slightly different compile process. Whenever you have new/modified code that needs to be executed, run LaTeX, run pythontex.py, then run LaTeX again (more details in documentation). The animate package will give an error after the first LaTeX run, but will be fine after the second run. If there is no new/modified code, then just run LaTeX like normal; output is cached.

I assume that you will be using this to create more complex frames. In that case, you will probably want to create a Python function within a pycode environment, and then print the output of that function without the \multiframe, using \pyc. The example below also includes a demonstration of this.

\documentclass{letter}

\usepackage{animate}
\usepackage{pythontex}

\begin{document}

Below changing numbers are displayed after clicking: 

\begin{animateinline}[loop]{1}
\multiframe{3}{i=0+1}{
\pyc{print("\LaTeX \i")}
}
\end{animateinline}

\begin{pycode}
def my_func(x):
    return '\\textbf{' + x + '}'
\end{pycode}

\begin{animateinline}[loop]{1}
\multiframe{3}{i=0+1}{
\pyc{print(my_func("\LaTeX \i"))}
}
\end{animateinline}

\end{document}
1
  • Thanks. I actually saw there exists this pythontex package but I must say that needing a multi-step compilation put me off a little. I will look into this since it seems to provide a working alternative. Thanks again.
    – Miguel
    Nov 10, 2014 at 21:02

You must log in to answer this question.

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