在循環重複環境中透過逗號分隔清單循環

在循環重複環境中透過逗號分隔清單循環

我正在嘗試在loop-repeat環境中循環使用逗號分隔的清單。但所有標籤名稱都會立即列印,並新增循環編號。

如何相應地定義變數名稱列表並將其連結到鏈?我嘗試了幾種變體,但尚未成功。

在此輸入影像描述

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

答案1

您的建議原則上可行,但存在語法錯誤。如果您想要一個字串數組,則需要將條目包裝在".然後可以讀取數組條目

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

這與您的表達式不同:您需要額外的{},並且需要使用方括號存取條目,並且第一個條目的索引為 0,因此\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}

在此輸入影像描述

答案2

我可以提出一個expl3基於解決方案,避免\edefor的怪癖\def以及有關變數名稱的問題。

其中\xloop有一個可選參數(起點,預設為1),一個強制參數為終點和要執行的程式碼,其中#1指的是循環中的目前值。如果需要,可以輕鬆新增該步驟的進一步可選參數。

該命令\listdefine應該是不言自明的;該命令\listextract從清單中提取請求的項目(索引從 1 開始);在第二個參數中\listextract可以使用算術表達式。

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

在此輸入影像描述

相關內容