22

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?

Martin Scharrer
  • 262,582
zar
  • 545

2 Answers2

30

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

two color dashed line

(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);
Frédéric
  • 11,087
  • 3
  • 37
  • 43
5

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}

preview for double color circle

Arne
  • 1,305