2

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}

gives enter image description here

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. enter image description here

Can someone offer some guidance towards the correct (not my \a=1 hack) solution, please?

3

You have wrong syntax for \ifthenelse.

\ifthenelse{\a=0}{\def\mycolor{blue!20}}{}
\ifthenelse{\a=1}{\def\mycolor{green!20}}{}
\ifthenelse{\a=2}{\def\mycolor{red!20}}{}

The syntax is

\ifthenelse{<test>}{<true>}{<false>}

and your code messed everything up.

You can avoid all the burden, though:

\documentclass{article}

\usepackage{graphicx}
\usepackage[margin=0.75in, heightrounded]{geometry}
\usepackage{tikz}

\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/\b in {0/blue!20, 1/green!20, 2/red!20} {
    \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
      \draw[top color=white, bottom color=\b, 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}
| improve this answer | |
3

You can even simplify it further. \a is just a counter, so you may want to do

foreach \mycolor [count=\a starting from 0] in {blue!20,green!20,red!20}

Full code:

\documentclass{article}
\usepackage[margin=0.75in, heightrounded]{geometry}
\usepackage{tikz}
\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 \mycolor [count=\a starting from 0] in {blue!20,green!20,red!20} {
            \foreach \y in {0,..., 3}  {
            \draw[black!80, fill=black!35, rounded corners=5ex] (\y*\u, 11.0+\v*\a) rectangle (4.2+\y*\u, 18.0+\v*\a);
            \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}

enter image description here

| improve this answer | |

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.