6

Is there any way to control whether the paths drawn by tikz path replacing inherit the styles of the original path? In trying to answer this question, I noticed that the thickness is (by default) not consistently inherited. In particular, thickness seems to be inherited by double lines and arrowheads, but not plain lines (possibly because those paths are produced by decorations themselves?). Other styles (e.g., color) mostly do not seem to be inherited at all. Is there a good way to pass all options to the replacement path?

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

\begin{document}
\begin{tikzpicture}
  \draw [z->, red, thick] (0,0) -- (1,0);
  \begin{scope}[red, thick]
    \draw [z->] (0,.2) -- (1,.2);
  \end{scope}
\end{tikzpicture}
\end{document}

inconsistent arrows

I would like (or rather, expect) the first command and the second to produce the same output.

2 Answers 2

6

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}

original; inherit prior options; ignore trailing options; alternate approach

1

This is not a solution, but a workaround :

  • One possibility is to use a parameter to pass the path style to z->.
  • Another possibility is to use a shorter scope command \scoped.

Here is the example code :

\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[#1,double] (\tikzinputsegmentfirst) -- (mid);
        \draw[#1,->] (mid) -- (\tikzinputsegmentlast);
      }
    },decorate
  }
}

\begin{document}
\begin{tikzpicture}
  \draw[z->={red, thick}] (0,0) -- (1,0);
  \scoped[green, thick]\draw[z->] (0,.2) -- (1,.2);
\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 .