201

What do newcommand, renewcommand, and providecommand do, and how do they differ?

I think \newcommand overwrites any previously existing command, while \providecommand creates the command only if it does not already exists. Does \renewcommand only work on already extant commands?

3 Answers 3

224

No, it is not correct.

\newcommand defines a new command, and makes an error if it is already defined.

\renewcommand redefines a predefined command, and makes an error if it is not yet defined.

\providecommand defines a new command if it isn't already defined.

If you want to define a command whether it is new or predefined, you can use \def or

\providecommand\foo{}
\renewcommand\foo{...}
3
  • 3
    I never knew what to do with \providecommand or how to do this unconditional definition in LaTeX-style code. Thanks!
    – Ryan Reich
    Nov 25, 2011 at 21:11
  • 9
    For some reason there appears to be missing a "define a macro even if it's already defined". Of course there is the plain old \def, but this sacrifices the ease of use of the LaTeX macros. The easiest way to emulate this is to \let\foo\outer\renewcommand\foo{stuff}. (I chose \outer because it's pretty likely to produce errors if used incorrectly). The best solution would of course be to make a \defcommand that always overwrote its argument regardless, but it would have to have a starred version like \newcommand and friends which complicates matters a bit...
    – kahen
    Apr 24, 2012 at 17:01
  • 2
    I guess this would work: \makeatletter\def\defcommand{\@ifstar\defcommand@S\defcommand@N} \def\defcommand@S#1{\let#1\outer\renewcommand*#1} \def\defcommand@N#1{\let#1\outer\renewcommand#1} \makeatother. (God I hate that we can't have linebreaks in comments...)
    – kahen
    Apr 24, 2012 at 17:07
73

They have all in common, that they define a new command, but the precondition and behavior differs a bit:

  • \renewcommand works only if the command is already defined: it's a redefinition

  • \newcommand doesn't work if the command is already defined: so it's a completely new definition

  • \providecommand works like \newcommand, but if the command is already defined, the (re)definition is ignored

\renewcommand and \newcommand would throw an error, if the condition is not fulfilled, to ensure clean programming, so you don't accidentally overwrite existing commands or try to redefine one which doesn't exist.

\providecommand may be helpful, if the same code would be used in several documents. If you would use it, you should be aware that it might have no effect. But using it you can avoid compile time errors.

For all commands exists a starred version. To see the difference, have a look at:

If you really don't want error checking, you could use \def, see:

29

The idea is to prevent an accidental overwriting of commands by a not so knowledgeable user.

  1. \newcommand defines a new command and gives an error if the command already exists. It does not overwrite an existing command.

  2. \renewcommand overwrites an existing command and gives an error if the command does not already exist.

  3. \providecommand defines a new command if the command with this name does not exist, or does nothing if it exists.

Exercise: use \providecommand and \renewcommand to define a command which may either exist or not.

2
  • Leo Liu's answer already contains the solution to your exercise. Nov 25, 2011 at 19:34
  • 1
    @GonzaloMedina We wrote our texts simulatenously :)
    – Boris
    Nov 25, 2011 at 20:51

You must log in to answer this question.

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