我想在 TikZ 中创建虚线,每条虚线的末端都有一个箭头。以下代码使用 \foreach 循环实现此目的,但正在寻找一种使用样式键来实现此目的的方法。类似于
\draw [densely dashed, ->] (0,0) -- ++(100pt,0);
但将箭头放在所有破折号上而不仅仅是末端。
梅威瑟:
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [densely dashed] (0,0) -- ++(100pt,0);
\foreach \i in {0,5,10,...,100}{%
\draw [->] (\i*1pt,-0.25) -- ++(3pt,0);
}
\end{tikzpicture}
\end{document}
答案1
我想对于这个最简单的可能性之一就是采用decorations.markings
。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\draw [dash pattern=on 3pt off 2pt,postaction={decorate,
decoration={markings,
mark=between positions 3pt and 1 step 5pt with {\arrow{>};}}}] (0,0) -- ++(100pt,0);
\end{tikzpicture}
\end{document}
根据实际应用,可以将其设为样式,或者,对于弯曲路径来说,更好的方法是声明一个新的(元)装饰。幸运的是,pgfmanual 在第 1007 页上有一个元装饰,只需稍微修改一下即可
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations,arrows.meta}
\pgfdeclaremetadecoration{many arrows}{initial}{
\state{initial}[width=0pt, next state=arrow] {
\pgfmathdivide{100}{\pgfmetadecoratedpathlength}
\let\factor\pgfmathresult
%\pgfsetlinewidth{1pt}
\pgfset{/pgf/decoration/segment length=4pt}
}
\state{arrow}[
switch if less than=\pgfmetadecorationsegmentlength to final, width=\pgfmetadecorationsegmentlength/3,
next state=end arrow]
{
\decoration{curveto}
\beforedecoration
{
\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}
} }
\state{end arrow}[width=\pgfmetadecorationsegmentlength/3, next state=move] {
\decoration{curveto}
\beforedecoration{\pgfpathmoveto{\pgfpointmetadecoratedpathfirst}}
\afterdecoration
{
\pgfsetarrowsend{Latex[length=1pt,width=1pt]}
\pgfusepath{stroke}
}
}
\state{move}[width=\pgfmetadecorationsegmentlength/2, next state=arrow]{} \state{final}{}
}
\begin{document}
\begin{tikzpicture}
\draw[ultra thin,decorate,decoration={many arrows,meta-segment length=3pt}] (0,0) .. controls (0,2) and (3,2) .. (3,0)
.. controls (3,-2) and (0,-2) .. (0,-4)
.. controls (0,-6) and (3,-6) .. (3,-8)
.. controls (3,-10) and (0,-10) .. (0,-8);
\end{tikzpicture}
\end{document}
这里我放大了结果以显示箭头很小。(回想一下,直径是 3 厘米。)