0

This question is similar to this one but not exactly the same.

The following "not doing what I need" MWE comes from this answer.

I have not found how to make it evoluate such as to iterate over non overlaping couples.

IMPORTANT ! The number of arguments separated by pipes will allways be odd.

\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\deco{?}

\begin{document}

\multiapplycouple\deco{ 1 } % [[1]]

\multiapplycouple\deco{ 1 | 2 | 3 } % [[1]] (2-3)

\multiapplycouple\deco{ 1 | 2 | 3 | 4 | 5 } % [[1]] (2-3) (4-5)

\end{document}

projetmbc
  • 13,315
  • Wouldn't it be better if you explain *what* you expect the macros do? – egreg Aug 23 '20 at 14:08
  • @egreg One concrete use is for typing continued fractions. The expected output inside the MWE is easy to turn to this concrete use case. – projetmbc Aug 23 '20 at 14:09
  • And what's the reason for requiring expandability, since you just need to typeset? – egreg Aug 23 '20 at 14:10
  • You are right. Here the expandability is not useful but this could be the case in other situations. – projetmbc Aug 23 '20 at 14:11

1 Answers1

1

xparse can define many command signatures that are generalisations of standard LaTeX2e forms but at some level you are essentially writing a parser and need to drop lower. You can probably do what you want by turning the | separated list into an l3 sequence but this just parses it directly

enter image description here

\documentclass{article}

\def\deco#1#2{(#1-#2)}

\def\multiapplycouple#1#2{$\xmultiapplycouple#1#2|\relax|\relax|$} \def\xmultiapplycouple#1#2|{[[#2]]\xxmultiapplycouple#1} \def\xxmultiapplycouple#1#2|#3|{% \ifx\relax#2\else #1{#2}{#3}% \expandafter\xxmultiapplycouple\expandafter#1\fi}

\begin{document}

\multiapplycouple\deco{ 1 } % [[1]]

\multiapplycouple\deco{ 1 | 2 | 3 } % [[1]] (2-3)

\multiapplycouple\deco{ 1 | 2 | 3 | 4 | 5 } % [[1]] (2-3) (4-5)

\end{document}

David Carlisle
  • 757,742