Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 http://graphicdesign.stackexchange.com/questions/2204/gray-to-color-conversion.

share|improve this question
    
Could you please make your examples into complete MWEs, starting from \documentclass? That makes it a lot easier for people to try out the code to come up with a solution. –  Jake May 23 '11 at 13:59
    
I did that and tried removing \usetikzlibrary{scopes}, but the examples didn't compile without that library. I have the impression that the chains library should include scopes, which interferes with foreach. Or something like that... –  Christoph May 23 '11 at 14:32
    
Oh wow, that looks really impressive! –  Jake May 23 '11 at 21:14

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
That's it, thank you! Even works in conjunction with the question I linked to. –  Christoph May 23 '11 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.