# [tex-k] A370: braces also inserted around \write token lists

Thu May 20 12:03:45 CEST 2021

Appendix C of The TeXbook ends with an enumeration of character codes
hardwired into TeX, and one of them is:

(5) ... braces are inserted around an \output routine

which is true. But TeX also inserts braces when expanding the token list
of \write commands, although they are stripped later. This is confirmed
by the fact that

\catcode(=1 \catcode)=2 \catcode#=6
\def\\#1(#1\iffalse(\fi))
\immediate\write16(hello\expandafter\\\string)

prints "hello}" on your terminal, even if you're using INITEX! The idea
is that TeX expands the token list of \write commands by enclosing them
in {_1 ... }_2 and expands it in the same way after \edef, \message, etc.
This seems like an implementation detail, since the braces will disappear
anyway, but our devious program captured the }_2 by converting it into
}_12 via \string and using an auxiliary macro \\ to put a )_2 after the
}_12, so the right brace leaked and TeX didn't notice anything wrong.

`