1

This question already has an answer here:

Maybe my question is too broad, but I'd really like to know: what is best to use (and why) between xparse command definition (e.g. \NewDocumentCommand) and LaTeX "standard" definitions (e.g. \newcommand)?

When I started with LaTeX I learnt that LaTeX definitions are more convenient than TeX plain definitions (e.g. \def) for many reasons (e.g. not having the risk to inadvertently override some existing command and such).

I'm wondering if the new xparse definitions are better; for instance sometimes I've seen that the syntax to define a command is more clear (to me) and seems very powerful.

Are there any drawbacks? Thanks

marked as duplicate by Werner macros Nov 5 '15 at 11:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The xparse commands are regarded as stable, but as long as you do not use \DeclareExpandableDocumentCommand the \NewDocumentCommand aren't expandable -- that's a drawback – user31729 Nov 5 '15 at 10:42
  • @ChristianHupfer \newcommand defined commands are not expandable either if they have optional arguments. (I see you had the same statement in your answer:-) – David Carlisle Nov 5 '15 at 10:51
  • @DavidCarlisle: Yes, I answered rather than to use a short comment. – user31729 Nov 5 '15 at 10:52
6

Caution: Opinion based answer ahead ;-)

The command argument specification is a very powerful feature of xparse and its \NewDocumentCommand (and the other ones, like \DeclareDocumentCommand etc.) macro. (\NewDocumentCommand stands here for the whole bundle of such commands!)

The question if they are better (safer?) than \newcommand - defined macros is difficult to answer and opinion - based.

  • It's easier to cope with multiple optional arguments by using \IfValueTF etc.
  • It's easier to provide other types of optional etc. arguments

  • Concerning the undelimited argument facilities \NewDocumentCommand is definitely more of a \def from plain TeX than \newcommand

  • Since command definition checking is done as well, xparse commands are safer compared to \def concerning redefinition of macros.
  • Macros defined by xparse commands allow for starred versions, which is very convenient for redefinition of \section* etc.
  • In addition: Environments defined by \NewDocumentEnvironment can use the arguments even in the end code - section, contrary to \newenvironment

However, a command defined with \NewDocumentCommand isn't fully expandable and can not be used where a full expansion is needed, as soon as optional arguments are involved, so this might be a drawback. But in many cases, it's possible to fall back to a simple \newcommand without optional argument then.

For such cases, \DeclareExpandableDocumentCommand has to be used.

I started with \def a long time ago, then improved with \newcommand and nowadays use \NewDocumentCommand for most of my personal documents and of course, for answers here.

In conjunction with expl3 xparse (which is in fact a expl3 application package of course) is very powerful and recommendable.

  • 2
    If there are no optional arguments it'll be expandable anyway, so the extra feature is that it is possible to define a command that looks for optional arguments via expansion – David Carlisle Nov 5 '15 at 10:52
  • Yes, I was confused for a moment: I frequently use optional arguments so the non-optional argument \NewDocumentCommand macro is rarely used by me – user31729 Nov 5 '15 at 10:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.