5

Following this question, I was wondering if it is possible to draw (with a single \draw call) a curved line that is partially of one style then of another etc ...

A solution proposed in the linked post works for straight lines. But when adding in = ..., out=... options to the to paths, the style goes back to the default, which is not really surprising.

I already tried with append after command to see it I could append the style but it doesn't seem to work.

Basically, the idea would be to alter the styles to support other options but I am not comfortable enough with this to see what to do.

Thanks for the help!

The MWE below, taken from @AlainMatthes answer reproduces the missing styles.

\documentclass[tikz]{standalone}

\tikzset{%
xultra thick/.style={to path={%
\pgfextra{%
 \draw[line cap=round, line join=round,ultra thick]
      (\tikztostart) -- (\tikztotarget);} (\tikztotarget) \tikztonodes}},
xthin/.style={to path={%
\pgfextra{%
 \draw[line cap=round, line join=round,thin] 
       (\tikztostart) -- (\tikztotarget);} (\tikztotarget)  \tikztonodes}}} 
\begin{document}
\begin{tikzpicture}
  \draw (0.25,0) -- (0.75,1.2) -- (1.25,1.5) 
  to[xultra thick,in=70,out=50]  (1.75,0.7) -- (2.25,1.1) -- (2.75,0.5)
                 -- (3.25,1.5) to[xthin,in=50,out=80] (4.25,1.8);
\end{tikzpicture}
\end{document} 
6

Seems that we have sort of agreed that it is not necessarily a good idea to work with \pgfextra and glad to see you consider using edges. One way to avoid the need to double the coordinates has been proposed in this nice answer, which is interesting on its own.

A common problem of many of these proposals is, however, that they tend to destroy the paths. That is, you can no longer use the path to define a boundary that can be filled, or use the path in intersections and so on. Here is a way to avoid this problem, building on this answer. Look at the MWE

\documentclass[tikz,border=3.14mm]{standalone} 
\usetikzlibrary{intersections} % only to illustrate the path does not get destroyed
\pgfkeys{tikz/.cd,
set edge options/.code={\tikzset{edge style/.style={#1}}},
}
\begin{document}
\begin{tikzpicture}[every edge/.append code = {% https://tex.stackexchange.com/a/396092/121799 
    \global\let\currenttarget\tikztotarget % save \tikztotarget in a global variable
    \pgfkeysalso{append after command={to[edge style] (\currenttarget)}}},
every edge/.append style={edge style}   ] 
\path[name path=A,fill=blue!20] (0.25,0)  
[set edge options={in=-120,out=50}]  edge[ultra thick] (0.75,1.2) 
[set edge options={in=-140,out=60}] edge[ultra thick,blue]  (1.25,1.5) 
[set edge options={in=70,out=50}] edge[ultra thick,red,dashed]  (1.75,0.7) 
-- (2.25,0.1) 
[set edge options={bend left=20}] edge[purple,dotted,thick] (2.75,-0.5) 
[set edge options={bend left=20}] edge[purple,dashed,thick] cycle;
\draw[name path=B] (3,0) to[bend right=16] (0,1);
\fill[yellow,name intersections={of=A and B}] (intersection-1) circle(2pt)
(intersection-2) circle(2pt);
\end{tikzpicture} 
\end{document}

enter image description here

All you need to is to say in a path

(<point n>) [edge options={<path options>}] edge[<visual>] (<point n+1>)

Here the path options are those that you normally would feed into a to[...] of a path like in=...,out=...,looseness=... or bend left=..., say. The visual things are those that affect the style of the curve such as color, dashed, thickness etc. In particular, these are the things that you normally can not change along the path.

Note that the intersections library is only loaded for illustration, the syntax proposed here does not rely on it.

ANOTHER UPDATE: Just noticed that you do not have to separate the <path options> from the <visual> ones. TikZ is clever enough to just ignore the directives if they do not apply. So you can simplify things further to

\documentclass[tikz,border=3.14mm]{standalone} 
\pgfkeys{tikz/.cd,
edge options/.code={\tikzset{edge style/.style={#1}}},
}
\begin{document}
\begin{tikzpicture}[every edge/.append code = {% https://tex.stackexchange.com/a/396092/121799 
    \global\let\currenttarget\tikztotarget % save \tikztotarget in a global variable
    \pgfkeysalso{append after command={to[edge style] (\currenttarget)}}},
every edge/.append style={edge style}   ] 
\path[fill=blue!20] (0.25,0)  
[edge options={in=-120,out=50,ultra thick,black,-latex}] edge (0.75,1.2) 
[edge options={in=-140,out=60,ultra thick,blue,latex-}] edge   (1.25,1.5) 
[edge options={in=70,out=50,ultra thick,red,dashed}] edge  (1.75,0.7) 
-- (2.25,0.1) 
[edge options={bend left=20,purple,dotted,thick}] edge (2.75,-0.5) 
[edge options={bend left=20,purple,dashed,thick}] edge cycle;
\end{tikzpicture} 
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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