LaTeX中的pgffor
包允许遍历包含"listlike“信息的命令。正如pgf
manual的第88节所提到的,pgffor
包还允许同时迭代两组“类似列表”的信息,有点像python
的zip
函数。
\documentclass{article}
\newcommand\iterable{
item1/description1,
item2/description2,
item3/description3}
\usepackage{pgffor}
\begin{document}
\foreach \i/\q in \iterable{
\noindent \i \q\\
}
\end{document}
上面的代码将同时迭代由正斜杠字符/
分隔的\iterable
中的项。但是,如果我有大量不同的“列表”,那么在\iterable
命令中维护它们可能会很困难。
pgffor
是否允许将可迭代项分离到不同的命令中,以便我可以更好地跟踪需要迭代的项?例如:
\newcommand\iterable1{item1,item2,item3}
\newcommand\iterable2{description1, description2, description3}
发布于 2021-10-09 15:53:51
可能不是最快的解决方案,但您可以遍历这两个列表:
\documentclass{article}
\newcommand\iterablea{item1,item2,item3}
\newcommand\iterableb{description1, description2, description3}
\usepackage{pgffor}
\begin{document}
\foreach \i [count=\xi] in \iterablea {
\foreach \q [count=\xq] in \iterableb {
\ifnum\xi=\xq
\noindent \i{} \q\par
\fi
}}
\end{document}
https://stackoverflow.com/questions/69508212
复制相似问题