TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I feel as I really should know this, but sadly I don't. Why is the output of \typeout{\the\myToken} not the same as having it printed in the PDF as \the\myToken. No problem with a \def, just with something declared via \newtoks.

The PDF shows:

enter image description here

but the console shows



Expansion issue? Well, that is what I thought, but then \expandafter\typeout{\the\myToken} should have worked?


  • Why is this happening?
  • How do I define a \MyTypeout{} (or preferably redefine \typeout{}) macro so that the actual current expanded value is output in the console the same was it would be displayed in the PDF.





\typeout{DefaultValue=\DefaultValue}  DefaultValue=\DefaultValue

\typeout{MyToken=\the\myToken}        MyToken=\the\myToken
share|improve this question
up vote 11 down vote accepted

In typesetting context, the expression \the\myToken is fully expanded and typeset in TeX's stomach. Thus the expected result is 1-9.

Macro \typeout is defined in latex.ltx:


In this context (\immediate\write) the expression \the<token register> is expanded once, but the contents of the token register is not expanded further.

From "The TeXbook", "Chapter 20: Definitions (aka Macros)":

Expanded definitions that are made with \edef or \xdef continue to expand tokens until only unexpandable tokens remain, except that token lists produced by \the are not expanded further.

\immediate\write is not \edef or \xdef, thus we also need:

TeX's primitive commands \mark{...}, \message{...}, \errmessage{...}, \special{...}, and \write<number>{...} all expand the token lists in braces almost exactly as \edef and \xdef do. However, a macro parameter character like # should not be duplicated in such commands; [...]


> \x=macro:
->\DefaultValue .

> \x=macro:

Thus you need to trigger the expansion twice to get the contents of the token register expanded.

Token register can be used to simulate eTeX's \unexpanded (however the construct will be non-expandable, \unexpanded is expandable). For example, this is used in \g@addto@macro (latex.ltx) that adds some tokens in the second argument to the macro in the first argument:


Macro #1 is expanded once and the result is put into a token register with the additional tokens in #2. The following \xdef defines the macro with its original definition text with the tokens in #2 appended.

Expansion of token register inside \typeout

The contents of a token register looses their non-expandable behavior if it is touched twice:



Macro \typeout should not be redefined, the contents of token registers can contain stuff that breaks, if it would expanded (undefined commands, endless recursion, ...). Also it would disturb the usages of \typeout for other, is the tokens where suddenly expanded.

share|improve this answer
Thanks. So is there a good way to change this behaviour? It seems that \makeatletter \def\typeout#1{% \begingroup \edef\ExpandedOutput{#1} \set@display@protect \immediate\write\@unused{\ExpandedOutput}% \endgroup } \makeatother seems to do what I desire, but is there a reason I should not do that? – Peter Grill Nov 29 '12 at 8:07
@PeterGrill I wouldn't redefine a kernel command; in any case, use \protected@edef and not \edef. – egreg Nov 29 '12 at 8:37
@PeterGrill Example added, how the token register can be expanded inside \typeout. – Heiko Oberdiek Nov 29 '12 at 11:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.