2

I have a (long) list, say \def\List{0, 4, 7, 11, 2, 3}.
I draw an animation with a foreach-loopfor that (this is a very small part of a very large tikzpicture):

enter image description here

But I would like to repeat the picture for "0" (that is the black filled one) after each number. Like as the list would look like 0, 4, 0, 7, 0, 11, 0, 2, 3, 0.

Is there maybe a trick to get that?

\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}
2
  • 1
    Use a second, inner loop. Fill it {\No, 0} .
    – MS-SPO
    Commented Jun 6, 2023 at 11:19
  • 1
    Ah, yes. I think that's mostly elegant.
    – cis
    Commented Jun 6, 2023 at 11:21

1 Answer 1

4

You can put a second tikzpicture inside the loop:

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

Alternatively you can add a nested loop:

\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}
2
  • Thx. But I think a second counter would be better, so I do not have to repeat the (long) tikzpicture-code. ;)
    – cis
    Commented Jun 6, 2023 at 11:28
  • 2
    @cis: I updated my answer.
    – Sandy G
    Commented Jun 6, 2023 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .