[Tex/LaTex] \renewcommand for commands defined with \def

macros

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.

Best Answer

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.