A token register is very similar to a macro with no arguments, but there are differences in syntax and expansion rules.
Using the plain and latex definition of
\toks 0 you can store the tokens
Note that a macro has a specific command to set the macro (
\def or wrappers around that such as
\newcommand) but token registers just use the register name followed by a _balanced text group.
Conversely unlike macros which expand just by referencing their name, token registers, like other registers are expandable and their contnts are accessed by expanding
each expand to
Apart from these syntactic differences the expansion of token registers differs from macros in two important ways.
1) expansion in
\edef and other expansion only contexts is limited to one level/
\toks@ contain the same token list
xx \tmp but
\A is defined by fully expanding expandable tokens and so has defnition
xx aaa but the tokens returned by
the are not further expanded in the
edef and so
\B has definition
2) The second difference is that as token registers do not have arguments
# does not need to be (and is not) special when defining or expanding the register.
is just a token register consisting of a list of two tokens
This is put to use in LaTeX's
is supposed to add
abc to the end of the current definition of
A simple (and if I recall correctly original) definition could be
That works fine and shows the definition as
123abc however try
and you get
! Illegal parameter number in definition of \foo.
However we can use the fact that
# isn't special in a toks register and that the register contents are only expanded once
\toks@ register is defined to contain the expansion of the macro passed in the first argument, followed by the contents of the second argument.
# is safe to use in a toks assignment. Then the macro is globally defined to be the expansion of
\thetoks which is exactly the contents of
\toks@ with no further expansion, even if that conatins
# has been added as intended (only one
# has been added the doubled
## is an artefact of using