双色自定义箭头 tikz

双色自定义箭头 tikz

我正在创建一个自定义箭头,其绘图中需要有两种颜色。

提供一种颜色作为标准选项color=

我在附加代码中对第二种颜色进行了硬编码。我想second color为第二种颜色创建一个选项(例如),但我不知道在定义箭头时如何正确地做到这一点。手册第 100 节中的所有示例都与尺寸有关,而不是颜色。

为了澄清起见,我希望有一个选项可以让我将绿色内部设置为我想要的任何其他颜色。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\pgfdeclarearrow{
  name = twocolor,
  parameters = {%
   \the\pgfarrowlength 
  },
  setup code = {%
    % The different end values:
    \pgfarrowssettipend{.25\pgfarrowlength}
    \pgfarrowssetlineend{-.25\pgfarrowlength}
    \pgfarrowssetvisualbackend{-.5\pgfarrowlength}
    \pgfarrowssetbackend{-.75\pgfarrowlength}
    % The hull
    \pgfarrowshullpoint{.25\pgfarrowlength}{0pt}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}%
    % Saves: Only the length:
    \pgfarrowssavethe\pgfarrowlength
  },
  drawing code = {%
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
    \pgfsetcolor{green}
    \pgfpathmoveto{\pgfqpoint{.15\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{.4\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.4\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{-.4\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
  },
  defaults = {%
   length = 2cm 
  },
}

\begin{document}



\begin{tikzpicture}
\draw [color=red,arrows={-twocolor[length=3cm,color=blue]}] (0,0) -- (8,0);
\end{tikzpicture}

\end{document}

双色标准箭头

答案1

好的,根据我之前的一个问题的回答使用路径选项设置自定义阴影的颜色,我已经能够设置 pgfkeys 来使其工作。但是,我担心这样做会绕过箭头缓存机制,所以我认为这可能不是最佳选择。

无论如何,代码如下:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{
    /pgf/arrow keys/second color/.store in=\mycolor,
    /pgf/arrow keys/second color=green
}

\pgfdeclarearrow{
  name = twocolor,
  parameters = {%
   \the\pgfarrowlength 
  },
  setup code = {%
    % The different end values:
    \pgfarrowssettipend{.25\pgfarrowlength}
    \pgfarrowssetlineend{-.25\pgfarrowlength}
    \pgfarrowssetvisualbackend{-.5\pgfarrowlength}
    \pgfarrowssetbackend{-.75\pgfarrowlength}
    % The hull
    \pgfarrowshullpoint{.25\pgfarrowlength}{0pt}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}%
    % Saves: Only the length:
    \pgfarrowssavethe\pgfarrowlength
  },
  drawing code = {%
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
    \pgfsetcolor{\mycolor}
    \pgfpathmoveto{\pgfqpoint{.15\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{.4\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.4\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{-.4\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
  },
  defaults = {%
   length = 2cm 
  },
}

\begin{document}



\begin{tikzpicture}
\draw [color=red,arrows={-twocolor[length=3cm,color=blue,second color=orange]}] (0,0) -- (8,0);
\end{tikzpicture}

\end{document}

我欢迎任何有关更好方法的建议。

以橙色为中心双色箭头

答案2

如果我改变内部颜色而不改变主颜色或长度,您的代码对我不起作用。例如,

\begin{tikzpicture}
  \draw [color=red,arrows={-twocolor[length=3cm,color=blue,second color=orange]}] (0,0) -- (8,0);
  \draw [color=red,arrows={-twocolor[length=3cm,color=blue,second color=green]}] (0,1) -- (8,1);
\end{tikzpicture}

生产

橙色太多

这可能不是你想要的。

为什么不将它配置成与标准笔尖相同的工作方式呢?然后您可以说例如blue, fill=red并获得蓝色轮廓和红色笔芯作为笔尖,而不管线条颜色如何。

我不知道这是否是正确的方法,但这在最低限度的测试中是有效的:

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{arrows.meta}
\makeatletter
\pgfdeclarearrow{
  name = twocolor,
  parameters = {%
   \the\pgfarrowlength
  },
  setup code = {%
    % The different end values:
    \pgfarrowssettipend{.25\pgfarrowlength}
    \pgfarrowssetlineend{-.25\pgfarrowlength}
    \pgfarrowssetvisualbackend{-.5\pgfarrowlength}
    \pgfarrowssetbackend{-.75\pgfarrowlength}
    % The hull
    \pgfarrowshullpoint{.25\pgfarrowlength}{0pt}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}%
    % Saves: Only the length:
    \pgfarrowssavethe\pgfarrowlength
  },
  drawing code = {%
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
    \pgfseteorule
    \ifx\pgf@arrows@stroke@color\pgfutil@empty\relax\pgfsetfillcolor{pgfstrokecolor}\else\pgfsetfillcolor{\pgf@arrows@stroke@color}\fi
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}}
    \pgfpathmoveto{\pgfqpoint{.15\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{.4\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.4\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.65\pgfarrowlength}{-.4\pgfarrowlength}}
    \pgfpathclose
    \pgfusepathqfill
  },
  defaults = {%
   length = 2cm
  },
}
\makeatother
\begin{document}
\begin{tikzpicture}
  \draw [color=red, arrows={-twocolor[length=3cm, blue, fill=orange]}] (0,0) -- (8,0);
  \draw [color=red, arrows={-twocolor[length=3cm, orange, fill=magenta]}] (0,1) -- (8,1);
  \draw [color=red, arrows={-twocolor[length=3cm, green, fill=yellow]}] (0,2) -- (8,2);
\end{tikzpicture}
\end{document}

多色箭头

即使只改变填充颜色,这也有效:

\begin{tikzpicture}
  \draw [color=red, arrows={-twocolor[length=3cm, blue, fill=orange]}] (0,0) -- (8,0);
  \draw [color=red, arrows={-twocolor[length=3cm, blue, fill=magenta]}] (0,1) -- (8,1);
  \draw [color=red, arrows={-twocolor[length=3cm, blue, fill=yellow]}] (0,2) -- (8,2);
\end{tikzpicture}

3 种填充颜色

当然,麻烦的是,您需要填充两次,而不是每次描边和填充一次。我不知道箭头尖在两条闭合路径下是否安全,或者它们是否真的只打算处理一条路径。但这个问题似乎是您的尖头设计的必然结果。

答案3

@cfr 我展示的箭头只是一个例子。

假设我改用这个双色箭头:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\tikzset{
    /pgf/arrow keys/second color/.store in=\mycolor,
    /pgf/arrow keys/second color=green
}

\pgfdeclarearrow{
  name = twocolor,
  parameters = {%
   \the\pgfarrowlength 
  },
  setup code = {%
    % The different end values:
    \pgfarrowssettipend{.25\pgfarrowlength}
    \pgfarrowssetlineend{-.25\pgfarrowlength}
    \pgfarrowssetvisualbackend{-.5\pgfarrowlength}
    \pgfarrowssetbackend{-.75\pgfarrowlength}
    % The hull
    \pgfarrowshullpoint{.25\pgfarrowlength}{0pt}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}%
    \pgfarrowshullpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}%
    % Saves: Only the length:
    \pgfarrowssavethe\pgfarrowlength
  },
  drawing code = {%
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathclose
    \pgfusepathqfill
    \pgfsetcolor{\mycolor}
    \pgfpathmoveto{\pgfqpoint{.25\pgfarrowlength}{0pt}}
    \pgfpathlineto{\pgfqpoint{-.75\pgfarrowlength}{-.5\pgfarrowlength}}
    \pgfpathlineto{\pgfqpoint{-.5\pgfarrowlength}{0pt}}
    \pgfpathclose
    \pgfusepathqfill
  },
  defaults = {%
   length = 2cm 
  },
}

\begin{document}



\begin{tikzpicture}
\draw [color=red,arrows={-twocolor[length=3cm,color=blue,second color=orange]}] (0,0) -- (8,0);
\end{tikzpicture}

\end{document}

现在,描边/填充选项没有正确的语义。

带有两种颜色分割的箭头

相关内容