1

Can someone show me how to make the following macro (\applycouple) expandable using expl3, if it is possible?

This question is similar to this post of mine.

\documentclass{article}

\newcounter{applycoupleposition} \newcommand\applycouplemacro{}

% #1 : macro to apply % #2 : multi-args \newcommand\applycouple[2]{ \setcounter{applycoupleposition}{0} \renewcommand\applycouplemacro{#1} \applycouplerecu#2|\nil \renewcommand\applycouplemacro{} }

\def\applycouplerecu#1|#2|#3\nil{ \stepcounter{applycoupleposition} \applycouplemacro{#1}{#2} \if\relax\detokenize{#3}\relax\else \applycouplerecu#2|#3\nil \fi }

\newcommand\decocouple[2]{% (#1)[#2]% }

\newcommand\multiapplycouple[1]{% \applycouple{\decocouple}{#1} }

\begin{document}

\multiapplycouple{1|12|123|1234}

\end{document}

projetmbc
  • 13,315

1 Answers1

1

Quite similar to the implementation in my answer to your other question, but this one takes two arguments, so we need to add two \q_recursion_tail. Also another intermediary macro is used to insert a \prg_do_nothing: in the second item to avoid losing spaces and braces.

enter image description here

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand \multiapplycouple { m +m }
  { \mbc_multiapply_pair:Nn #1 {#2} }
\cs_new:Npn \mbc_multiapply_pair:Nn #1 #2
  {
    \__mbc_multiapply_pair:nNw { 0 } #1
      | \prg_do_nothing: #2 | \q_recursion_tail | \q_recursion_stop
  }
\cs_new:Npn \__mbc_multiapply_pair:nNw #1 #2 | #3 |
  { \__mbc_multiapply_pair:nNww {#1} #2 | #3 | \prg_do_nothing: }
\cs_new:Npn \__mbc_multiapply_pair:nNww #1 #2 | #3 | #4 |
  { \__mbc_multiapply_pair:oofN {#3} {#4} { \int_eval:n {#1+1} } #2 }
\cs_new:Npn \__mbc_multiapply_pair:nnnN #1 #2 #3 #4
  {
    \quark_if_recursion_tail_stop:n {#2}
    \quark_if_recursion_tail_stop:n {#1}
    \exp_not:e { \exp_not:N #4 {#3} { \tl_trim_spaces:n {#1} } { \tl_trim_spaces:n {#2} } }
    \__mbc_multiapply_pair:nNww {#3} #4 | \prg_do_nothing: #2 | \prg_do_nothing:
  }
\cs_generate_variant:Nn \__mbc_multiapply_pair:nnnN { oof }
\ExplSyntaxOff

\newcommand\decocouple[3]{Item #1: (#2)[#3]\quad}

\begin{document} \multiapplycouple\decocouple{ 1 | 12 | 123 | 1234 } \end{document}