I would like to draw a rectangle with a double color border: the border should be a line dashed with two alternating colors (blue and red, for example). Is it possible?
2 Answers
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);
- 11,087
- 3
- 37
- 43
-
Nice! You could even use three or more colors using this technique. – Martin Scharrer Apr 17 '11 at 20:05
-
5For more complicated paths, it might be more convenient to use a
postaction, so that the path doesn't need to be specified twice. – Caramdir Apr 17 '11 at 20:05 -
Good! One last thing: how can I draw a "good" rectangle with this tecnique? I would like to have rounded corners, but now rounded dashes. – zar Apr 17 '11 at 20:14
-
I'm not sure what you mean about a good rectangle, with rounded corners and now [to you mean not] rounded dashes. – Frédéric Apr 17 '11 at 20:21
-
-
-
In case you want to have a double colored border around a node, you can do it like this:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[circle,draw=blue,dash pattern= on 3pt off 5pt,thick,postaction={draw,red,dash pattern= on 3pt off 5pt,dash phase=4pt,thick}] at (0,0) {TEST};
\end{tikzpicture}
\end{document}
- 1,305

tikzyou can define a dash pattern using e.g.dash pattern=on 2pt off 2pt. No idea how to handlegeogebrahere. – Martin Scharrer Apr 17 '11 at 20:04