Take the 2-minute tour ×
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.

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?

share|improve this question

3 Answers 3

up vote 55 down vote accepted

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{...}
share|improve this answer
1  
I never knew what to do with \providecommand or how to do this unconditional definition in LaTeX-style code. Thanks! –  Ryan Reich Nov 25 '11 at 21:11
    
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 '12 at 17:01
1  
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 '12 at 17:07

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:

share|improve this answer

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.

share|improve this answer
    
Leo Liu's answer already contains the solution to your exercise. –  Gonzalo Medina Nov 25 '11 at 19:34
1  
@GonzaloMedina We wrote our texts simulatenously :) –  Boris Nov 25 '11 at 20:51

Your Answer

 
discard

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.