What is the difference between \let and \def?
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 charactershello
(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 tohello
and in the second case togoodbye
. 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.
While the existing answers are all true I like to highlight one point which wasn't explicitly mentioned yet. I myself got this information recently from Joseph Wright (see his answer and our comments in Simple un-obfuscation of some LaTeX internals).
As Michael said \let\macroa\macrob
"copies" the definition of \macrob
to \macroa
. However an IMHO important thing here is that the definition isn't actually copied, i.e. exists twice, but the command sequence \macroa
now points to the same hash table entry as \macrob
.
This means that \let\macroa\macrob
uses less memory space (very important in the early days but not anymore) and is faster then \def\macroa{\macrob}
because in the second form two command sequence names have to be resolved in the hash table.
Also \let
actually "copies" the definition of tokens, which do not need to be macros/command sequences. This allows the definition of command sequences representing implicit characters like \let\bgroup={
.
They're pretty completely different. \let
copies a command to a new name, while \def
creates a new command.
For example:
\def \foo {bar}
creates a new command \foo
, that evaluates to bar
when run.
\let \foo \bar
copies the commands from the \bar
commands to the \foo
command, so you can call either. Because it's a copy (and not a pointer from one to the other), redefining \foo
won't change the behavior of \baz
. Hence:
\def \foo {bar}
\let \baz \foo
\baz % Outputs 'bar'
\def \foo {new-definition}
\baz % Still outputs 'bar'