undefining custom commands

If your concern is whether or not a command exists and you want to define/redefine it, LaTeX provides \providecommand which has a similar interface as \newcommand and \renewcommand. It does the appropriate (re)definition where necessary.

Alternatively, if you want to "undefine" a command \foo, you could also just use \let\foo\undefined.


Consider the following example. It shows the way as Seamus suggested and the way with \let...\undefined.

\documentclass{minimal}

\newcommand{\foo}{Foo}
\newcommand{\baz}{Baz}

\begin{document}
x\foo\baz x

\renewcommand{\foo}{}
\let\baz\undefined

x\foo%
%\baz x% can’t use \baz anymore

%\newcommand{\foo}{FOO}% doesn?t work
\renewcommand{\foo}{FOO}% still need \renew...
\newcommand{\baz}{BAZ}

x\foo\baz x
\end{document}

\foo dosen’t produce an output anymore but can’t be defined a second time (it still needs the \renew… command). \baz is removed an can be defined a second time.


If the macro is used nowhere else, there's no problem leaving it defined surely.

Rather than encapsulation, you could consider name-spacing your chapter specific macros.

So macros used only in chapter one look like this: \chaponefoo… while macros from chapter three will be: \chapthreefoo… and so on.

(My previous answer had some problems, and Werner's answer is better.)

Tags:

Macros