1

I want to uncover/draw a picture bit by bit for beamer slides. I imagined that using \only inside a \foreach loop should work, for instance:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
  \begin{frame}
    \begin{tikzpicture}[every node/.style={draw,circle}]
      \node (n0) {$0$};
      \foreach \i [evaluate=\i as \j using \i-1] in {1,...,3} {%
        \only<\i->{%
          \node[right of=n\j] (n\i) {$\i$};
        }
      }
    \end{tikzpicture}
  \end{frame}
\end{document}

Unfortunately, two problems occur.

  • Something goes wrong with node names; I get errors of the form

    Package pgf Error: No shape named n0.0 is known.
    
  • When using \j in the parameter of \only, I get hundreds of errors and pages.

What is going on here?

Raphael
  • 5,983
  • While I managed to solve the issue at hand (by collecting stuff from other tex.SE posts) I'd appreciate answers that shed more light on the situation. Is this a bug, or does evaluate work as intended and I used it wrong? – Raphael Oct 21 '13 at 09:50
  • related: http://tex.stackexchange.com/a/131892/21891 – jub0bs Oct 21 '13 at 09:54
  • 1
    You would have solved your issue with \foreach \i [evaluate=\i as \j using int(\i-1)]; see percusse's comment. – Claudio Fiandrino Oct 21 '13 at 09:55
  • @ClaudioFiandrino Ah, that's way more elegant of course. Would you mind adding this as an answer? – Raphael Oct 21 '13 at 10:01
  • related: http://tex.stackexchange.com/questions/99701/continuing-a-tikz-animation-with-onslide-after-a-foreach-loop – jub0bs Oct 21 '13 at 10:09
  • @Raphael: as you already provided an answer, you might split it into two parts: one with your previous method and on with the one you just learned. :) – Claudio Fiandrino Oct 21 '13 at 10:10
  • 1
  • This has already been discussed here just not for beamer overlays. Possible duplicate: How to decrease the counter in tikz foreach loop – Qrrbrbirlbel Oct 21 '13 at 13:01
  • @Qrrbrbirlbel While some of the solutions carry over, the problem is not the same (to the user). An important part of this question is that using evaluate variables with only, bad stuff happens. I think that should be searchable. – Raphael Oct 21 '13 at 13:16
  • It isn’t? I’d say the problem is the same, the symptom is not. You’re using <0.0->, <1.0-> and so on for the overlays. Using int, count or remember (with initially here) works for me for the overlays, too. Either way, it is linked now and can be found. :) By the way, you can also say \node<\i-1> …. – Qrrbrbirlbel Oct 21 '13 at 13:23
  • @Qrrbrbirlbel Call it symptom, if you will; the fact remains, somebody experiencing the symptom has a hard time finding a solution. I know I did (you may check this search or similar; many results lead to unrelated questions) and that's why I added this question. (If you consider only underlying problems, most questions likely are duplicates, aren't they? ;)) – Raphael Oct 21 '13 at 13:30
  • @Qrrbrbirlbel Thanks about the note regarding positioning. That's what happens if there's lots of old stuff on the net, I guess. :/ – Raphael Oct 21 '13 at 13:34
  • I hope that this question will not be deleted because it's closed. The linked question does not provide a good search target for people that experience the symptoms I did. – Raphael Oct 22 '13 at 13:26

1 Answers1

1

Apparently, evaluate does not produce integers but floats (the error message hints thus). Using

\def\j{\the\numexpr\i-1\relax}

instead (inside the loop, of course) computes \j in the desired way and solves both problems.

PGF has some macros that make this nicer;

\pgfmathtruncatemacro{\j}{\i-1}

has the same effect.

The most elegant way, however, has been proposed by Claudio Fiandrino:

\foreach \i [evaluate=\i as \j using int(\i-1)]
Raphael
  • 5,983