# What I Want

I'm creating a presentation with pdflatex and the beamer documentclass.

In an external program I created a stepwise graphic. The graphic is one PDF file where each page in that PDF is a single step.

I want to show the steps of this graphics one at a time. The graphic for each step should replace the graphic of the previous step. This is essentially what \multiinclude from xmpmulti.sty does, except that all steps are taken from a single PDF file.

I did so with the following tex code:

\begin{frame}
Content before
\pause

\begin{overprint}
\onslide<+|handout:0>\includegraphics[page=1]{img}
\onslide<+|handout:0>\includegraphics[page=2]{img}
\onslide<+-|handout:1>\includegraphics[page=3]{img}
\end{overprint}
\onslide<+->

Content after
\end{frame}


As I want to include many such stepwise graphics I wanted to automate this process with a for loop and later with a command. However, I cannot seem to get it right.

# What I Have

At the moment I have the following

\nonstopmode
\pdfsuppresswarningpagegroup=1

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}

\begin{document}
\begin{frame}
\pdfximage{img.pdf}
\newcounter{laststep}
\setcounter{laststep}{\pdflastximagepages}
\begin{overprint}
\foreach \x in {1,...,\value{laststep}} {
%\onslide<+|handout:0>
\includegraphics[page=\x]{img}
}
\end{overprint}
\end{frame}
\end{document}


To make this example self contained: Here is the output of base64 img.pdf. Copy-paste this into base64 -d > img.pdf to the get the file or use an online service.

The code as shown compiles and includes all steps of img.pdf at the same time, but when I uncomment \onslide<+|handout:0> I get the following error over and over again. pdflatex runs endlessly.

! Missing number, treated as zero.
p
l.19 \end{frame}


I spent several hours trying to automate inclusion of these stepwise graphics. In other attempts I also used other loop macros such as forloop but nothing worked so far. This is my best attempt so far.

# Question

How can I include a stepwise graphic from a single PDF? Either by fixing the broken \foreach or by using a different approach or even existing solution in form of a package.

• Try replacing the loop by \newcounter{iloop} \setcounter{iloop}{0} \loop\stepcounter{iloop} \onslide<+|handout:0> \includegraphics[page=\number\value{iloop}]{img} \ifnum\value{iloop}<\value{laststep}\repeat. BTW, your question is very similar to tex.stackexchange.com/q/445916. – Schrödinger's cat Nov 9 '19 at 16:55
• The suggested loop resulted in the errors 2×Command \c@laststep already defined and 2×Command \c@iloop already defined. – Socowi Nov 9 '19 at 17:00
• My bad, \newcounter{iloop}  should not be inside the frame, and the same statement applies to \newcounter{laststep}. – Schrödinger's cat Nov 9 '19 at 17:08
• Sorry – I found no good file hoster to share img.pdf, so you have to coope with the base64 representation. But the content is irrelevant. You can take any pdf you have lying around. – Socowi Nov 9 '19 at 17:09
• Yes, I figured that. ;-) The loop does work if one does the \newcounters before the frame. – Schrödinger's cat Nov 9 '19 at 17:09

This works for me (I created my own img.pdf with different contents).

\nonstopmode
\pdfsuppresswarningpagegroup=1

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\newcounter{laststep}
\newcounter{iloop}

\begin{document}
\begin{frame}
\pdfximage{img.pdf}
\setcounter{laststep}{\pdflastximagepages}
\begin{overprint}
\setcounter{iloop}{0}
\loop\stepcounter{iloop}
\onslide<+|handout:0>
\includegraphics[page=\number\value{iloop}]{img}
\ifnum\value{iloop}<\value{laststep}\repeat
\end{overprint}
\end{frame}
\end{document}


• Thank you. Moving the counter declaration worked for your loop. Pfg's \foreach still failed, but that doesn't matter – I'm happy to have one dependency less. ¶ Do you know why declaring a counter inside frame fails? – Socowi Nov 9 '19 at 18:23
• @Socowi Yes. If you use \pause or something in a frame, this means that the code gets executed more than once. Therefore \newcounter gets executed more than once, which triggers an error. – Schrödinger's cat Nov 9 '19 at 18:29
• Ah, makes sense. Thank you for all the help! – Socowi Nov 9 '19 at 18:30

Thanks to the user Schrödinger's cat I was able to complete my command. I ran into another small problem, where the next \pause after my command would pause twice, but I fixed it by modifying the counter beamerpauses.

The final command is:

\nonstopmode
\pdfsuppresswarningpagegroup=1
\documentclass{beamer}
\usepackage[utf8]{inputenc}
\newcounter{laststep}
\newcounter{step}
\newcommand{\stepwisegraphics}[1]{%
\pdfximage{#1.pdf}
\setcounter{laststep}{\pdflastximagepages}
\setcounter{step}{1}
\begin{overprint}
\loop
\onslide<+|handout:0>
\includegraphics[page=\thestep]{#1}
\stepcounter{step}\ifnum\value{step}<\value{laststep}\repeat
\addtocounter{beamerpauses}{-1} % workaround mystic double pause
\pause
\includegraphics[page=\thestep]{#1}
\end{overprint}
}
\begin{document}
\begin{frame}
top\par
\pause \stepwisegraphics{img}
\pause middle 1\par
\pause middle 2\par
\pause \stepwisegraphics{img}
\pause bottom
\end{frame}
\end{document}


• Maybe \resetcounteronoverlays{step} also works? (I didn't try.) (+1) – Schrödinger's cat Nov 9 '19 at 18:24
• I don't think so. The documentation of \resetcounteronoverlays states »After you have invoked this command, the value of the specified counter will be the same on all slides of every frame.«. However, beamerpauses must change, or else I wouldn't get a step-by-step presentation. – Socowi Nov 9 '19 at 18:27
• Yes, you are right. – Schrödinger's cat Nov 9 '19 at 18:29