4

I'd like to make a \NewDocumentCommand using the s argument, but with + or - or ... instead of * and then be able to test witch special character was used.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\foo}{%
    s}{%
    \IfBooleanTF{#1}{%
    with boolean
    % here I'd like to test witch special character wos used
    }{%
    without boolean}%
    }

\begin{document}

\foo    % works

\foo*   % works

\foo+   % doesn't work, but I would like ;-)

\end{document}
  • 6
    \NewDocumentCommand{\foo}{st+t-}{\IfBooleanT{#1}{star}\IfBooleanT{#2}{plus}\IfBooleanT{#3}{minus}} – clemens Nov 8 '14 at 16:49
  • Thank you. Is there the only way ? What if there is other arguments and on wants to add new special character ? – Tarass Nov 8 '14 at 17:07
  • I don't understand your last comment...? You can test for a token with the t argument. Every test counts as a new argument... – clemens Nov 8 '14 at 17:10
  • 1
    @cgnieder My point is, if there is first 2 booleans, the fisrt mandatory is #3, if I will add (an hypothetic update) a third boolean, the first madatory argument shift and others too. I asked for a mathode where all the booleans are #1 and the I test #1. If it is not possible, your answers fits me. You can write it and I'll accepte it. Thanks. – Tarass Nov 8 '14 at 17:19
  • Everything is possible :) Your requirement could be achieved using two macros where the first tests for tokens and sets some flags, say, and then calls the second macro with the normal mandatory macros. – clemens Nov 8 '14 at 17:21
1

I think another approach might be better. Added some control on the first boolean, and No-Value-like error messages.

\documentclass{article}
\usepackage{xparse,ifthen,xstring}

\NewDocumentCommand{\foo}{%
    lm}{%

\StrLen{#1}[\L]%
\ifthenelse{\L>1}{-- \#1 Too long ! -- }{%
    \ifthenelse{\equal{#1}{}}{It's empty ! }{
        \StrPosition{*+-}{#1}[\L]%
        \ifthenelse{\L=0}{-- \#1 Forbiden boolean ! -- }{%
            \ifthenelse{\equal{#1}{*}}{It's a star !}{}%
            \ifthenelse{\equal{#1}{-}}{It's a minus !}{}%
            \ifthenelse{\equal{#1}{+}}{It's a plus !}{}
        }%
    }%
}%
#2 -- #1
}

\begin{document}

\foo{bob}   % works
\foo*{bob}  % works
\foo+{bob}  % works
\foo-{bob}  % works
\foo!{bob}  % works
\foo*!{bob} % works
\foo!!{bob} % works

\end{document}
  • I didn't remember this l argument. Still, it's not perfect, for instance, if you want an optional argument before the mandatory one. – Manuel Nov 8 '14 at 20:39
  • I beg to differ with your opinion. – egreg Nov 8 '14 at 20:43
  • @cgnieder It's a banal syntax error, try any command with a star if it hasn't one. One can filter this case by \ifx#1 you stutter \else ... ;-) – Tarass Nov 8 '14 at 20:59
  • I agree with you. I was lazzy and didn't use the ifthen package. Still, the l parameter type save the situation until xparse gives a proper solution. One can imagine that s parameter type could have optional argument such as s[+-/] then one can remplace the star by any character in the list. – Tarass Nov 8 '14 at 21:14
  • 3
    @cfr It's used for grabbing whatever is between the macro and the first open brace, in order to be examined later. The usage is rather technical, but usually not limited to a single token for defining variants. – egreg Nov 8 '14 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.