我正在尝试使用矢量来装饰路径。我创建了两种不同的样式:一种是创建切线矢量,另一种是创建随机矢量。
我希望在路径的每个选定点上有两个向量:一个(dl
)是切线,另一个(Rvec
)是随机旋转的。
不幸的是,我无法同时应用这两种样式(第三张图片)。样式似乎出了点问题Rvec
:在第二张图片中它根本就不显示,而在第三张图片中它以某种方式禁止了装饰dl
。dl
样式似乎运行良好。
另外在某些点以Rvec
某种方式应用了两次(每个点有 2 个随机向量)但我似乎无法再次重现该行为。
你知道该如何解决这个问题吗?
这是我的最小例子
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.markings}
\tikzset{
% Tangent vector
dl/.style={
decoration={
markings,
mark=between positions 0 and 1 step #1 with {
\draw[{Circle[length=4pt]}-Latex, purple, very thick] (0,0) -- (28pt, 0) node[above]{$\vec{dl}$};
}
},
postaction={decorate}
},
% Random vector
Rvec/.style={
decoration={
markings,
mark=between positions 0 and 1 step #1 with {
\draw[{Circle[length=4pt]}-Latex, black, very thick] (0,0) -- (rand*360:28pt) node[above]{$\vec{H}$};
}
}
},
postaction={decorate}
}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, dl=0.2] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, Rvec=0.2] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, Rvec=0.2, dl=0.2] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\end{document}
答案1
有两个问题:
postaction=decorate
超出了Rvec
风格,这就是为什么第二张图片中没有显示随机向量的原因。- 装饰必须按升序排列。因此,您可能需要绘制两次路径。使用
use path
诡计。
建议代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.markings}
\makeatletter % https://tex.stackexchange.com/a/38995/121799
\tikzset{
use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\tikzset{
% Tangent vector
dl/.style={
decoration={
markings,
mark=between positions 0 and 1 step #1 with {
\draw[{Circle[length=4pt]}-Latex, purple, very thick] (0,0) -- (28pt, 0) node[above]{$\vec{dl}$};
}
},
postaction={decorate}
},
% Random vector
Rvec/.style={
decoration={
markings,
mark=between positions 0 and 1 step #1 with {
\draw[{Circle[length=4pt]}-Latex, black, very thick]
(0,0) -- (rand*360:28pt) node[above]{$\vec{H}$};
}
},
postaction={decorate}
}
}
\begin{document}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, dl=0.2] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, Rvec=0.2] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\begin{tikzpicture}[scale=1.3]
\draw[thick, red, Rvec=0.2,save path=\tmpath] (0,1) to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\path[dl=0.2,use path=\tmpath];
\end{tikzpicture}
\end{document}