NewDocumentCommand 替代了 s

NewDocumentCommand 替代了 s

我想\NewDocumentCommand使用s参数来进行,但是使用+或或-...而不是然后*能够测试使用了哪个特殊字符。

\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}

答案1

我认为另一种方法可能更好。对第一个布尔值和No-Value类似错误消息添加了一些控制。

\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}

相关内容