2

Consider the following example:

\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}

example output

How can I achieve my expectation in no. 4? (I tried to reduce the problem but am unsure how to correctly apply what worked in 2a.)

2 Answers2

3

I'm not sure what's the target. However, you need to enable full expansion.

\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}

enter image description here

egreg
  • 1,121,712
  • Thanks for the comment, I almost missed Expandable. Is it possible to avoid introducing cameron_concat? (Apologies for the contrived MWE. I tried to keep it simple.) – Cameron Hudson May 25 '21 at 12:16
  • @CameronHudson No, \SplitList makes the command not expandable. – egreg May 25 '21 at 12:36
1

A simple approach with 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}

enter image description here

  • Pardon my ignorance, but how does this help with no. 4? – Cameron Hudson May 25 '21 at 12:19
  • @CameronHudson Well, it is not expandable, if that is what you are getting at. Nonetheless, it allows particular entries to be extracted and concatenated, which is what I thought you were concerned with in item 4 – Steven B. Segletes May 25 '21 at 12:25