[Tex/LaTex] using foreach to draw a chain with a branch

loopsnodestikz-chainstikz-pgf

This is an extension to How to draw the border of a node using late options

I'm drawing a chain of nodes with different colors using foreach from tikz. Now I want to add a branch, also using foreach. However, starting a branch inside a chain that was constructed using foreach does not work the way I expected:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes}

\begin{document}
  \begin{tikzpicture}[node distance=2mm,
                    every node/.style={shape=rectangle,minimum size=1cm},
                   ]
  { [start chain=values going below]
    \foreach \m in {0,...,8}
    {
      \node[on chain] {\m};
      \ifnum\m=3
      {
        { [start branch=stuff going right]
          \node[on chain] {A};
        }
      }
      \fi
    }
  }
  \end{tikzpicture}
\end{document}

All nodes are placed below each other, in the order 0 1 2 3 A 4 5 6 7 8. It seems that [start branch=stuff going right] does not have the effect of extending the branch to the right. The same happens when I replace the manual insertion of the A-node with another foreach.

Everything is drawn correctly when I build the chain manually, as in

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes}

\begin{document}
  \begin{tikzpicture}[node distance=2mm,
                    every node/.style={shape=rectangle,minimum size=1cm},
                   ]
  { [start chain=values going below]
    \node[on chain] {1};
    \node[on chain] {2};
    \node[on chain] {3};
    { [start branch=stuff going right]
      \node[on chain] {A};
    }
    \node[on chain] {4};
  }
  \end{tikzpicture}
\end{document}

Is there an error in my code or my understanding of foreach?
Is there a workaround? (I need the evaluate option of foreach or an equivalent, see the question I linked above)

Edit: I also tried using a matrix and a foreach inside, but the foreach closing } interferes with the matrix code.


Finished! The following code uses chains (including a branch), color calculations, and placing stuff using perpendicular lines to generate the result shown below.

\documentclass{standalone}
\usepackage[rgb]{xcolor} % tikz doesn't support Hsb
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{scopes} % needed by the chains lib

\begin{document}
  \begin{tikzpicture}
    { [start chain=values going below,node distance=2mm,
       every node/.style={shape=rectangle,minimum size=1cm}]
      \foreach \n [evaluate=\n as \value using 1-\n*0.125] in {0,...,8}
      {
    \definecolor{tmpca}{Hsb}{0,0,\value}
    \node[on chain,fill=tmpca] {};
    \ifnum\n=3
    {
      \begin{scope}[start branch=saturations going right]
        \foreach \m [evaluate=\m as \saturation using \m*0.125] in {1,...,8}
        {
          \definecolor{tmpcb}{Hsb}{0,\saturation,\value}
          \node[on chain,fill=tmpcb] {};
        }
      \end{scope}
    }
    \fi
      }
    }
    \draw (values-1.south west) rectangle (values-1.north east);
    \node[anchor=south,rotate=90] at (values-5.west) {value scale (light/dark contrast)};

    \node[anchor=south west] at (values/saturations-3.north west) {chroma scale (saturation contrast)};
    \draw (values/saturations-3.south west) node[anchor=north west] (chroma-label) {gray to full chroma};
    \draw (chroma-label)[line width=1pt,->] -- (chroma-label.east -| values/saturations-end.west);
    \draw (chroma-label.south west) node [anchor=north west] {values of each color remain equal to gray};

    \node[anchor=south east,text width=8cm]
    at (values-end.south -| values/saturations-end.east)
    {This is a Ti\emph{k}Z implementation of a figure found in \textsc{Puhalla, M.}:
    \textit{Perceiving hierarchy through intrinsic color structure}.
    Visual Communication 7 (2008), Nr. 2, pp 199--228};

  \end{tikzpicture}
\end{document}

enter image description here

I don't like the gray impression of the red squares, they don't seem to match the gray square on the left. I've transferred this aspect of the problem to https://graphicdesign.stackexchange.com/questions/2204/gray-to-color-conversion.

Best Answer

The scopes library introduces the shorthand { [options] <...> } for the longer \begin{scope}[options] <...> \end{scope}. Apparently, this shorthand does not work perfectly though: If you use the conventional \begin{scope} notation, your example works:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
  \begin{tikzpicture}[node distance=2mm,
                    every node/.style={shape=rectangle,minimum size=1cm},
                   ,start chain=values going below]
   \foreach \m in {0,...,6}
    {
      \node[on chain] {\m};
      \ifnum\m=3
        \begin{scope}[start branch=stuff going right]
          \node[on chain] {A};
        \end{scope}
      \fi
    }
  \end{tikzpicture}
\end{document}

foreach with chain branch