TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Inspired by a question about \let and \def, I have some that essentially all boil down to asking

What are the differences between \def and \newcommand?

In particular, I wonder

  1. Is it possible to have parameters passed to \defed commands, both optional and required?
  2. Is there a \redef command equivalent to \renewcommand?
  3. I have only ever used \newcommand -- is there any reason I should change that habit?

but I welcome comments on anything else I may not have thought of in this list!

share|improve this question
Hi Michael, could you please give the reference to the other question? Thanks! And good question! – Nils Schmidt Jul 30 '10 at 16:29
@Nils: The other question is referenced above; perhaps the formatting makes it harder to see: click anywhere on "\let and \def". – ShreevatsaR Jul 30 '10 at 16:58
I've modified the link to hopefully make it a little more obvious... – Michael Underwood Jul 30 '10 at 18:22
& ShreevatsaR: Didnt see the link before, but now even I am able to recognize it ;-) Thanks! – Nils Schmidt Jul 31 '10 at 14:07
No one mentions this: \def is shorter and easier to type without typos. It seems a silly reason, but it could be an enough reason ... if you know what are you doing (i.e, you know the danger of \def\par{foo}, for instance). – Fran Mar 13 at 10:15
up vote 132 down vote accepted

\def is a TeX primitive, \newcommand is a LaTeX overlay on top of \def. The most obvious benefits of \newcommand over \def are:

  1. \newcommand checks whether or not the command already exists
  2. \newcommand allows you to define an optional argument

In general, anything that \newcommand does can be done by \def, but it usually involves a little trickery and unless you want something that \newcommand can't do, there's no point in reinventing the wheel.

In answer to your particular points:

  1. Yes and No. A command defined using \def has to know exactly what its options are, and how they will be presented to it. However, a TeX command can be a bit clever and examine things other than its arguments. The way that optional commands are handled is to look at the next character in the stream and if it is [ then one version of the command is called, whereas if it isn't then another is called. So if you're prepared to do some hackery, then optional arguments are possible via \def, but it isn't half so easy as \newcommand.
  2. No. Since \def doesn't check for a command already being defined, you can redefine stuff simply using \def again. To get the checking, you need to use the \@ifundefined command (note the @ sign!).
  3. No!

There's probably lots more to be said on the differences, but that's enough from me.

share|improve this answer
What ampersand? – Mark Meckes Jul 30 '10 at 15:46
He means ampersat. – Will Robertson Jul 30 '10 at 16:00
I'd add that \newcommand can't make anything starting \end.... This is because LaTeX uses these names for the end of environments, and wants to 'discourage' you from breaking things. – Joseph Wright Jul 30 '10 at 18:18
Gosh! Until I looked at that link, I didn't understand what you were all talking about. I think there's a mistake in the mapping table in my brain. Hang on: sudo aptitude update brain. – Loop Space Jul 30 '10 at 18:48
@Andrew: did you mean sudo aptitude upgrade brain, by any chance? – SamB Dec 2 '10 at 3:02

Adding to Andrew's already good response, the one thing that \def can do that \newcommand cannot is define commands that take delimiters other than braces. For example, if you want to be able to write \foo<hello> and have the ‘hello’ interpreted as an argument, you can write

\def\foo<#1>{...something with #1...}

You can also have commands that read input up to the next brace, such as

\foobar 47pt {hello}

For a combination of the extensibility of \def with the ease-of-use of \newcommand, check out David Kastrup's suffix package. Very handy when you want to define custom markup for your document.

Also look at the LaTeX3 package xparse which takes a more structured and generalised look at how \newcommand should work.

share|improve this answer

One quite important difference, not mentioned in the discussion so far, is:

  • \newcommand\foo{...} corresponds to \long\def\foo{...}
  • \newcommand*\foo{...} corresponds to \def\foo{...}

What does \long mean? Well, if \myCommand is defined using \long, then its arguments can contain paragraph breaks, like this:

\myCommand{Lorem ipsum

dolor sit 


If \myCommand were defined without \long, then the code above would generate an error. The idea is, that if you know that your command won't ever need to take arguments containing paragraph breaks, then defining it without \long helps you to catch some situations where you've forgotten a closing brace.

For more in-depth discussion: What's the difference between \newcommand and \newcommand*?

Slight digression. Here is a little example that highlights this particular difference between \newcommand and \def.

The following code prints Not empty ...

\ifx\foo\empty Empty\else Not empty\fi

... but if you swap \newcommand for \def, it would print Empty! This phenomenon has been explained in a comment by Qrrbrbirlbel. The \empty command is created using \def\empty{}. Then, the \foo command is created (via \newcommand) using \long\def\foo{}. The presence of the \long stops these definitions being equal, and thus makes the test in the conditional fail.

share|improve this answer
Hi John. A tip: if you directly paste the url of a question/answer, the title will automatically appear. – Claudio Fiandrino May 24 '13 at 13:12

Here is a summary of the main points made by the other answers. In each case, click on the ▶ symbols for more details.

  • \newcommand is part of LaTeX; \def is part of TeX. []

  • \newcommand fails if the command is already defined; \def doesn't. []

  • \newcommand provides a convenient syntax for specifying the number of arguments, and specifying a (single) optional argument; \def enables pattern-matching on the form of its arguments, which makes it more general but also less readable. []

  • \newcommand can't define a command whose name starts with "end"; \def can. []

  • \newcommand defines "long" commands; \def (unless \long\def is used) defines short ones. (The arguments of a "long" command can include paragraph breaks.) []

share|improve this answer
I thought I would add this summary because there is no single "definitive" answer here; there are instead several good points raised in different answers. – John Wickerson May 25 '13 at 8:08

One subtle difference is that you cannot define commands starting with end:




results in

! LaTeX Error: Command \endtest already defined.
               Or name \end... illegal, see p.192 of the manual.

You can always use \newenvironment to achieve this. But the above code doesn't work even when using \providecommand instead of \newcommand.

Why is there no \provideenvironment? According to Frank Mittelbach, it's performance considerations -- saving memory, 20 years ago. But now there's \ProvideDocumentEnvironment from the xparse package. Plus,

Unlike LaTeX 2ε’s \newcommand and relatives, the \DeclareDocumentCommand function do not prevent creation of functions with names starting \end.

share|improve this answer
Interesting that LaTeX reports \endtest already defined when using \newcommand, but with renewcommand the message is \endtest undefined. I didn't know that LaTeX suffers from schizophrenia. :-) – Peter Grill Jul 5 '12 at 17:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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