
我正在使用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 的頻率kZ想它位於位置 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 處的「真實」箭頭被十字線遮住。