\foreach 中的 Latex 宏列表

\foreach 中的 Latex 宏列表

我有以下代码,其中我定义了两个单独的列表。我希望“foreach”从这两个单独的列表中选取项目。此问题已更新,以明确“AA”和“BB”也需要作为输出。

\def\mylistAA{11/110,22/220}
\def\mylistBB{111/1110,222/2220}

\foreach \t/\l in{AA/\mylistAA, BB/\mylistBB} {
    \foreach \x/\y in \l {
        this is \x and \y  from list \t \\
    }
}

我希望它能产生:

this is 11 and 110 from list AA
this is 22 and 220 from list AA
this is 111 and 1110 from list BB
this is 222 and 2220 from list BB

相反,我得到:

this is 11/110,22/220 and 11/110,22/220 from list AA
this is 111/1110,222/2220 and 111/1110,222/2220 from list BB

您能帮我重新定义以便我完成我需要的吗?谢谢!

答案1

所需的输出可以通过以下方式生成

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\def\mylistAA{11/110,22/220}
\def\mylistBB{111/1110,222/2220}

\foreach \x/\y in \mylistAA
{this is \x\ and \y  \par}
\foreach \x/\y in \mylistBB
{this is \x\ and \y  \par}
\end{document}

在此处输入图片描述

这可以形成一个嵌套循环。

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\def\mylistAA{11/110,22/220}
\def\mylistBB{111/1110,222/2220}

\foreach \lst in {\mylistAA,\mylistBB}
{\edef\Lst{\lst}
\foreach \x/\y in \Lst
{this is \x\ and \y  \par}}
\end{document}

在此处输入图片描述

我省略了AA/and ,BB/我不明白它的用途。(这个\edef技巧在这里似乎很重要。)

答案2

对 marmot 提出的解决方案进行了简单的扩展,以包含宏的另一部分(AA 和 BB)。下面我发布了基于上述解决方案的有效版本:

\def\mylistAA{11/110,22/220}
\def\mylistBB{111/1110,222/2220}

\foreach \t/\l in{AA/\mylistAA, BB/\mylistBB} {
    \edef\Lst{\l}
    \foreach \x/\y in \Lst {
        this is \x and \y  from list \t \\
    }
}

相关内容