5

This question already has an answer here:

Is it possible to change the line style in a tikz figure in the same \draw command?

I am looking for something like:

\documentclass[tikz]{standalone}
\begin{document}
    \begin{tikzpicture}
        \draw[style1] (0,0) -- ++(1,0)  **CHANGE STYLE** -- ++(1,0);
    \end{tikzpicture}
\end{document}

The main motivation is that I don't want to loose the relative coordinate as I go on with a long \draw command.

marked as duplicate by Paul Gaborit tikz-pgf Mar 26 '16 at 8:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

8

You can't have multiple line styles within the same path. You can insert square brackets pretty much anywhere on a path to change options, but most of them will then affect the entire path.

One way out is using the edge operation, which is similar to a node in that it is actually a separate path:

\draw[style1] (0,0) -- ++(1,0) edge[style2] ++(1,0);

This works fine for straight lines and basic curves (try bend left, in angle and out angle as options in style2).

Adding to Jens Polz's answer, I would suggest using the coordinate operation instead of node: using it, you don't need to provide empty node contents and you also don't get gaps in your path:

\draw[style1] (0,0) -- ++( 1,0) coordinate (x);
\draw[style2] (x)   -- ++( 0,1) coordinate (x);
\draw[style3] (x)   -- ++(-1,0);

It's possible to re-use the same name for this temporary coordinate.

  • You can prevent the gaps by setting inner sep=0 in the node options, which I added above. However your approach seems to be a little more elegant. – JMP Mar 26 '16 at 8:36
5

I am not quite sure, if you can change the style within a plotting command, so I would be interested in an answer to your question myself. However, to not loose the relative coordinates, you can do the following. By placing and naming a node at the last coordinate of your plotting command, you can call it in the next one, without loosing your relative coordinates:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
  \draw[thick] (0,1) -- ++ (1,0) -- ++ (4,2) -- ++ (1,1) node(A)[inner sep=0]{};
  \draw[thick, dashed] (A) -- ++ (-2,0) -- ++ (0,-2) node(B)[inner sep=0]{};
  \draw[thick] (B) -- ++ (2,0);
\end{tikzpicture}  
\end{document}
  • Thanks Jenz. I thought about this earlier as well. My biggest was that I was using this snippet in a for loop and so I wasn't quite sure how to label the nodes in the loop, or how it will work out. Thanks for your help and good point. – Bichoy Mar 27 '16 at 16:22

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