As cgnieder's excellent answer says, \unexpanded
is a primitive of e-TeX, that is, an extension of the original TeX language. Primitives don't have a definition, they're commands directly understood by the engine. Some of them are expandable, that is, they don't reach TeX's “stomach” because they are expanded just like macros (the same happens to the conditionals, for example); \unexpanded
is one of these macros.
It's a generalization of \noexpand
which, in the context of an \edef
or a \write
, has a null expansion but also the effect of making the next token unexpandable for the task at hand. Thus
\def\foo{something}
\def\baz{else}
\edef\x{\foo\noexpand\baz}
would be equivalent to
\def\x{something\baz}
With \unexpanded
you can protect from expansion in an \edef
(or \xdef
) or \write
an entire token list, without the need to prepend \noexpand
to all expandable tokens.
Since \unexpanded
follows the pattern
\unexpanded <general text>
it has an interesting feature. When TeX wants to expand it, it looks forward to find a <general text>
which is defined as
<filler> { <balanced text> <right brace>
and expands tokens to recognize a <filler>
and, eventually, the {
. A <filler>
is just an arbitrary sequence of \relax
and space tokens which are simply ignored. The {
is an explicit or implicit token of category code 1.
A consequence of this is that
\unexpanded\expandafter{\cs}
will cause the expansion of \cs
before the {
that starts the list of tokens which will not be expanded any more. Indeed, the definition of \expandonce
is
\newcommand{\expandonce}[1]{\unexpanded\expandafter{#1}}
Also \detokenize
(another expandable primitive of e-TeX) has the same properties.
Beware of the <filler>
: it can cause some unexpected effects under certain circumstances as shown in Get the lion to run in loops. Tersely
source2e
.\unexpanded
is an eTeX primitive, have a look at the eTeX manualeTeX
stuff? Or how would I usedtexdef
to show the source code?texdef
for\unexpanded
is the same as trying to use it for say\def
or\relax
: you won't get anything. As it's a primitive, the source is in WEB (a form of Pascal), but unless you really need the detail then I'd go with the engine manual (eTeX in this case).