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.)