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.

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?

share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Please see tex.stackexchange.com/questions/20655/…, this appears like a duplicate question. –  Yiannis Lazarides Nov 23 '11 at 22:04
1  
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

3 Answers 3

up vote 10 down vote accepted

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.

share|improve this answer
    
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
6  
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.

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

share|improve this answer
    
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
1  
@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.

share|improve this answer
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

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