这是代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{snakes}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{positioning}
\tikzstyle{rev} = [draw,decorate,decoration={snake,amplitude=.4mm,segment length=2mm,post length=1mm}]
\begin{document}
\begin{tikzpicture}[->,>=stealth',pics/parallel arrow/.style={code={\draw[-latex,snake] (#1) -- (-#1);}}]
\node (v) {v};
\node [below left=1cm of v] (a) {a};
\node [below right=1cm of v] (b) {b};
\draw[] (v) -- pic[sloped,rev]{parallel arrow={-0.3,-0.15}} (a);
\draw[] (v) -- pic[sloped,rev]{parallel arrow={-0.3,-0.15}} (b);
\end{tikzpicture}
\end{document}
其渲染效果如下:
{-0.3,-0.15}
这里,我对使用了相同的参数parallel arrow
。但是,一条蛇朝相反的方向走来(这就是我想要的),而另一条蛇却朝错误的方向走去。将 参数更改为 可以{+0.3,-0.15}
解决问题。
是否可以使用一个相同 pic
命令并总是看到蛇朝正确的方向走来(相反的方向)?
答案1
有三种样式 ( postaction
+ decoration
) 可用作TikZ
线段的属性。它们会生成与线段平行的反向蛇形箭头。
rArrow
接受一个参数(放置箭头的线段的一侧);它必须是 1 或 -1。它产生一个固定长度的箭头,放置在与线段的固定距离处。在上图中,它对应于黑色元素。rArrowL
与前一个相同,但需要三个参数,即侧面、标签和标签的方向必须是 1 或 0. 对应红色元素。rSArrow
接受两个参数(角度和边)。它生成一个长度可变且与线段距离可变的箭头(由角度参数控制)。在上图中,它对应于蓝色元素。
评论。 rArrow
其定义中包含一个参数\d
,该参数定义蛇箭的长度。
代码
\documentclass[11pt, border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\tikzset{%
rArrow/.style={% side = 1 or -1
postaction={%
decoration={
show path construction,
lineto code={
\tikzmath{
real \d, \a;
\d = 1.4; % length/2 of the snake arrow
\a = (-1)*(#1)*90;
{
\path (\tikzinputsegmentfirst) coordinate (I);
\path (\tikzinputsegmentlast) coordinate (F);
\draw[arrows={Latex[width=2ex, length=1ex]-},
decorate, decoration=snake]
let
\p1 = ($(F)-(I)$),
\n1 = {atan2(\y1, \x1)},
\p2 = ($(I)!.5!(F) -(\n1: \d em)$)
in
(\p2) ++(\n1+ \a: 2 ex) -- ++(\n1: 2*\d em);
};
}
}
},
decorate
}
},
rArrowL/.style n args={3}{% side = 1 or -1, label, label orientation = 0 or 1
postaction={%
decoration={
show path construction,
lineto code={
\tikzmath{
real \d, \a;
\d = 1.4;
\a = (-1)*(#1)*90;
{
\path (\tikzinputsegmentfirst) coordinate (I);
\path (\tikzinputsegmentlast) coordinate (F);
\draw[arrows={Latex[width=2ex, length=1ex]-},
decorate, decoration=snake]
let
\p1 = ($(F)-(I)$),
\n1 = {atan2(\y1, \x1)},
\p2 = ($(I)!.5!(F) -(\n1: \d em)$)
in
(\p2) ++(\n1+ \a: 2ex) -- ++(\n1: 2*\d em)
(\p2) ++(\n1+ \a: 4ex) ++(\n1: \d em)
node[rotate={\n1 +#3*180}] {\small #2};
};
}
}
},
decorate
}
},
rSArrow/.style n args={2}{% angle, side = 1 or -1
postaction={%
decoration={
show path construction,
lineto code={
\tikzmath{
real \c;
\c = .15;
{
\path (\tikzinputsegmentfirst) coordinate (I);
\path (\tikzinputsegmentlast) coordinate (F);
\draw[arrows={Latex[width=2ex]-},
decorate, decoration=snake]
($(I)!\c!-{(#2)*#1}:(F)$) -- ($(F)!\c!{(#2)*#1}:(I)$);
};
}
}
},
decorate
}
}
}
\begin{tikzpicture}
\node (v) at (0, 0) {$v$};
\node [below left=2cm of v] (a) {$a$};
\node [below right=2cm of v] (b) {$b$};
\draw[->, rArrow={1}] (v) -- (a);
\draw[->, rArrow={-1}] (v) -- (b);
\draw[red, ->, rArrowL={1}{fixed}{1}] (6, -1) -- ++(-3.6, -1.2);
\draw[red, ->, rArrowL={1}{fixed}{0}] (3, -3) -- ++(3, 1);
\draw[blue, ->, rSArrow={60}{-1}] (3, -.5) -- ++(3, 1);
\end{tikzpicture}
\end{document}