[Tex/LaTex] the difference between \NewDocumentCommand and \DeclareDocumentCommand

latex3xparse

These commands are part of the LaTeX3 Project xparsepackage. This question is to satisfy curiosity…

From xparse documentation released on 25 Nov 2014:

\DeclareDocumentCommand will always create the new definition, irrespective of any existing with the same name.

\NewDocumentCommand will issue an error if has already been defined.

Is this the equivalent of \def and \newcommand?

In what situation would I need this?

Best Answer

Is this the equivalent of \def and \newcommand?

Not quite: \DeclareDocumentCommand has the same syntax as \NewDocumentCommand, unlike \def with respect to \newcommand. But the documentation seems clear about it: with \DeclareDocumentCommand, there is no check that the command name you give as argument already has a definition.

Don't forget there's also \RenewDocumentCommand.

In what situation would I need this?

I know a few ones, but I usually recommend using \NewDocumentCommand. Don't use \DeclareDocumentCommand. I hope the team will also provide \NewExpandableDocumentCommand.

What's the usage of \DeclareDocumentCommand? Every time you have to do \let\foo\relax and \DeclareRobustCommand\foo{...}, then it's time to \DeclareDocumentCommand. If you don't know what I mean, then use \NewDocumentCommand and live happier.

Related Question