564

I just spotted someone use \newcommand* in an answer and realised that I'd never quite sorted out in my head what the star (asterisk) was there for.

(This one is practically impossible to search for on the internet so this is a good place to record the answer!)

Jake
  • 232,450
Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 7
    so much for a self-documenting system, huh? texdoc fail =) i thought it can do that. – Dima Aug 04 '10 at 18:14

4 Answers4

544

A bit of background first. When Knuth wrote TeX, he realised that most macros would not need to absorb more than one paragraph as an argument. As a result, a good way to test for errors such as a missing } is to forbid macros to accept paragraph tokens (either those generated by blank lines or explicit \par tokens). So he created \def for definitions which cannot take a paragraph token, and the \long prefix to allow them to:

\def\examplea#1{% #1 cannot contain \par
}
\long\def\exampleb#1{% #1 can contain \par
}

When LaTeX was written, Lamport created \newcommand as a wrapper around \def with various bits of error checking. He found that paragraph tokens can pop up in odd places. So he decided that \newcommand would be 'long'. When the LaTeX team took over for LaTeX2e, they decided that you would need to add the modifier * to prevent this:

\newcommand{\examplea}[1]{% #1 can contain \par
}
\newcommand*{\exampleb}[1]{% #1 cannot contain \par
}

Most of the time, \newcommand* is the best choice as you want the error-checking that it provides. That is why examples given by experienced LaTeX users normally use this form, rather than just \newcommand.

The same behaviour is seen with \newenvironment:

\newenvironment{examplea}[1]{% #1 can contain \par
}{}
\newenvironment*{exampleb}[1]{% #1 cannot contain \par
}{}

This works by defining \examplea and \endexamplea more-or-less using \newcommand, and \exampleb and \endexampleb more-or-less using \newcommand*. As a result, the 'end' macros have the same 'long' status as the 'begin' ones, even though they never take arguments. (Note that this does not affect what can go into the body of the environment, only the arguments at the start.) Environments are covered in more detail in What is the difference between \newenvironment and \newenvironment*?.

For LaTeX3, we've decided to take a somewhat hybrid approach. If you use xparse for defining document commands, they are only 'long' if you ask for it:

\NewDocumentCommand\examplea{m}{% #1 cannot contain \par
}
\NewDocumentCommand\examplab{+m}{% #1 can contain \par
}

(We've decided on + to represent a long argument). On the other hand, for internal use it is normally best to accept paragraph tokens, and to leave the filtering to the user interface level.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 44
    So what’s the common/best practice for macros without arguments? – Konrad Rudolph Aug 05 '10 at 09:41
  • 35
    A very good question. I favour making macros long only when needed, so use \newcommand*/\def for macros with no arguments. However, it does not matter for the application of the macro. One place it does matter is when you do an \ifx test: identical macros with no arguments but where one is long and the other is not do not evaluate to 'identical' as far a TeX is concerned. – Joseph Wright Aug 05 '10 at 10:00
  • Thanks. But I’ve just noticed that it cannot (easily?) be used with \WithSuffix or \expandafter. :-( Or is there an obvious way around this? – Konrad Rudolph Aug 05 '10 at 10:55
  • 1
    I don't know about \WithSuffix (I'd use xparse instead), but \expandafter\newcommand\expandafter*\csname some-name\endcsname{} works fine. Remember that we go past one token with \expandafter. – Joseph Wright Aug 05 '10 at 11:07
  • Thanks. For the record, to make it work with \WithSuffix, it’s sufficient to put the whole defining command in braces, i.e. {\newcommand*}. As for xparse, one of these days I’ll learn its usage but for now I’m still struggling with the basics of TeX. – Konrad Rudolph Aug 06 '10 at 07:26
  • 3
    We're hoping that xparse means most people have less basic TeX to learn :-) – Joseph Wright Aug 06 '10 at 11:24
  • 1
    The starred variant of \newcommand wasn't introduced by Lamport but by the LaTeX2e team with the LaTeX2e release 1994/12/01. (see usrguide) –  May 18 '13 at 07:27
  • 1
    In LuaMetaTeX, Hans completely removed the code path for non-\long macros and made \long a no-op. – Henri Menke Dec 09 '19 at 02:05
89

According to the 2nd Edition of the LaTeX Companion: (p. 846):

[\newcommand*] defines a [command], that is not, in TeX terms, long. This means that the newly defined command does not accept empty lines or \par.

This is also the default behaviour of \def, while the unstarred version is actually equivalent to \long\def.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
34

From here: "Using the starred version of \newcommand means that the arguments of the defined command cannot contain a blank line or \par. This makes it a lot easier to spot runaway arguments."

hpesoj626
  • 17,282
Jukka Suomela
  • 20,795
  • 13
  • 74
  • 91
20

In the Latex2e for authors (usrguide.pdf) I found section 3.4 relating about all "defin-y" commands:

All the above five ‘defining commands’ now have *-forms that are usually the better form to use when defining commands with arguments, unless any of these arguments is intended to contain whole paragraphs of text. Moreover, if you ever do find yourself needing to use the non-star form then you should ask whether that argument would not better be treated as the contents of a suitably defined environment.

Dima
  • 12,427
  • 8
  • 40
  • 53