2

I am curious if there is a technical reason that the token-type args of xparse must follow the correct order?

MWE

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\foo}{s t+}{---
\IfBooleanT{#1}{star }
\IfBooleanT{#2}{plus }---\par}
\begin{document}
\foo \foo* \foo+ \foo*+ \foo+*
%                        swapping order doesn't work
\end{document}
3
  • 1
    it would be more work to allow any order (and not really any clearer, and would make the behaviour of {ss} rather harder to define. Nov 5, 2021 at 21:17
  • 1
    most people think of a command like \section* as a command with name section* (rather than a command with a boolean marker following) so it would be odd to allow \section + * to be a variant of \section* Nov 5, 2021 at 21:19
  • 1
    @DavidCarlisle my thought, for example, was that + wouldn't be a modifier for \section*, but just another modififer for section. For example, * disables number and ToC entry, but + could change the style of the section text, for exampke. How ** would be handled IMO is one * is True False and two ** is True True, Just wanted to throw this out there, hopefully it brings some consideration for xparse--an incredibly useful package btw, thanks. Nov 6, 2021 at 1:19

1 Answer 1

5

You're asking for an optional * which can be followed by an optional +. The combination +* has not been envisaged in the definition, so with

\foo+*

the command \IfBooleanT{#1} returns false (hence nothing).

Do you want to allow any combination? Then you have to work harder.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\foo}{}
 {
  \peek_regex_replace_once:nn
   {% at most two characters among * and +
    (\*|\+){0,2}
   }
   {% replace with
    \c{veget_foo:n}\{\0\}
   }
 }

\cs_new_protected:Nn \veget_foo:n
 {
  \str_case:nn { #1 }
   {
    {}{nothing}
    {*}{star}
    {+}{plus}
    {*+}{star~and~plus}
    {+*}{star~and~plus}
    {**}{star*}
    {++}{plus+}
   }
 }
\ExplSyntaxOff

\begin{document}

\foo

\foo+

\foo*

\foo+*

\foo*+

\foo++

\foo**

\foo*+*

\foo++*

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .