这是另外两个问题的后续问题。第一个是每个顶点都有节点第二个是这。我的目标是将两者结合起来。具体来说,我想写一些类似的东西
\draw[polyline] (0,0) -- (1,1) -- (2,0) -- (3,1);
结果将是一条折线(多边形),其中每个顶点将用圆圈标记,并且每个线段的中间都会有一个箭头。这是我目前所得到的:
\begin{tikzpicture}[
mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
node at every point/.style={%
decoration={%
show path construction,
lineto code={%
\coordinate (pos) at ($0.5*(\tikzinputsegmentlast)+0.5*(\tikzinputsegmentfirst)$);
\coordinate (direction) at ($(\tikzinputsegmentlast)-(\tikzinputsegmentfirst)$);
\gettikzxy{(direction)}{\dirx}{\diry}
\pgfmathsetmacro\rotationdir{atan(\diry/\dirx)}
\node at (pos) [rotate=\rotationdir] {>};
\node at (\tikzinputsegmentlast) [#1] {};
},
moveto code={\node at (\tikzinputsegmentfirst) [#1] {};},
curveto code={\node at (\tikzinputsegmentlast) [#1] {};}
},
postaction=decorate
}
]
\draw[node at every point=mnode] (0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
结果如下:
此代码存在两个问题。首先,箭头看起来很糟糕,其次它无法处理垂直线段。在后一种情况下,\dirx
为零,代码失败。我尝试使用\pgfmathifthenelse{x}{y}{z}
of来破解它\ifthenelse
,但对我没有任何作用...
我该如何解决这两个问题?
答案1
如何装饰装饰?在这种情况下,用装饰lineto
来装饰代码markings
。我还没有用节点做过,但你应该明白大概的意思了。
\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\begin{document}
\begin{tikzpicture}[>=stealth,
node at every point/.style={%
decoration={%
show path construction,
lineto code={%
\path [decoration={markings,
mark=at position 0 with {\fill circle [radius=1pt];},
mark=at position .5 with {\arrow{>};},
mark=at position 1 with {\fill circle [radius=1pt];},
}, decorate] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
postaction=decorate
}
]
\draw [node at every point](0,0) -- (1,2) -- (2.3,0) -- (2.5,1);
\end{tikzpicture}
\end{document}