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}
... % chapter body
% end of chapter

Is there anything like the fictitious \forgetcommand macro above?

  • 1
    Please see tex.stackexchange.com/questions/20655/…, this appears like a duplicate question. – Yiannis Lazarides Nov 23 '11 at 22:04
  • 2
    It should also work to use \let\foo\undefined. – Werner Nov 23 '11 at 22:09
  • 3
    @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

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.

  • 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
  • 18
    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

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



x\foo\baz x


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

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

x\foo\baz x

\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.

  • 1
    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

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.)

  • 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
  • When defining a command in a chapter, I currently need to check previous chapters to see if it's been already defined differently. If it is, I must use \renewcommand. If it isn't, I must use \newcommand. I don't want to have to worry about this, so I want a chapter-specific command to be forgotten entirely at the end of its chapter. Consider this desire for encapsulation a programmer's itch. – SuperElectric Nov 23 '11 at 22:15
  • @SuperElectric: You can do that with a combination of \providecommand{\foo}{} and \renewcommand{\foo}<etc> – Brent.Longborough Nov 23 '11 at 22:35
  • 2
    @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

Not the answer you're looking for? Browse other questions tagged or ask your own question.