2

The xparse package offers the possibility to define commands delimited by single special start/end character. This can be very useful, for instance it is used a lot in the physics package. I was wondering if it would be possible to also define commands delimited by other structures such as \{, \| or \langle. Curiously, it seems to almost work out of the box:

enter image description here

It appears that the braces get recognized correctly, however some additional characters are inserted. Why is this happening & is there a way to patch it?

\documentclass[12pt]{standalone}
\usepackage{xparse}
\NewDocumentCommand{\testA}{d\{\}}{ \IfNoValueTF{#1}{error}{(#1)} }
\NewDocumentCommand{\testB}{d\|\|}{ \IfNoValueTF{#1}{error}{(#1)} }
\NewDocumentCommand{\testC}{d\langle\rangle}{ \IfNoValueTF{#1}{error}{(#1)} }
\begin{document}
$ \testA\{X\} \testB\|X\| \testC\langle X\rangle $
\end{document}
5
  • What is the d in your commands for?
    – Bernard
    Feb 24, 2020 at 19:29
  • @Bernard d means an argument delimited by two tokens in xparse Feb 24, 2020 at 19:42
  • Oh! yes. I had forgotten this way to specify arguments in xparse. But what do you want to really produce with such unusual delimiters?
    – Bernard
    Feb 24, 2020 at 19:49
  • 1
    The documentation says d⟨char1⟩⟨char2⟩ and control sequences are not really supported.
    – egreg
    Feb 24, 2020 at 20:20
  • Does the provided answer solve the problem? Jan 25 at 4:34

1 Answer 1

3

The argument specifiers d and D should be followed by two characters, not arbitrary tokens.

If you enable a check that should always be used when testing programs, like

\documentclass[12pt]{standalone}
\usepackage[log-declarations]{xparse}
\NewDocumentCommand{\testA}{d\{\}}{ \IfNoValueTF{#1}{error}{(#1)} }
\NewDocumentCommand{\testB}{d\|\|}{ \IfNoValueTF{#1}{error}{(#1)} }
\NewDocumentCommand{\testC}{d\langle\rangle}{ \IfNoValueTF{#1}{error}{(#1)} }
\begin{document}
$ \testA\{X\} \testB\|X\| \testC\langle X\rangle $
\end{document}

you get the warnings

LaTeX3 Warning: Argument delimiter '\{' for the command '\testA' should be a
(LaTeX3)        single character.

LaTeX3 Warning: Argument delimiter '\}' for the command '\testA' should be a
(LaTeX3)        single character.

LaTeX3 Warning: Argument delimiter '\|' for the command '\testB' should be a
(LaTeX3)        single character.

LaTeX3 Warning: Argument delimiter '\|' for the command '\testB' should be a
(LaTeX3)        single character.

LaTeX3 Warning: Argument delimiter '\langle ' for the command '\testC' should
(LaTeX3)        be a single character.

LaTeX3 Warning: Argument delimiter '\rangle ' for the command '\testC' should
(LaTeX3)        be a single character.

Possibly these should be actual errors. The behavior in these cases is not predictable.

See https://github.com/latex3/latex3/issues/368

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.