0

I'm looking for a solution concerning a foreach-loop using the knots package. I figured out that defining a foreach loop and using strands inside doesn't give any output unless I'm using the draw option. But no knots are recognised since the red lines don't seem to have an id. Look at the two pictures on the left.

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

Rearranging the loop gives nearly the output needed. But the option \strand [red,->] doesn't execute all options to every strand. All strands are red, but only one of them has an arrow at the tip. Using the draft mode, you can see that there is only one strand created, which makes sense looking at the definition of the loop. Look at the two pictures on the right.

So my question is how to create separate strands within a foreach loop?

\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}

Upper loop (non-draft/draft mode) -- Loop from full code (non-draft/draft mode)

1 Answer 1

1

You can use a loop that does not start groups.

\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}

enter image description here

1
  • That's gorgeous! Thank you very much for your answer! It works like charm :)
    – Nivek
    Nov 11, 2020 at 2:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .