# Why do the following nested loops spin beyond my expectation?

Let's consider the following code that reflects my simplified scenario. I want to show a distinct integer for each slides. Of course the real scenario is not to do such a silly thing.

\documentclass{beamer}
\usepackage{pgffor}

\begin{document}
\begin{frame}
\foreach \x in {0,1,2}{%
\foreach \y in {0,1}{%
\only<\numexpr2*\x+\y+1\relax>{\numexpr2*\x+\y+1\relax}}}
\end{frame}
\end{document}


My question is why do nested loops spin beyond my expectation? Theoretically there should be 6 slides in total.

• Problems often come from not knowing what I am doing. – kiss my armpit Sep 23 '13 at 13:53

You are missing \the

\documentclass{beamer}
\usepackage{pgffor}

\begin{document}
\begin{frame}
\foreach \x in {0,1,2}{%
\foreach \y in {0,1}{%
\only<\the\numexpr2*\x+\y+1\relax>{\the\numexpr2*\x+\y+1\relax}}}
\end{frame}
\end{document}


\numexpr is like an anonymous count register. For the part inside the {} you need \the (or \number or \romannumeral etc) to generate text form just as you would had it been \count@. For the one inside <> What it does do is fall over the + looking for the beamer <+-> syntax. so you need to expand that away as soon as possible.

• The literal 3 should be changed to 2. I have made an illogical reasoning in the original question (before I edited). – kiss my armpit Sep 23 '13 at 13:41
• @PGFTricks I was going to mention you get more than 6 slides with that formula:-) (done) – David Carlisle Sep 23 '13 at 13:44