I have two nested \foreach
loops. The inner loop works fine, but the outer loop does not. There are two commands which should be executed during each iteration.
Both commands change during each execution of the nested loops. The command which identifies position works fine, but the command which uses a color that is assigned depending on the iteration of the outer loop does not.
The following is my original code and the output. Note that the foreground color red, assigned by
\ifthenelse{\a=2}{\def\mycolor{ red!20 }{}}
is omitted in the output:
\documentclass{article}
\usepackage{graphicx}
\usepackage[margin=0.75in, heightrounded]{geometry}
\usepackage{tikz}
\newcommand\mycolor{ red!15 }
\usepackage{ifthen}
\begin{document}
\begin{center}
\begin{tikzpicture}[x=1.0cm, y=1.0cm]
\def\z{0.25}
\def\u{4.5}
\def\v{7.5}
\foreach \a in {0, 1, 2} {
\foreach \y in {0, 1, 2, 3} {
% Outer most rectangle
\draw[black!80, fill=black!35, rounded corners=5ex] (\y*\u, 11.0+\v*\a) rectangle (4.2+\y*\u, 18.0+\v*\a);
% Inner most rectangle
\ifthenelse{\a=0}{\def\mycolor{ blue!20 }{}}
\ifthenelse{\a=1}{\def\mycolor{ green!20 }{}}
\ifthenelse{\a=2}{\def\mycolor{ red!20 }{}}
\draw[top color=white, bottom color=\mycolor, rounded corners=4ex] (\y*\u+1*\z, 11.0+1*\z+\v*\a) rectangle (4.2+\y*\u-1*\z, 18.0-1*\z+\v*\a);
}
}
\end{tikzpicture}
\end{center}
\end{document}
There are two independent modifications that I tried from a similar question: Why doesn't TikZ's \foreach iterate over the last element of the list?:
1) Using \foreach \a in {0, 1, 2.0001} {
gives the error Missing = inserted for \ifnum
, and identifies the line of the error as being on the closing }
for this \foreach
.
2) When I try
\foreach \aa in {0, 10, 20} {
\pgfmathsetmacro\a{0.1*\aa}
I get the same error: Missing = inserted for \ifnum
.
The question:Why does the iterator in foreach not work? is about parsing arithmetic in a \foreach
loop which is not my issue.
If I modify my original code by:
\ifthenelse{\a=0}{\def\mycolor{ blue!20 }{}}
\ifthenelse{\a=1}{\def\mycolor{ green!20 }{}}
\ifthenelse{\a=1}{\def\mycolor{ red!20 }{}}
then I get the correct output. But I shouldn't because \a=1
in the second and third lines should give red
not green
for both of the middle and upper rows.
Can someone offer some guidance towards the correct (not my \a=1
hack) solution, please?