4

I' m trying to make an animation with the following code

\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\section{Bilder}
\begin{frame}
\begin{tikzpicture}
[pedestrian/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\draw[step=1cm,gray,very thin]  (-6,0) grid (3,1) ;

\let\mylist\empty
\foreach \x[count=\i] in {1,...,5}{
\node<\i> [gray,above] at (-7,0.2) {$step=\i$};
\pgfmathtruncatemacro\y{6-\i}
\foreach \x in {1,...,\y}
\node<\i>  at  (-6.5+\x, 0.5) [pedestrian] {\tiny{\x,\i,\y|\mylist}};

\ifx\mylist\empty
\xdef\mylist{5};
\else
\foreach \z in \mylist{
\node<\i> at  (-2.5+\i , 0.5) [pedestrian] {\tiny{\z}};
 }    
\xdef\mylist{\y,\mylist};
\fi
}

\end{tikzpicture}
\end{frame}

\end{document}

Something is wrong with the variable z which is alway 5. I checked the values of mylist, which keeps growing, but still... z is always 5 (the last element of the list).

Here is the result that I was expecting: PDF-FILE

I got it by doing some stupid copy-paste

\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgffor}
\begin{document}
\section{Bilder}
\begin{frame}
\begin{tikzpicture}
[pedestrian/.style={circle,draw=blue!50,fill=blue!20,thick},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\draw[step=1cm,gray,very thin]  (-6,0) grid (3,1) ;

\uncover<1>{
\node [gray,above] at (-7,0.2) {$step=0$};
\foreach \x in {1,...,5}
\node  at  (-6.5+\x + 0, 0.5) [pedestrian] {\tiny{\x}};
}

\uncover<2>{
\node [gray,above] at (-7,0.2) {$i=5$};
\foreach \x in {1,...,4}
\node  at  (-6.5+\x + 0, 0.5) [pedestrian] {\tiny{\x}};

\node  at  (-6.5+6 + 0, 0.5) [pedestrian] {\tiny{5}};
}

\uncover<3>{
\node [gray,above] at (-7,0.2) {$i=4$};
\foreach \x in {1,...,3}
\node  at  (-6.5+\x + 0, 0.5) [pedestrian] {\tiny{\x}};

\foreach \x in {4,...,5}
\node  at  (-6.5+1 + \x, 0.5) [pedestrian] {\tiny{\x}};
}
\uncover<4>{
\node [gray,above] at (-7,0.2) {$i=3$};
\foreach \x in {1,...,2}
\node  at  (-6.5+\x + 0, 0.5) [pedestrian] {\tiny{\x}};

\foreach \x in {3,...,5}
\node  at  (-6.5+1 + \x, 0.5) [pedestrian] {\tiny{\x}};

}
\uncover<5>{
\node [gray,above] at (-7,0.2) {$i=2$};

\foreach \x in {2,...,5}
\node  at  (-6.5+1 + \x, 0.5) [pedestrian] {\tiny{\x}};

\node  at  (-6.5 + 1, 0.5) [pedestrian] {\tiny{1}};
}

\uncover<6>{
\node [gray,above] at (-7,0.2) {$i = 1$};

\foreach \x in {1,...,5}
\node  at  (-6.5+1 + \x, 0.5) [pedestrian] {\tiny{\x}};

}

\end{tikzpicture}
\end{frame}

\end{document}
9
  • 1
    Please complete you code to make it compilable. We want an example we can copy-paste-compile to see just the problem you are seeing. That makes it easier to understand and address your question. – cfr Nov 18 '14 at 1:21
  • It looks like you have \let\mylist\empty, and then you check if it is \empty which executes \xdef\mylist{5}. Plus you seem to be missing a { after the \foreach. Without that only the \node is executed, and the \xdef\mylist{\y,\mylist} only gets exectuted after the \foreach is complete. – Peter Grill Nov 18 '14 at 1:25
  • Yes but that happens only once. After that we are in the else. Only the node should be in foreach. – Tengis Nov 18 '14 at 1:28
  • You are reusing the foreach variable \x twice and at the end not expanding properly. One option \expandafter\gdef\expandafter\mylist\expandafter{\mylist,\y}% – percusse Nov 18 '14 at 1:40
  • does not help. The problem persists even if i loop over something like {1,2,3}. At the end it will be just 3 – Tengis Nov 18 '14 at 1:52
2

You example works but you are hit by beamer syntax <i>

Consider the line change on your last foreach loop

\node[opacity=0.2] at  (-2.5+\i , 0.5) [pedestrian] {\tiny{\z}};% It was <i> HERE

Then we see that the loop is run correctly but because all have the same slide specification <i> they got overprinted by the last element which happens to be 5.

enter image description here

Now we can see that the content is there. You can either try to fix this or you can use a more compact version such as the following (which you can even further code golf it)

\documentclass[presentation]{beamer}
\usepackage{tikz}
\begin{document}
\section{Bilder}
\begin{frame}
\begin{tikzpicture}
[pedestrian/.style={circle,draw=blue!50,fill=blue!20,thick,font=\tiny},
transition/.style={rectangle,draw=black!50,fill=black!20,thick}]
\draw[step=1cm,gray,very thin]  (-6,0) grid (3,1) ;

\foreach \x[count=\xi] in {5,...,0}{
  \foreach \y[count=\yi from 0] in {1,...,5}{
    \ifnum\yi<\x\relax
      \node<\xi>[pedestrian]  at  (-6.5+\y, 0.5)  {\y};
    \else
      \node<\xi>[pedestrian]  at  (-6.5+\y+1, 0.5)  {\y};
    \fi
    }
}
\end{tikzpicture}
\end{frame}
\end{document}
2
  • you example works fine. It is even better. That was my first try with tikz (so frustrating) when you don't know the basics of that programming language (types, expressions, ..). I could not find a manual explaining in details how tikz as a programming language works. Could you please explain why <i> influences the result "visible" part of the list. It is always the last element – Tengis Nov 18 '14 at 10:49
  • @Tengis <i> is a beamer syntax but TikZ understands it. It means show this item only on slide i. But your last loop runs always on the same spin of \i So all nodes see the same slide number and they get overprinted. Because i is only updated per the outer most spin. Maybe you would have used \z to move the position of the nodes. Is that a bit clearer? – percusse Nov 18 '14 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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