TikZ 中自定义箭头有多种选择

TikZ 中自定义箭头有多种选择

我想通过命令为自定义箭头设置多个选项pgfsetarrowoptions。可以吗?我该怎么做?然后,如何使用命令捕获值pgfgetarrowoptions

例如,想象一下,在下面的例子中,我需要从以下位置更改尖端的角度和颜色pgfmanual

\documentclass[border=1mm,tikz]{standalone}
\newlength\arrowsize
\pgfarrowsdeclare{var arc}{var arc} % options is an angle
{
\arrowsize=0.2pt
\advance\arrowsize by .5\pgflinewidth
\pgfarrowsleftextend{-4\arrowsize-.5\pgflinewidth}
\pgfarrowsrightextend{.5\pgflinewidth}
}
{
\arrowsize=0.2pt
\advance\arrowsize by .5\pgflinewidth
\pgfsetdash{}{0pt} % do not dash
\pgfsetroundjoin % fix join
\pgfsetroundcap % fix cap
\pgfpathmoveto{\pgfpointorigin}
\pgfpatharc{-90}{-180+\pgfgetarrowoptions{var arc}}{4\arrowsize}
\pgfusepathqstroke
\pgfpathmoveto{\pgfpointorigin}
\pgfpatharc{90}{180-\pgfgetarrowoptions{var arc}}{4\arrowsize}
\pgfusepathqstroke
}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-2,-4) grid (1,4);
\foreach \option in {-60,-50,...,60}
{
\pgfsetarrowoptions{var arc}{\option}
\draw[ultra thick,-var arc] (-2,\option/15) -- (0,\option/15);
}
\end{tikzpicture}
\end{document}

答案1

我认为,如果没有太多属性需要传送到箭头宏,那么将所有内容加载到一个变量中是一种替代方法。这是一个概念证明。我没有过多关注变量打包的健康和便利性,但它在这个例子中确实起到了作用。

\documentclass[border=1mm,tikz]{standalone}
\newlength\arrowsize
\def\getmyoptions#1/#2/{
\xdef\optionone{#1}%
\xdef\optiontwo{#2}%
}
\pgfarrowsdeclare{var arc}{var arc} % options is an angle
{
\arrowsize=0.2pt
\advance\arrowsize by .5\pgflinewidth
\pgfarrowsleftextend{-4\arrowsize-.5\pgflinewidth}
\pgfarrowsrightextend{.5\pgflinewidth}
}
{
\begingroup\edef\temp{\endgroup\noexpand\getmyoptions\pgfgetarrowoptions{var arc}}\temp
\arrowsize=0.2pt
\advance\arrowsize by .5\pgflinewidth
\pgfsetdash{}{0pt} % do not dash
\pgfsetroundjoin % fix join
\pgfsetroundcap % fix cap
\pgfsetstrokecolor{blue!\optiontwo}
\pgfpathmoveto{\pgfpointorigin}
\pgfpatharc{-90}{-180+\optionone}{4\arrowsize}
\pgfusepathqstroke
\pgfpathmoveto{\pgfpointorigin}
\pgfpatharc{90}{180-\optionone}{4\arrowsize}
\pgfusepathqstroke
}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-3,-4) grid (1,2);
\foreach \x in {1,...,10}
{
\pgfmathtruncatemacro{\xj}{-10*\x}
\pgfmathtruncatemacro{\xk}{5*\x}
\edef\option{\xj/\xk/}
\pgfsetarrowoptions{var arc}{\option}
\draw[ultra thick,-var arc] (-2 cm,2cm - \xk mm) -- (0,2cm - \xk mm);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容