4

I create a rectangle like this:

\draw (2,2) -- (2,4) -- (6,4) -- (6,2) -- (2,2);

I would like to give the red colour to the side defined by points (2,2) and (2,4).

I tried doing

\draw [red] (2,2) -- (2,4) -- (6,4) -- (6,2) -- (2,2);

but this colours the entire rectangle.

1
  • Better end cycles with -- cycle rather than -- (a,b) where (a,b) are the coordinates of the first point.
    – Manuel
    Nov 9, 2014 at 13:22

2 Answers 2

5

there may be other ways but just splitting in to two is a simple possibility:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [red] (2,2) -- (2,4);
\draw (2,4) -- (6,4) -- (6,2) -- (2,2);
\end{tikzpicture}
\end{document}
4
  • I'm sorry sir we don't serve experts in TikZ questions. I'm gonna have to ask you to leave. Hmm, I think you have been called by the L3 team to meet at the reception...
    – percusse
    Nov 9, 2014 at 13:57
  • @percusse seriously if there is a way to have it as a continuous cycle and change colour you should post, of course it's not so obvious with a rectangle but a smooth curve would be hard to keep smooth if done as two paths I'd guess Nov 9, 2014 at 14:33
  • As far as I know, you cannot change the pen properties on a single path.
    – percusse
    Nov 9, 2014 at 16:09
  • @percusse I knew I should stick to picture mode Nov 9, 2014 at 16:20
2

Another option: draw the rectangle and overwrite in red the side you want.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (2,2) rectangle (6,4);
\draw [red] (2,2) -- (2,4);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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