Tikz decorations inheriting styles?
The prior options can be inherited if the decoration-declaring style first includes /utils/exec=\csname tikz@options\endcsname,
, which should extract the current options into this style and be inherited by the show path construction
methods. (I'm not a TikZ expert, so comments from more advanced users about the safety of this approach would be welcome.)
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
z->/.style={
decoration={
show path construction,
lineto code={
\path (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast) coordinate[pos=.5] (mid);
\draw[double] (\tikzinputsegmentfirst) -- (mid);
\draw[->] (mid) -- (\tikzinputsegmentlast);
}
},decorate
}
}
\tikzset{
y->/.style={
/utils/exec=\csname tikz@options\endcsname,
decoration={
show path construction,
lineto code={
\path (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast) coordinate[pos=.5] (mid);
\draw[double] (\tikzinputsegmentfirst) -- (mid);
\draw[->] (mid) -- (\tikzinputsegmentlast);
}
},decorate
}
}
\tikzset{
x->/.style={
inherit options/.code={\csname tikz@options\endcsname},inherit options,
decoration={
show path construction,
lineto code={
\path (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast) coordinate[pos=.5] (mid);
\draw[double] (\tikzinputsegmentfirst) -- (mid);
\draw[->] (mid) -- (\tikzinputsegmentlast);
}
},decorate
}
}
\begin{document}
\begin{tikzpicture}
\node[anchor=west] at (1,0) {\tiny original};
\draw [red, thick, z->] (0,0) -- (1,0);
\begin{scope}[red, thick]
\draw [z->] (0,.2) -- (1,.2);
\end{scope}
\tikzset{yshift=-0.6cm}
\node[anchor=west] at (1,0) {\tiny inherit prior options};
\draw [green, thick, y->] (0,0) -- (1,0);%options PRIOR to "y->" are inherited
\begin{scope}[green, thick]
\draw [y->] (0,.2) -- (1,.2);
\end{scope}
\tikzset{yshift=-0.6cm}
\node[anchor=west] at (1,0) {\tiny does not inherit trailing options};
\draw [blue, thick, y->, densely dotted] (0,0) -- (1,0);% options AFTER "y->" are NOT inherited
\begin{scope}[blue, thick, densely dotted]
\draw [y->] (0,.2) -- (1,.2);
\end{scope}
\tikzset{yshift=-0.6cm}
\node[anchor=west] at (1,0) {\tiny alternate approach};
\draw [orange, thick, x->] (0,0) -- (1,0);% options PRIOR to "x->" are inherited
\begin{scope}[orange, thick]
\draw [x->] (0,.2) -- (1,.2);
\end{scope}
\end{tikzpicture}
\end{document}