1

I'm trying to cycle trough a comma separated list in an loop-repeat environment. But all label names are printed at once and the cycle number is added.

How can I define and link the list of variable names to the strands accordingly? I tried several variations but with no success yet.

enter image description here

\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]
%
\def\j{$unused$,$\alpha$,$\beta$,$\gamma$}
\edef\x{1}
\loop
\edef\x{\the\numexpr\x+1}
\strand [red,->]
    (1,\x) coordinate (w\x) -- coordinate (e\x)(4,\x) node[anchor=left,above,at start]{\j{\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}

2 Answers 2

1

Your proposal works, in principle, but there are syntax errors. If you want an array of strings, you need to wrap the entries in ". Then the array entries can be read off with

\pgfmathsetmacro{\mc}{{\j}[\numexpr\x-2]}

which differs from your expression: you need extra { and }, and you need to access the entries with square brackets, and the first entry has index 0, hence the \numexpr\x-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]
%
\def\j{"$\alpha$","$\beta$","$\gamma$"}
\edef\x{1}
\loop
\edef\x{\the\numexpr\x+1}
\strand [red,->]
    (1,\x) coordinate (w\x) -- coordinate (e\x)(4,\x) 
    node[anchor=left,above,at start]{\pgfmathsetmacro{\mc}{{\j}[\numexpr\x-2]}%
    \mc};
\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
  • Thank you again! You are my hero of the day!
    – Nivek
    Nov 11, 2020 at 16:37
1

I can propose an expl3 based solution that avoids quirks with \edef or \def and also issues about names of variables.

In \xloop there are an optional argument (starting point, default 1), a mandatory one for the end point and the code to execute, where #1 refers to the current value in the loop. A further optional argument for the step can be added easily, if you need it.

The command \listdefine should be self-explanatory; the command \listextract extracts the requested item from the list (index starting from 1); in the second argument to \listextract one can use arithmetic expressions.

\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{knots}

\ExplSyntaxOn
% looping through integers
\NewDocumentCommand{\xloop}{O{1}mm}
 {
  \int_step_inline:nnn { #1 } { #2 } { #3 }
 }
% define lists
\NewDocumentCommand{\listdefine}{mm}
 {
  \clist_clear_new:c { l_nivek_list_#1_clist }
  \clist_set:cn { l_nivek_list_#1_clist } { #2 }
 }
% extracting items from lists
\NewExpandableDocumentCommand{\listelement}{mm}
 {
  \clist_item:cn { l_nivek_list_#1_clist } { #2 }
 }
\ExplSyntaxOff

\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
]
%
\listdefine{j}{$\alpha$,$\beta$,$\gamma$}
\xloop[2]{4}{
  \strand [red,->]
    (1,#1) coordinate (w#1) -- coordinate (e#1)(4,#1) 
     node[anchor=left,above,at start]{\listelement{j}{#1-1}};
}
%
\strand [thick,->] (2,1) -- (2,5);
\strand [thick,->] (3,1) -- (3,5);
\end{knot}
%
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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