As mentionned in Martin's comment, the trick is to draw the line twice. One of the constraints on paths in Tikz/PGF is that the color is global to the path.
Other than drawing a solid line covered by a dashed line, you may draw two dashed lines, with spaces between the dashes, as given by the following example (you can find more information in the Tikz manual) :
\begin{tikzpicture}
\draw[blue,dash pattern= on 3pt off 5pt] (0,0) |- (1,1) to[out=0,in=90] (2,0);
\draw[red,dash pattern= on 3pt off 5pt,dash phase=4pt] (0,0) |- (1,1) to[out=0,in=90] (2,0);
\end{tikzpicture}
The outcome is
(Following a comment by Caramdir) : The same result can be achieved with a postaction, and the path only has to be specified once :
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick}]
[blue,dash pattern= on 3pt off 5pt,thick] (0,0) |- (1,1) to[out=0,in=90] (2,0);
(Following a request for a rectangle) * I'm not sure this answers the question in the comment *
For a rectangle you would type in the command
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick}]
[blue,dash pattern= on 3pt off 5pt,thick] (0,0) rectangle (3,2);
This rectangle does not have rounded corners, it is a "normal" rectangle. If ever you do want rounded corners, add rounded corners
to the options :
\draw[postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick,rounded corners}]
[blue,dash pattern= on 3pt off 5pt,thick,rounded corners] (0,0) rectangle (3,2);
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
Best Answer
You can define your own parameterized style to do this, with
Complete example:
The two nodes are identical: