我想将路径均匀地(分成三部分)分割,这样当箭头尖遮住部分路径时就可以使用。换句话说,我希望以下文档中每种颜色的片段长度相等:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\newcommand{\withthirdscoordinates}[1]{
\begin{scope}
\coordinate (n0) at (\tikzinputsegmentfirst);
\coordinate (n3) at (\tikzinputsegmentlast);
\coordinate (n1) at (barycentric cs:n0=2,n3=1);
\coordinate (n2) at (barycentric cs:n0=1,n3=2);
#1
\end{scope}
}
\tikzset{
mystyle/.style={
decoration={
show path construction,
lineto code={
\withthirdscoordinates{
\draw [-, color=red] (n0) -- (n1);
\draw [-, color=green] (n1) -- (n2);
\draw [color=blue] (n2) -- (n3);
}
},
},
decorate
}
}
\tikzset{
every node/.style={circle, draw}
}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[-{Triangle[open]}, mystyle] (A) -- (B);
\end{tikzpicture}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[-{Triangle[open, length=35pt]}, mystyle] (A) -- (B);
\end{tikzpicture}
\end{document}
答案1
这个答案非常片面,因为它只适用于水平直线,而不适用于倾斜的直线。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
mystyle/.style={
-{Triangle[open, length=#1]},
decoration={
show path construction,
lineto code={
\coordinate (n0) at (\tikzinputsegmentfirst);
\coordinate (n3) at ([xshift=-#1]\tikzinputsegmentlast);
\coordinate (n4) at (\tikzinputsegmentlast);
\coordinate (n1) at (barycentric cs:n0=2,n3=1);
\coordinate (n2) at (barycentric cs:n0=1,n3=2);
\draw [-, color=red] (n0) -- (n1);
\draw [-, color=green] (n1) -- (n2);
\draw [-, color=blue] (n2) -- (n3);
\draw (n3) -- (n4);
}
},
decorate
},
mystyle/.default=5pt,
every node/.style={circle, draw}
}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[mystyle] (A) -- (B);
\end{tikzpicture}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (5,0) {B};
\draw[mystyle={35pt}] (A) -- (B);
\end{tikzpicture}
\end{document}
也可以看看这里。