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.

When I want some macro to (temporarily) do nothing, I normally write

\let\foo\relax

But somebody asked me why I don't just do

\def\foo{}

and I realized I couldn't answer his question.

So, I have two questions:

  1. Can somebody give a practical example of when the two would be different;
  2. For the purpose of disabling a macro, which of the two is preferred?

Here's an example of how I normally use this:

\let\print@style\relax
\def\setprintstyle#1{\def\print@style{#1}}
\def\print#1{\print@style#1}
share|improve this question

2 Answers 2

up vote 22 down vote accepted

When you

\def\foo{}

you are giving \foo an empty definition, but \foo still 'exists' using any of the tests used for this (such as \ifdefined or LaTeX's \@ifundefined). The same effect can be achieved by doing

\let\foo\empty

which is very slightly more efficient as it points to an already-used memory location (not a worry nowadays). When \foo has such a definition, it is expandable, and so

\edef\baz{\foo}

will result in \baz being empty, as \foo expands to nothing at all.

On the other hand

\let\foo\relax

makes \foo equal to the \relax primitive. That is a 'do nothing' operation, but importantly is not expandable. So in this case

\edef\baz{\foo}

leaves \baz with definition '\foo'. That can be useful: it's a way of temporarily preventing a macro from doing anything while retaining it's appearance in other code. On the other hand, sometimes you don't want that: it depends on the context. When \foo is equal to \relax, whether it is regarded as 'existing' by the various tests is more variable. TeX automatically creates control sequences equal to \relax in various cases, and so some tests will regard anything equal to \relax as 'not defined'.

So which is better depends on your use case.

share|improve this answer
    
Thanks for the explanation. I just added my use case to my question. –  Job Mar 23 '12 at 7:29

The first one lets \foo to point to \relax, whereas the second to an empty macro. (Provided no-one re-defined \relax or \empty.

\documentclass{article}
\begin{document}
\let\foo\relax
\ifx\foo\relax True\fi
\def\foo{}
\ifx\foo\empty True \fi
\end{document}

For the purpose of undefining none is recommended, rather use

\let\foo\@undefined

At any stage you can test via \meaning

\let\foo\relax
\meaning\foo

will show \relax

Edit

As per revised question. To temporarily disable let the command to a relax within a group as shown:

\def\foo{AAAAAA}
{\let\foo\relax \foo}
\foo

Within the group the meaning of the command will be equal to \relax and do nothing. Outside the group it will have its value restored.

share|improve this answer
    
for undefining, \let\foo\relax is fine in plain TeX. Otherwise I agree. –  mbork Mar 23 '12 at 7:10
    
I'm sorry I meant "disabling" a macro instead of "undefining" it. I still want to be able to use it. I updated my question. –  Job Mar 23 '12 at 7:13
    
@job You mean like if condition X do nothing else do something else (see edit)? –  Yiannis Lazarides Mar 23 '12 at 7:17
    
I updated my question with an example of how I normally use this. –  Job Mar 23 '12 at 7:25
    
@Job In your context \relax is fine. –  Yiannis Lazarides Mar 23 '12 at 7:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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