What is the difference between \relax and {}?
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}
- 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.
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 forplus
andminus
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+
.
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.