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}
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}