xparse:SplitList 和列表的列表

xparse:SplitList 和列表的列表

请考虑以下示例:

\documentclass{article}
\usepackage{booktabs,xparse}
\ExplSyntaxOn%
\NewDocumentCommand\GetListMember{s m m}{% https://tex.stackexchange.com/a/468450
  \IfBooleanTF{ #1 }{% * means control sequence
    \clist_item:Nn   #2   { #3 } }{%
    \clist_item:nn { #2 } { #3 } }}%
\NewDocumentCommand\ConcatArguments{>{\SplitList{,}} m } { #1 }
\ExplSyntaxOff%
\begin{document}
\newcommand\testA{{a,b},{c,d},{e,f}}
\begin{tabular}{lll}
\toprule
no.& reality                                                              & expectation\\
\midrule
1  & \ConcatArguments{{a,b},{c,d},{e,f}}                                  & a,bc,de,f\\
2  & \ConcatArguments{\testA}                                             & a,bc,de,f\\
% https://tex.stackexchange.com/a/323873 :
2a & \expandafter\ConcatArguments\expandafter{\testA}                     & a,bc,de,f\\
3  & \GetListMember*{\testA}{2}                                           & c,d\\
4  & \ConcatArguments{\GetListMember*{\testA}{2}}                         & cd\\
4a & \expandafter\ConcatArguments\expandafter{\GetListMember*{\testA}{2}} & cd\\
\bottomrule
\end{tabular}
\end{document}

示例输出

我怎样才能实现第 4 点中的期望?(我试图减少问题,但不确定如何正确应用2a

答案1

我不确定目标是什么。不过,你需要启用完全扩展。

\documentclass{article}
\usepackage{booktabs,xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand\GetListMember{s m m}
 {
  \IfBooleanTF{ #1 }
   {% * means control sequence
    \clist_item:Nn   #2   { #3 }
   }
   {
    \clist_item:nn { #2 } { #3 }
   }
 }

\NewDocumentCommand\ConcatArguments { m }
 {
  \cameron_concat:e { #1 }
 }
\cs_new:Nn \cameron_concat:n { \clist_map_function:nN { #1 } \use:n }
\cs_generate_variant:Nn \cameron_concat:n { e }

\ExplSyntaxOff

\begin{document}

\newcommand\testA{{a,b},{c,d},{e,f}}

\begin{tabular}{lll}
\toprule
no.& reality                                      & expectation\\
\midrule
1  & \ConcatArguments{{a,b},{c,d},{e,f}}          & a,bc,de,f\\
2  & \ConcatArguments{\testA}                     & a,bc,de,f\\
3  & \GetListMember*{\testA}{2}                   & c,d\\
4  & \ConcatArguments{\GetListMember*{\testA}{2}} & cd\\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

答案2

一种简单的方法listofitems

\documentclass{article}
\usepackage{listofitems}
\newcommand\ConcatArguments[2][]{%
  \readlist\mylist{#2}%
  \ifx\relax#1\relax
    \foreachitem\z\in\mylist{\z}%
  \else
    \readlist\myitems{#1}%
    \foreachitem\z\in\myitems{\mylist[\z]}%
  \fi
}
\begin{document}
\newcommand\testA{{a,b},{c,d},{e,f}}

\ConcatArguments{{a,b},{c,d},{e,f}}\par
\ConcatArguments{\testA}\par
\ConcatArguments[2]{\testA}\par
\ConcatArguments[2,3]{\testA}\par
\ConcatArguments[1,3]{\testA}
\end{document}

在此处输入图片描述

相关内容