TransWikia.com

expl3 macro to expandably iterate on a list and apply a macro on each pair of items

TeX - LaTeX Asked on February 4, 2021

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}
newcommandapplycouplemacro{}

% #1 : macro to apply
% #2 : multi-args
newcommandapplycouple[2]{
    setcounter{applycoupleposition}{0}
    renewcommandapplycouplemacro{#1}
    applycouplerecu#2|nil
    renewcommandapplycouplemacro{}
}


defapplycouplerecu#1|#2|#3nil{
    stepcounter{applycoupleposition}
    applycouplemacro{#1}{#2}
    ifrelaxdetokenize{#3}relaxelse
        applycouplerecu#2|#3nil
    fi
}

newcommanddecocouple[2]{%
    (#1)[#2]%
}

newcommandmultiapplycouple[1]{%
    applycouple{decocouple}{#1}
} 

begin{document}

multiapplycouple{1|12|123|1234}

end{document}

One Answer

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

newcommanddecocouple[3]{Item #1: (#2)[#3]quad}

begin{document}
multiapplycoupledecocouple{ 1 | 12 | 123 | 1234 }
end{document}

Answered by Phelype Oleinik on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP