# Continuing a TikZ animation with \onslide after a \foreach loop

I want to produce an animation in a tikzpicture environment in Beamer. The animation consists in a series of slides produced by combining \foreach and \onslide, which works fine. I then want to continue my animation with a few more slides produced by \onslide but not within the \foreach command. But the compilation gets stuck for some reason.

\documentclass{beamer}

\usepackage{tikz}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\pgfmathsetmacro{\N}{3}
\foreach \n in {1,2,...,\N}{
\onslide<\n>{
\draw (0,0) node {node on slide \n};
}
}
\onslide<\N+1>{
\draw (0,0) node {node on slide {\N+1}};
}
\end{tikzpicture}
\end{frame}
\end{document}


The problem stems from \onslide<\N+1>. Substituting \onslide<4> for that works fine. For maintainability reasons, though, I don't want to hardcode the slide number. In order to solve the problem, I've tried to surround \N+1 with braces:

\onslide<{\N+1}>

to no avail; the following code also doesn't work:

\documentclass{beamer}

\usepackage{tikz}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\pgfmathsetmacro{\N}{3}
\foreach \n in {1,2,...,\N}{
\onslide<\n>{
\draw (0,0) node {node on slide \n};
}
}
\pgfmathsetmacro{\n}{\N+1}
\onslide<\n>{
\draw (0,0) node {node on slide {\N+1}};
}
\end{tikzpicture}
\end{frame}
\end{document}


Any ideas?

-

The problem is that \pgfmathsetmacro inserts a decimal so that \N is defined to be 3.0 and that wreaks havoc in the inital frame content readout. If you want to define integers, either \define it or use \pgfmathtruncatemacro.

\documentclass{beamer}

\usepackage{tikz}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\pgfmathtruncatemacro{\N}{3}
\foreach \n in {1,2,...,\N}{
\onslide<\n>{
\draw (0,0) node {node on slide \n};
}
}
\pgfmathparse{int(\N+1)}
\onslide<\pgfmathresult>{
\draw (0,0) node {node on slide {\number\numexpr\N+1\relax}};
}
\end{tikzpicture}
\end{frame}
\end{document}

-
Wonderful! Thanks so much, percuße. – Jubobs Feb 24 '13 at 12:02
@Jubobs My pleasure. – percusse Feb 24 '13 at 12:08