TikZ:在所有其他foreach步骤之间重复某个步骤

TikZ:在所有其他foreach步骤之间重复某个步骤

我有一个(长)列表,比如说\def\List{0, 4, 7, 11, 2, 3}
我用 foreach 循环绘制一个动画(这只是一个非常大的列表中非常小的一部分tikzpicture):

在此处输入图片描述

但我想在每个数字后重复“0”的图片(即黑色填充的图片)。就像列表看起来的那样0, 4, 0, 7, 0, 11, 0, 2, 3, 0

有没有什么技巧可以实现这个目的?

\documentclass[margin=5mm, tikz]{standalone} 
\usepackage{tikz}

\def\List{0, 4, 7, 11, 2, 3}

\begin{document}
\foreach \No in \List {%%
\begin{tikzpicture}[]
\pgfmathsetmacro\Fill{\No==0 ? "black" : "none"}
\node[draw, minimum size=10mm, name=ListEntry, fill=\Fill]{\No};
\node[anchor=west] at (ListEntry.east){Situation: \No};
\end{tikzpicture}
}%%
\end{document}

答案1

您可以在循环内放置第二个 tikzpicture:

\documentclass[margin=5mm, tikz]{standalone} 
\usepackage{tikz}

\def\List{4, 7, 11, 2, 3}

\begin{document}
\foreach \No in \List {%%
\begin{tikzpicture}[]
\node[draw, minimum size=10mm, name=ListEntry, fill=black]{0};
\node[anchor=west] at (ListEntry.east){Situation: 0};
\end{tikzpicture}
\begin{tikzpicture}[]
\node[draw, minimum size=10mm, name=ListEntry]{\No};
\node[anchor=west] at (ListEntry.east){Situation: \No};
\end{tikzpicture}
}%%
\end{document}

或者,您可以添加嵌套循环:

\documentclass[margin=5mm, tikz]{standalone} 
\usepackage{tikz}

\def\List{4, 7, 11, 2, 3}

\begin{document}
\foreach \No in \List {%%
\foreach \B[evaluate=\B as \k using {int(\No*\B)}] in {0,1}{%%
\begin{tikzpicture}[]
\pgfmathsetmacro\Fill{\B==0 ? "black" : "none"}
\node[draw, minimum size=10mm, name=ListEntry, fill=\Fill]{\k};
\node[anchor=west] at (ListEntry.east){Situation: \k};
\end{tikzpicture}
}}%%
\end{document}

相关内容