# Commands delimited by \{, \|, \langle, etc

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:

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}

• What is the d in your commands for? 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? Feb 24, 2020 at 19:49
• The documentation says d⟨char1⟩⟨char2⟩ and control sequences are not really supported. Feb 24, 2020 at 20:20
• Does the provided answer solve the problem? Jan 25 at 4:34

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.