74

Are there compelling reasons to stick with \newcommand or should one always use \NewDocumentCommand instead, even when not using any of the additional features offered by xparse?

In addition and more specifically,

  • If I already require xparse to define one of the macros of my document/package/class, is it a good idea to use \NewDocumentCommand for all my other macro definitions?

  • Will \newcommand eventually get deprecated/discouraged in favor of \NewDocumentCommand with LaTeX3?

4
  • You should use \newcommand which will never be deprecated. \NewDocumentCommand is from the experimental LaTeX3 and more a command for package writers.
    – user2478
    Commented Feb 14, 2013 at 7:25
  • 2
    @Herbert I have to disagree here. yes \DeclareNewCommand is from the latex3 code in the version that runs on top of current LaTeX but the days that we classified all of that as experimental is long gone. These days there is a large stable part (kernel code of expl3 and some packages of which xparse is one). Also xparse is "user interface" for LaTeX2e-like syntax. Commented Feb 14, 2013 at 22:17
  • @Frank: then you should change the description of what LaTeX3 will be ;-)
    – user2478
    Commented Feb 15, 2013 at 6:43
  • 1
    NewDocumentCommand is integrated into all new latex kernels for years as of 2022.
    – ivo Welch
    Commented Apr 2, 2022 at 22:29

2 Answers 2

57

\NewDocumentCommand is part of xparse, which is 'LaTeX3-in-2e' code. The team have marked this part as 'stable', meaning that we will maintain this code for use with LaTeX2e in an ongoing sense. As such, there is no danger of \NewDocumentCommand vanishing.

Conceptually, \NewDocumentCommand is intended for 'package authors' to define commands, while \newcommand is rather more fluid as it is also used to create variables (macros used as storage). This makes \newcommand a better choice if what you are defining is a variable: we do not have a 'document level' LaTeX3 command for this concept at present. So the decision will depend to some extent on how you see the macros you are defining (commands with no arguments and variables are pretty hard to distinguish!).

Predicting the future is risky, but I think it's extremely unlikely that any stand-alone LaTeX3 format would not define \newcommand, even if the recommendation might be to use other methods. In any case, there are lots of other concepts that I suspect mean that for some arbitrary LaTeX2e document, you won't just be able to process with a stand-alone LaTeX3 format without adjustment.

4
  • Sorry to dredge up an old thread, but \NewDocumentCommand can create a command that takes no arguments, so this way would (presumeably) be preferred for things like \maketitle (not variables). Has there been any further discussion on this since February that can be referenced? Commented Jun 11, 2013 at 3:54
  • @SeanAllred I know it can, but what I mean is that 'insert a token list' can be viewed as a command with no arguments or the use of a token list variable.
    – Joseph Wright
    Commented Jun 11, 2013 at 6:14
  • Ahhh, I see. I.e. what is actually stopping something like make title from being used as, say, a stack. Gotcha. Commented Jun 11, 2013 at 11:22
  • 10
    @JosephWright is this answer still valid? The xparse package documentation dated 23rd of August, 2018 states: “The xparse package provides a high-level interface for producing document-level commands. In that way, it is intended as a replacement for the LATEX 2 ε \newcommand macro.” Commented Sep 11, 2018 at 18:44
35

To expand Joseph's answer about commands and values: Macros defined with \NewDocumentCommand are robust, they don't get expanded when e.g. moved to the .toc. This is a good thing for commands like \cite which should do something in the .toc but in general is not wanted for values which can change in a document and where you want to transport the current content to the .toc and not only the name of the command. As an example:

\documentclass{article}
\usepackage{xparse}

\begin{document}
\NewDocumentCommand\testA{}{ABC}
\newcommand\testB{ABC}

\tableofcontents

\RenewDocumentCommand\testA{}{CDE}
\renewcommand\testB{CDE}

\section{\testA, \testB}

\end{document}
2
  • Has something changed since this answer was written? Running on a recent TeXLive installation this produces a table of contents that reads "1 CDE, CDE".
    – N. Virgo
    Commented Aug 16, 2022 at 5:11
  • 1
    @N.Virgo you no longer need to load xparse, but apart from this it still works as described in the answer. On a current texlive I get i.sstatic.net/V8X4W.png. You can also try on texlive.net/run Commented Aug 16, 2022 at 7:22

You must log in to answer this question.

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