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.

As I understand it, the point of \relax is to stop a macro from swallowing up subsequent material (such as whitespace). But {} (\bgroup\egroup) serves the same function. Why do we need both?

share|improve this question
10  
Try starting TeX from the terminal without any file name and type {} at the ** prompt you get. ;-) Kill the process and retry typing \relax, this time. :) –  egreg Dec 10 '12 at 15:55
add comment

3 Answers

up vote 46 down vote accepted

Try the following couple examples:

\documentclass{article}

\begin{document}

\[
\begin{array}{rl}
+9mm   & +9m^2 \\
[+9mm] & [+3m]^2 \\{}
+9mm   & +9m^2 \\{}
[+9mm] & [+3m]^2 \\\relax
+9mm   & +9m^2 \\\relax
[+9mm] & [+3m]^2
\end{array}
\]

\end{document}

enter image description here

  • If you leave there \\ alone, the [+9mm] is interpreted as an optional argument to \\ and transformed into a vertical space.
  • If you use {}, then it inserts an "empty thing" into the formula and the + is improperly spaced.
  • Only the variant with \relax works correctly here, because it really does nothing.

Very similar examples can be used to show that \relax is the correct terminator of \dimexpr and \numexpr expansion etc.

share|improve this answer
add comment

It's hard to answer the question as the two constructs are different in almost all respects so it's a matter of where to start.

\relax is a single token, an unexpandable TeX primitive that does nothing when executed. {} is two tokens which (assuming the standard catcodes) mark a TeX group or non delimited macro argument.

  • \baselineskip =13pt\relax the \relax stops the length parser looking ahead for plus and minus components, but does nothing else. \baselineskip =13pt{} would define baselineskip the same way but then open and close a group which could have several effects, most notably in math mode where it will generate an empty math atom and affect the spacing of surrounding atoms.

  • {\let\section\relax...\immediate\write\@auxout{.. \section}} makes a command such as \section locally inert and safe to write (as itself) to an auxiliary file. {\let\section{}...\immediate\write\@auxout{.. \section}} does something but is unlikely to do what anyone wants:-)

  • \relax is the definition given to a new control name generated by \csname hence the common idiom \expandafter\ifx\csname foo\endcsname\relax to test if a macro is defined. This clearly has no analogue using {}

  • $a+{}$ produces infix spacing as the {} generates a mathord atom. $a+\relax$ is like $a+$ and not produce infix spacing for +.

share|improve this answer
add comment

There is more than one reason.

I'll give an example where \relax is good and {} isn't:

\def\mysize{144pt\relax} % when expanding \mysize we won't trigger
                         % expansion of the next token
\hbox to \mysize{\hfil abc\hfil}

When TeX is processing the \hbox command, it expands the tokens after to in order to find the required <dimen>; it will find 144pt\relax and the syntax rules make this \relax disappear (in TeX's jargon it's a <filler>); with

\def\mysize{144pt{}}

TeX would find

\hbox to 144pt{}{\hfil abc\hfil}

which would be a disaster.

Of course one might as well define

\def\mysize{144pt }

but then \mysize could not be used in text without \unskip. A bit contrived example, perhaps.

In any case, \relax does really nothing, while {} opens and closes a group.

You mention \bgroup\egroup as equivalent to {}: it isn't; for instance, a macro with an argument that finds {} will absorb an empty argument, while \bgroup\egroup would absorb \bgroup as the argument, leaving \egroup in the stream. Again, if we use \relax it would be one token.

share|improve this answer
add comment

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.