5

I am trying to stress xparse and find something I tried which is not documented anywhere. This code snippet is not compiling and I do not get why.

\documentclass{article}

\usepackage{xparse}

\DeclareDocumentCommand\mycom{gm}{%
  Optional: \IfNoValueTF{#1}{Empty}{#1}\par
  Mandatory: #2}

\begin{document}

\mycom{MAN}

\mycom{OPT}{MAN}

\end{document}

For what I read in the documentation this should produce something like

Optional: Empty
Mandatory: MAN

Optional: OPT
Mandatory: MAN

Did I read the documentation wrongly?

1
  • I know that using braces for both optional and mandatory argument is not a good practice, but since it is not explicitly forbidden by the documentation I would like to understand why it does not work. Commented Sep 11, 2015 at 22:22

1 Answer 1

3

In the first case the optional argument is found, because there is a brace; then TeX looks for the mandatory argument and the next token is a \par (generated by the blank line), which throws an error, because the mandatory argument is not “long”.

The optional argument should be the second one; it's just a matter of reversing the tests:

\documentclass{article}

\usepackage{xparse}

\DeclareDocumentCommand\mycom{mg}{%
  \IfNoValueTF{#2}
    {Mandatory: #1}
    {Optional: #1; Mandatory: #2}}

\begin{document}

\mycom{MAN}

\mycom{OPT}{MAN}

\end{document}

Don't use g, it's much easier.

This site is temporarily in read-only mode and not accepting new answers.

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