Tikz 装饰继承风格?

Tikz 装饰继承风格?

有没有办法控制 tikz 路径替换绘制的路径是否继承原始路径的样式?在尝试回答这个问题,我注意到厚度(默认情况下)不是一致继承的。特别是,厚度似乎是由双线和箭头继承的,但不是纯线(可能是因为这些路径是由装饰本身产生的?)。其他样式(例如颜色)似乎根本没有被继承。有没有一种好的方法可以将所有选项传递给替换路径?

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

不一致的箭头

我希望(或者更确切地说,期望)第一个命令和第二个命令产生相同的输出。

答案1

事先的如果装饰声明样式首先包含/utils/exec=\csname tikz@options\endcsname,,则可以继承选项,这将把当前选项提取到此样式中并由方法继承show path construction。 (我不是 TikZ 专家,因此欢迎更高级的用户对此方法的安全性发表评论。)

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

原始;继承先前的选项;忽略后续选项;替代方法

答案2

这不是一个解决方案,而是一个解决方法:

  • 一种可能性是使用参数将路径样式传递给z->
  • 另一种可能性是使用更短的范围命令\scoped

以下是示例代码:

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

在此处输入图片描述

相关内容