Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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. –  Martin Scharrer Apr 17 '11 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 '11 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. –  Martin Scharrer Apr 17 '11 at 20:04

1 Answer 1

up vote 12 down vote accepted

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);
share|improve this answer
    
Nice! You could even use three or more colors using this technique. –  Martin Scharrer Apr 17 '11 at 20:05
3  
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 '11 at 20:05
    
@Caramdir : Thanks for the suggestion –  Frédéric Apr 17 '11 at 20:12
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.