循环遍历具有不同于逗号的分隔符的列表

循环遍历具有不同于逗号的分隔符的列表

我正在尝试迭代一个列表,其分隔符不是逗号。不使用逗号作为分隔符的原因是它们是有效负载的一部分。有没有办法迭代分隔符为分号(或任何其他分隔符)的列表?

到目前为止,我已经尝试过

\documentclass{article}

\makeatletter
\newcommand{\ls}[1]{\@for\tmp:=#1 \do{\tmp}}
\makeatother

\begin{document}

\ls{(a,b);(c,d)}

\end{document}

因此,我想循环遍历用分号分隔的元组,即(a,b)和(c,d)。

编辑:我也试图寻找一些相关的文档\@for,但是没有找到任何内容。

答案1

etoolbox许多有趣的功能,包括制作列表解析器。另请参阅无星号版本。

\documentclass[a4paper]{memoir}
\usepackage{etoolbox}
\begin{document}
\newcommand\handler[1]{item: #1\par}
\DeclareListParser*\forsemicolonlist{;}
\forsemicolonlist\handler{(a,b);(c,d);(e,f)}
\end{document}

答案2

例如,您可以使用以下代码:

\def\ls#1{\lsA#1;;}
\def\lsA#1;{\ifx;#1;\else \dosomething{#1}\expandafter\lsA\fi}
\def\dosomething#1{\message{I am doing something with #1}}

\ls{(a,b);(c,d);(e,f)}

相关内容