Until recently, I thought that execution of
\futurelet\testtoken<token1><token2>...
has the following effect: The control sequence \testtoken is \let to <token2> and \futurelet\testtoken is stripped from the input stream, so TeX continues with <token1><token2>....
In this answer of mine I observed (with TH's help) that this is not quite true with respect to catcodes, and in this nice answer Philippe explains that things can also go terribly wrong in nested alignments. For the sake of completeness let me give two short examples here.
\def\activateA{\catcode`A=\active}
{\activateA
\gdefA{undocumented behaviour?}
}
An \futurelet\testtoken\activateA A
\bye
The expected output is "An undocumented behaviour?", but this one gets only when \futurelet\testtoken is removed. With the \futurelet, the output is just "An A". The reason seems to be that \futurelet fixes the catcode of the last A. (One still gets that \testtoken=the letter A.)
The second example is a bit longer and more involved.
\def\begintestalign{\show\testtoken
$\vcenter\bgroup\halign\bgroup##&##\cr}
\def\endtestalign{\egroup\egroup$}
\halign{#&#\cr
a & \futurelet\testtoken
\begintestalign
& c \cr
test & de \cr
\endtestalign \cr
}
\bye
The expected output is
(and one would expect \testtoken=alignment tab character &), but instead one gets \testtoken=\outer endtemplate, and then
! Emergency stop.
<recently read> \endtemplate
<template> \endtemplate
l.7 &
c \cr
! ==> Fatal error occurred, no output PDF file produced!
I can't fully understand what's happening here, but my main question is: Where do I find this behaviour of \futurelet documented?
(And honestly, I'd really like to have a \futurelet that behaves as expected; the implemented behaviour is rather mean, and I wonder if that's a bug or a feature.)
\futurelethas to be fed 3 tokens (the first of which has to be a control sequence), and so these tokens have to be formed and sent to TeX's stomach. (I hadn't thought of this!) And then the trouble can't be avoided anymore. Yes, the 2nd example is about automatically inserted code after the template; this was clear to me (hidden behind "can't fully understand"), but probably not to others, so it's good you explained that. Maybe it's implicitly explained in the TeXbook, but I didn't find an explicit explanation. – Hendrik Vogt Feb 21 '11 at 16:44