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);
You can use the library patterns
. For example:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\draw[pattern=north west lines, pattern color=blue] (0,0) rectangle (2,4);
\end{tikzpicture}
\end{document}
which gives you as result:
You find much more in the documentation (pgfmanual): Chapter 62 Pattern Library.
Best Answer
Just an idea, declare an opposite pattern to the desired result and use it within a
postaction
over a gradient colored area.Following code declares a
mycheckerboard
pattern (inspired in originalcheckerboard
pattern). It fills little squares (2mmx2mm) leaving 1mm space between them. Used in a postaction simulates 1mm width crossing lines.