Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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}
share|improve this answer
    
Wonderful! Thanks so much, percuße. –  Jubobs Feb 24 '13 at 12:02
    
@Jubobs My pleasure. –  percusse Feb 24 '13 at 12:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.