以下代码在线的中点绘制箭头:
\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
mid arrow/.style={
postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow{latex}}
}}
},
]
\draw[thin,gray,dashed] (0,-1) -- (0,1);
\draw[mid arrow] (-1,0.1) -- (1,0.1);
\draw[mid arrow] (1,-0.1) -- (-1,-0.1);
\end{tikzpicture}
\end{document}
结果正如预期:
请注意,箭头的尖端是标记放置位置的参考点,这就是虚线灰色线与两个尖端相交的原因。(一般来说,这就是你想要的:线末端的箭头应该在线末端!)
不过,我希望这两个箭头位于中线上,例如这样:
(我通过将标记的位置调整为 0.54 来实现这一点,但这当然是一个取决于线的长度的技巧。)
是否有某种方法可以将箭头固定在其尖端以外的其他地方,或者以其他方式调整标记的位置,比如说,3.2pt
(在我的实际使用情况下,是箭头长度的一半)?
答案1
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.markings, arrows.meta}
\begin{document}
\begin{tikzpicture}[
mid arrow/.style={
postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow{Latex[length=6.4pt, sep=-3.2pt -1]}}
}}
},
]
\draw[thin,gray,dashed] (0,-1) -- (0,1);
\draw[mid arrow] (-1,0.1) -- (1,0.1);
\draw[mid arrow] (1,-0.1) -- (-1,-0.1);
\draw[mid arrow, ultra thick] (-1,-0.4) -- (1,-0.4);
\draw[mid arrow, ultra thick] (1,-0.6) -- (-1,-0.6);
\end{tikzpicture}
\end{document}
编辑: 额外的线宽偏移sep=-3.2pt -1
是错误的 - 可以在将箭头设为红色时看到这一点
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.markings, arrows.meta}
\begin{document}
\begin{tikzpicture}[
mid arrow/.style={
postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow{Latex[length=6.4pt, sep=-3.2pt, red]}}
}}
},
]
\draw[thin,gray,dashed] (0,-1) -- (0,1);
\draw[thin,gray,dashed, xshift=3.2pt] (0,-1) -- (0,1);
\draw[thin,gray,dashed, xshift=-3.2pt] (0,-1) -- (0,1);
\draw[mid arrow] (-1,0.1) -- (1,0.1);
\draw[mid arrow] (1,-0.1) -- (-1,-0.1);
\draw[mid arrow, ultra thick] (-1,-0.4) -- (1,-0.4);
\draw[mid arrow, ultra thick] (1,-0.6) -- (-1,-0.6);
\end{tikzpicture}
\end{document}
但当箭头为黑色时,从视觉上看是错误的:
我猜想总是需要根据线宽和箭头进行手动调整。