将参数传递给 \pgfdeclaredecoration

将参数传递给 \pgfdeclaredecoration

这个想法是创建绘制半实半虚矢量的装饰。根据 percusse 的建议,我写了以下 MWE:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{decorations}

\pgfdeclaredecoration{halfdashed}{initial}{%
\state{initial}[width=0.5*\pgfdecoratedpathlength,next state=final]{%
\pgfpathmoveto{\pgfpoint{0}{0}}%
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}%
\pgfusepathqstroke
}%
\state{final}{%
\pgfsetdash{{10pt}{10pt}}{10pt}%
\pgfpathmoveto{\pgfpoint{0}{0}}%
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}%
}}

\begin{tikzpicture}
\draw[line width=2pt,decorate,decoration={halfdashed},->] (0,1) -- ++(110pt,0);
\draw[line width=2pt,decorate,decoration={halfdashed},->] (0,0) -- ++(100pt,0);
\end{tikzpicture}

\end{document}

结果如下:

在此处输入图片描述

我得出结论,\pgfdeclaredecoration只在绘制装饰的最后使用描边参数。我怎样才能获取这些参数并将它们放在前面pgfpathqstroke?顺便说一句,我不得不使用pgfpathqstroke,因为\pgfdeclaredecoration在一开始就传递了箭头参数(doh!)。

答案1

那么,扭转这个想法是可行的。

所以,我画了虚线,并将前半部分装饰成实线

您不想弄乱装饰的最后部分,因为弄乱默认设置后您无法清理。但您可以弄乱装饰的前一部分(\pgfsetlinewidth{\pgflinewidth}),做您的事情,然后在完成状态之前返回默认设置(\pgfsetlinewidth{\tikzscope@linewidth}),然后就大功告成了!

免责声明:我思考\tikzscope@linewidth是装饰外部的默认线宽,但我不是 100% 确定。

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{decorations}

\makeatletter
\pgfdeclaredecoration{halffull}{initial}{%
\state{initial}[width=0.5*\pgfdecoratedpathlength,next state=final]{%
\pgfsetlinewidth{\pgflinewidth}
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}
\pgfusepathqstroke
\pgfsetlinewidth{\tikzscope@linewidth}}
\state{final}{%
\pgfpathmoveto{\pgfpoint{0}{0}}
\pgfpathlineto{\pgfpoint{0.5*\pgfdecoratedpathlength}{0}}}}
\makeatother

\begin{tikzpicture}
\draw[ultra thick,dashed,dash phase=3pt,decorate,decoration=halffull,->] (-2,0) -- (2,0);
\draw[very thin] (0,-1) -- (0,1);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容