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?

3
  • 2
    Related question: Two color line in gnuplot. The basic idea is to draw the line twice. Once solid with one color then dashed with the second one. Apr 17, 2011 at 19:35
  • Thank you. I don't know how to equally divide the space (actually, I am using geogebra to generate the rectangle: how can I specify the parameters of the dashed line?)
    – zar
    Apr 17, 2011 at 19:56
  • 1
    For tikz you can define a dash pattern using e.g. dash pattern=on 2pt off 2pt. No idea how to handle geogebra here. Apr 17, 2011 at 20:04

2 Answers 2

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);
7
  • Nice! You could even use three or more colors using this technique. Apr 17, 2011 at 20:05
  • 5
    For 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, 2011 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, 2011 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, 2011 at 20:21
  • Sorry, "not" rounded dashes.
    – zar
    Apr 17, 2011 at 20:34
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .