我正在使用knots
tikzlibrary 绘制一些结,并想为结定位,即添加箭头。我使用一种称为“arrowat”的装饰样式,它会在所需位置插入箭头(例如,\draw[arrowat=0.5]
将在结果线的中间产生一个箭头)。它的定义如下
\tikzset{%
arrowat/.style={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\arrow[xshift=2pt]{>}}}}
}
}
但是,当我尝试在结上使用这种样式时,会出现一些问题:在某些位置,一切都很好,但如果我尝试在其他某些位置画箭头,就会出现多个箭头(以及线条中的间隙)。下面是一个 MWE,显示了两种情况,一种是一切正常(左),另一种是一切不正常(右)。我的问题是:我该如何解决这个问题,或者更确切地说,如何防止它发生?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,knots}
\tikzset{%
arrowat/.style={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\arrow[xshift=2pt]{>}}}}
}
}
\begin{document}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick, arrowat=0.8] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick, arrowat=0.5] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\end{document}
答案1
我没有真正的(即优雅的)解决方案。只是好奇地想部分了解发生了什么,并检查了 Ti 的频率钾是想它位于位置 0.5。在此示例中,这是 10 次,您可以通过取消注释来验证这一点\typeout{\number\value{arrowcount}}
。然后可以添加一个开关,指示箭头真正应该绘制在哪个位置。相应的样式称为conditional arrow at
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,knots}
\newcounter{arrowcount}
\tikzset{%
arrowat/.style={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\arrow[xshift=2pt]{>}}}}
},
conditional arrow at/.style 2 args={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\stepcounter{arrowcount}%
%\typeout{\number\value{arrowcount}}%
\ifnum\value{arrowcount}=#2
\arrow[xshift=2pt]{>}%
\fi}}}
},
}
\begin{document}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick, arrowat=0.8] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick,conditional arrow at={0.5}{6}] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\end{document}
使用钥匙后,情况会改善 2 倍(正如预期的那样)only when rendering
。然后您“只”有 5 个插槽,每个插槽实际上都指向一个箭头。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,knots}
\newcounter{arrowcount}
\tikzset{%
arrowat/.style={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\arrow[xshift=2pt]{>}}}}
},
conditional arrow at/.style 2 args={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\stepcounter{arrowcount}%
\typeout{\number\value{arrowcount}}%
\ifnum\value{arrowcount}=#2
\arrow[xshift=2pt]{>}%
\fi}}}
},
}
\begin{document}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick, arrowat=0.8] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick,only when rendering/.style={conditional arrow at={0.5}{3}}] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\end{document}
所以问题似乎在于路径被分解成段。这告诉我们,我们可以将箭头附加到不可见的路径上。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,knots}
\newcounter{arrowcount}
\tikzset{%
arrowat/.style={%
postaction={decorate,decoration={
markings,
mark=at position #1 with {\arrow[xshift=2pt,opacity=1]{>}}}}
},
}
\begin{document}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\strand [thick, arrowat=0.8] (0,0)
to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0);
\end{knot}
\end{tikzpicture}
\begin{tikzpicture}
\begin{knot} [consider self intersections, ignore endpoint intersections=false, clip width=7, flip crossing=3]
\def\mypath{(0,0) to [out=180, in=270] (-1,1)
to [out=90, in=180] (1,2.5)
to [out=0, in=90] (3,1)
to [out=270, in=0] (2,0)
to [out=180, in=270] (-0.2,2.5)
to [out=90, in=180] (1,3.8)
to [out=0, in=90] (2.2,2.5)
to [out=270, in=0] (0,0)}
\draw[thick,opacity=0,arrowat=0.55] \mypath;
\strand [thick] \mypath;
\end{knot}
\end{tikzpicture}
\end{document}
这可能是这些提议中最优雅的,并且还揭示了 0.5 处的“真实”箭头被交叉所遮挡。