tikz foreeach 列表是空的吗?

tikz foreeach 列表是空的吗?

我正在尝试使用以下代码制作动画

\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}

变量有问题z,它始终为 5。我检查了的值mylist,它一直在增长,但仍然……z始终为 5(列表的最后一个元素)。

这是我期望的结果:PDF文件

我通过一些愚蠢的复制粘贴得到了它

\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}

答案1

你的例子有效,但是你被beamer语法所困扰<i>

foreach考虑最后一次循环中的线路变化

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

然后我们看到循环运行正确,但由于所有循环都具有相同的滑动规格,因此<i>它们被最后一个元素(恰好是 5)叠印。

在此处输入图片描述

现在我们可以看到内容了。您可以尝试修复此问题,也可以使用更紧凑的版本,例如以下版本(您甚至可以进一步对其进行编码)

\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}

相关内容