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.

In the following MWE I've used \renewcommand to redefine a command defined through \def:

\documentclass{article}

\def\mymacro{Hello}
\renewcommand{\mymacro}{Hello world}

\begin{document}
\mymacro
\end{document} 

and it works fine, as far as I can see.

What I would like to know is:

  1. Are there cases in which it doesn't work?
  2. Is it a good practice or I should I use \def to redefine commands defined through \def?

EDIT

I've already taken a look at What is the difference between \def and \newcommand?. What I mean is that sometimes I need to hack some commands defined through \def and I would like to know whether I'm allowed to do that with \renewcommand or not.

share|improve this question
    
The general rule (in LaTeX) is not to use \def unless you need to. Use \(re)newcommand where possible. –  Ian Thompson Nov 16 '13 at 9:16
    
@percusse Thanks for the link. But see my edit. –  Sterry Nov 16 '13 at 9:29
1  
renewcommand is only a wrapper around \def. –  Marco Daniel Nov 16 '13 at 10:17
1  
if a command created with \def is "context sensitive", it can't be redefined with \renewcommand. but for a simple definition like the one you show, \renewcommand works just fine, and is probably preferable. –  barbara beeton Nov 16 '13 at 13:32
4  
All that \renewcommand requires is 1) that the command already have a meaning, 2) that it is not \relax, and 3) that it doesn't start with \end. It doesn't care how the command acquired it's meaning. It could be via \def, \let or even \chardef, \countdef, etc. What @barbarabeeton is saying is that \renewcommand can't always duplicate the argument syntax of the original command. –  Dan Nov 16 '13 at 22:53

1 Answer 1

up vote 20 down vote accepted

if a command created with \def is "context sensitive" (or "delimited"), it can't be redefined with \renewcommand. but for a simple definition like the one you show, \renewcommand works just fine, and is considered preferable in LaTeX.

the following pattern is used internally in amsmath to check the presence or class of symbols in order to "do the right thing" when defining symbol commands that are more flexible than similar ones provided by plain tex:

\def\@tempa#1#2\@nil{<...>}

the string \@nil looks like a command, but it really isn't. instead, it is used as a "sentinel" to delimit the scope of what is being tested.

use of a sentinel in this matter avoids the need for braces when using the defined command. the "LaTeX way" is to use braces, but in some contexts, the proliferation of braces makes input more difficult to read (and debug when errors occur), and the sentinel method can be used to advantage.

edit: as noted by @Dan, this doesn't mean that a command can't be redefined using \renewcommand, but that it's impossible to replicate the usage syntax of the original when doing so.

share|improve this answer
    
Thank you very much. –  Sterry Nov 16 '13 at 19:13

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.