[Tex/LaTex] How to draw the border of a node using late options

colornodestikz-chainstikz-pgf

I want to create a series of rectangles, each with a different shade. The shade varies from white to black. I want to make the white (first) rectangle visible by drawing its border.

My first step is to draw all the rectangles, without any conditional code:

\documentclass{standalone}
\usepackage[rgb]{xcolor}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
  \begin{tikzpicture}[start chain=chroma 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{tmpc}{Hsb}{0,0,\value}
      \draw node[on chain,fill=tmpc] {};
    }
  \end{tikzpicture}
\end{document}

I tried to replace the foreach code with this:

    {
      \definecolor{tmpc}{Hsb}{0,0,\value}
      \draw node[on chain,fill=tmpc] {}
      \ifnum\n=0
        (\tikzlastnode) [late options={draw}] % no effect
        (\tikzlastnode.east) [draw] -- ++(1cm,0) % drawn correctly
      \fi
      ; % ends the path
    }

Adding draw to the node options directly works. Only the late options don't behave like I expect them to do – maybe I'm not understanding something here? I have added code to see if the ifnum code is actually executed, and it is indeed.

Is there a class of options that don't work as late options?

Best Answer

A slightly different way is described in the following code. Note that you don't really need xcolor for this. Two tikz pictures are drawn, one using xcolor, the other using the basic commands in tikz. I drew the chain horizontally to save on space. I also added a macro that let's you change the number of nodes.

\documentclass{minimal}

\usepackage[rgb]{xcolor}
\usepackage{tikz}
\usetikzlibrary{chains}


\begin{document}

%with xcolor
\begin{tikzpicture}[start chain=chroma going right,
    node distance = 2mm,
    every node/.style ={shape=rectangle,minimum size =1cm,on chain}]

\pgfmathsetmacro{\nbnodes}{8}
\pgfmathsetmacro{\factor}{1/\nbnodes}

\foreach \n [evaluate=\n as \value using 1-\factor*\n] in {0,...,\nbnodes}{

    \pgfmathsetmacro{\bordershade}{ifthenelse(\n==0,100,0)}

    \definecolor{tmpc}{Hsb}{0,0,\value}
    \node[draw=black!\bordershade,fill=tmpc] {};
}
\end{tikzpicture}


%without xcolor
\begin{tikzpicture}[start chain=chroma going right,
    node distance = 2mm,
    every node/.style ={shape=rectangle,minimum size =1cm,on chain}]

\pgfmathsetmacro{\nbnodes}{8}
\pgfmathsetmacro{\factor}{100/\nbnodes}

\foreach \n [evaluate=\n as \shade using \factor*\n] in {0,...,\nbnodes}{

    \pgfmathsetmacro{\bordershade}{ifthenelse(\n==0,100,0)}

    \node[draw=black!\bordershade,fill=black!\shade] {};
}
\end{tikzpicture}

\end{document}

The output is

shading