
環境内でコンマ区切りのリストを循環処理しようとしています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
私は、orexpl3
の癖や変数名に関する問題を回避する、ベースドソリューションを提案できます。\edef
\def
には、\xloop
オプションの引数 (開始点、デフォルトは 1)、終了点の必須引数、および実行するコードがあり、#1
ループ内の現在の値を参照します。必要に応じて、ステップの追加のオプション引数を簡単に追加できます。
このコマンドは\listdefine
説明を要しません。コマンドは\listextract
要求された項目をリストから抽出します (インデックスは 1 から始まります)。2 番目の引数では\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}