Tikz-knots:在 foreach 迴圈中定義單獨的鏈

Tikz-knots:在 foreach 迴圈中定義單獨的鏈

我正在尋找有關foreach使用該knots包的循環的解決方案。我發現定義一個 foreach 循環並在內部使用鏈不會給出任何輸出,除非我使用繪製選項。但沒有識別出任何結,因為紅線似乎沒有 ID。看左邊的兩張圖。

\foreach \x in {2,3,4} {
\strand [draw,red,->] (1,\x) -- (4,\x); % instead of [red,->]
}

重新安排循環幾乎可以得到所需的輸出。但該選項\strand [red,->]並未執行每條鏈的所有選項。所有的線都是紅色的,但只有一根的尖端有一個箭頭。使用草稿模式,您可以看到僅建立了一條鏈,這在查看循環的定義時是有意義的。看右邊的兩張圖。

所以我的問題是如何在 foreach 循環中建立單獨的鏈?

\documentclass[tikz,border=5mm]{standalone}
%\documentclass[convert={density=1200,size=4320x3200,outext=.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{knots}
%
\begin{document}
\begin{tikzpicture}[scale=1.0,>=latex']
%
\draw[fill=white] (0,0) rectangle (5,6);
%
\begin{knot}[ %draft mode=crossings,
clip width=5,
clip radius=6pt]
%
\strand [red,->]
    \foreach \x in {2,3,4} {
    (1,\x) coordinate (w\x) -- coordinate (e\x)(4,\x)
    };
%
\strand [thick,->] (2,1) -- (2,5);
\strand [thick,->] (3,1) -- (3,5);
%\flipcrossings {2}
\end{knot}
%
\end{tikzpicture}
\end{document}

上層循環(非草稿/草稿模式)--從完整程式碼循環(非草稿/草稿模式)

答案1

您可以使用不啟動群組的循環。

\documentclass[tikz,border=5mm]{standalone}
%\documentclass[convert={density=1200,size=4320x3200,outext=.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{knots}
%
\begin{document}
\begin{tikzpicture}[scale=1.0,>=latex']
%
\draw[fill=white] (0,0) rectangle (5,6);
%
\begin{knot}[ %draft mode=crossings,
clip width=5,
clip radius=6pt]
%
\edef\x{1}
\loop
\edef\x{\the\numexpr\x+1}
\strand [red,->]
    (1,\x) coordinate (w\x) -- coordinate (e\x)(4,\x);
\ifnum\x<4\repeat
%
\strand [thick,->] (2,1) -- (2,5);
\strand [thick,->] (3,1) -- (3,5);
%\flipcrossings {2}
\end{knot}
%
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容