# undefining custom commands [duplicate]

Possible Duplicate:
How to undo a \def (i.e., Need a \undef capability)

After you define a command using \newcommand, is it possible to undefine it?

My motivation is that I'm writing a thesis, where each chapter is in a separate source file. Some chapters define their own commands that are used nowhere else, so I'd like them to be visible only within that chapter.

I'd therefore like to be able to do something like:

\chapter{Some Chapter}
\newcommand{foo}{...}
... % chapter body
\forgetcommand{foo}
% end of chapter


Is there anything like the fictitious \forgetcommand macro above?

-

## marked as duplicate by egreg, Werner, Peter Grill, Mico, SeamusNov 24 '11 at 17:52

Please see tex.stackexchange.com/questions/20655/…, this appears like a duplicate question. –  Yiannis Lazarides Nov 23 '11 at 22:04
It should also work to use \let\foo\undefined. –  Werner Nov 23 '11 at 22:09
@Yiannis Lazarides: If I were familiar with raw TeX, I'd probably be able to parse that question you linked to, and its answer. Unfortunately I've only ever dealt with the subset of LaTeX needed to write scientific papers. I suspect I'm not alone. –  SuperElectric Nov 23 '11 at 22:18
@Werner: Thanks, that worked. If you post that as an answer, I'll choose it. –  SuperElectric Nov 23 '11 at 22:20
@SuperElectric: I've done so. Also see the other approach I provide via \providecommand - it seems to be more in line with what you may be after. –  Werner Nov 23 '11 at 22:31


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

-
Thanks. FWIW, I much prefer your \let\foo\undefined option over using \providecommand. The latter leaves open the possibility that I'll define \foo in chapter1, forget to redefine it in chapter 2, then invoke it in chapter 2, expecting its chapter-2-specific definition which I forgot to provide. With \let\foo\undefined, this mistake would be caught by the compiler. –  SuperElectric Nov 23 '11 at 22:43
It should be noted that \undefined does not have a special meaning here, but it is simply assumed that it is not defined. \let copies every aspect of a control sequence including its undefinedness. So \let\foo\anymacrowhichisnotdefined will also work. This of course breaks if any code before defines that macro, Often a \@undefined is used inside packages, to lower that risk a little. –  Martin Scharrer Nov 24 '11 at 5:42

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.

-
The latter will gobble arguments if the same macro was used in subsequent chapters, rather than doing nothing or producing an error. –  Werner Nov 23 '11 at 22:08
@SuperElectric: You can do that with a combination of \providecommand{\foo}{} and \renewcommand{\foo}<etc> –  Brent.Longborough Nov 23 '11 at 22:35
@Brent.Longborough: See my comment to Werner's answer for why I prefer \let\foo\undefined (it makes the compiler catch my mistakes). –  SuperElectric Nov 23 '11 at 22:48

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.

-
I'd \let to \undefined, as that was the request here (\relax is not the same thing). –  Joseph Wright Nov 23 '11 at 22:25
@JosephWright: Thanks. I thougt there would be a better way, but didn’t know it yet. I used \let…\relax in the past and it worked fine till now. I’ll change my example. –  Tobi Nov 23 '11 at 23:47