# [Tex/LaTex] the difference between \let and \def

macrostex-core

What is the difference between the \let and \def commands in TeX/LaTeX?

Ideally please provide a simple example that will illustrate the difference between them.

The difference is in the time at which the ‘right hand side’ is evaluated.

Thus \let\foo\bar defines \foo to have the value that \bar had at the point of definition. On the other hand, \def\foo{\bar} in effect defines \foo to have the value that \bar has at the point of use.

Consider:

\def\bar{hello}
\let\fooi\bar
\def\fooii{\bar}
\fooi +\fooii

\def\bar{goodbye}
\fooi +\fooii


This produces

hello+hello
hello+goodbye


This is a simple process.

However it's also a subtle one, so it might be worth highlighting a few key points:

• When TeX encounters control sequences such as \fooi, it evaluates them; if these are macros (that is, they have been defined by \def, or \let equal to something which was defined by \def), then the result is that they will expand to other tokens, which TeX will then examine in turn, and so on, recursively, until what's left is either ‘primitive’ control sequences or letters (I'm simplifying a little bit).

• \fooi expands directly to the characters hello (because \bar initially did, and \fooi was defined to have the same value).

• \fooii, in contrast, expands to \bar, which is then immediately reexamined and reexpanded. In the first case, \bar expands to hello and in the second case to goodbye. The definition of \fooii hasn't changed, but \bar has been redefined in between.

• Getting a clear idea of the process of this recursive expansion is very helpful when learning how to develop and debug TeX macros.