6

The documentation of xparse states (emphasis mine):

r Given as r⟨token1⟩⟨token2⟩, this denotes a “required” delimited argument, where the delimiters are ⟨token1⟩ and ⟨token2⟩. If the opening delimiter ⟨token1⟩ is missing, the default marker -NoValue- will be inserted after a suitable error.
R Given as R⟨token1⟩⟨token2⟩{⟨default⟩}, this is a “required” delimited argument as for r, but it has a user-definable recovery ⟨default⟩ instead of -NoValue-.

However in both cases, an error is produced if the parameter is not specified. And I can't see any difference in the error message between r and R.

  • How exactly does this default marker -NoValue- intervene?
  • And what is the purpose of this additional {⟨default⟩}? Any use case?

Is it to avoid additional errors where the corresponding parameter #N is used? Or (for R) somehow document the parameter with that default value? Any notable difference between r and R in the end?

Note: I already know about the related optional types d/D.

2 Answers 2

5

Let's do a test:

\documentclass{article}

\NewDocumentCommand{\test}{r<>}{?#1?}

\begin{document}

\test<X>

\test X

\end{document}

Upon running LaTeX on it we get

! LaTeX cmd Error: Required argument missing for command '\test'.

For immediate help type H <return>.
 ...

l.9 \test X

and the output is

enter image description here

You see that, in order to recover from the error, LaTeX inserts <-NoValue> after \test, so processing can continue without a possible further error from a missing >. The output would be invalid anyway.

Second test.

\documentclass{article}

\NewDocumentCommand{\test}{R<>{what}}{?#1?}

\begin{document}

\test<X>

\test X

\end{document}

The error message is the same, but now the output is

enter image description here

Instead of “what” you can use something different that will catch your attention in the PDF file. Better than -NoValue-. This might also be useful if you do something more complicated with the argument, so you can perhaps avoid further error messages.

0

Thanks to egreg for the very detailed answer!
I'd like to add another interesting usecase I just found for a required argument default marker/value.

Let's assume I want to split one argument and be able to pass 1, 2 or 3 values :

\test{1, 2, 3}
\test{1, 2}
\test{1}

Using:

\NewDocumentCommand{\test}{ >{ \SplitArgument{2}{,} } m }{\impl #1}

\impl will respectivelly receive

\impl{1}{2}{3}
\impl{1}{2}{-NoValue-}
\impl{1}{-NoValue-}{-NoValue-}

optional arguments will fail here as the arguments are passed no matter what, with {}.

\NewDocumentCommand\impl{m m m}{%
    \#1 = #1,
    \#2 = \IfValueTF{#2}{#2}{default},
    \#3 = \IfValueTF{#3}{#3}{default}
}

outputs:

#1 = 1, #2 = 2, #3 = 3
#1 = 1, #2 = 2, #3 = default
#1 = 1, #2 = default, #3 = default

You must log in to answer this question.

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