# Why does LaTeX (contrarily to TeX) not allow catcode changes in footnotes?

In Plain TeX the following compiles with no error

Hello\footnote*{\tt\catcode92=12 \world}!
\bye


\documentclass{article}
\begin{document}
Hello\footnote{\ttfamily\catcode92=12 \world}!
\end{document}


fails with error message

 ERROR: Undefined control sequence.

--- TeX said --- <argument> ...ces \ttfamily \catcode 92=12 \world
\@finalstrut \strutbox  l.4 Hello\footnote{\ttfamily\catcode92=12 \world}
!


which just says that \world was still seen as an undefined control sequence. So it is as if the (mandatory) argument to \footnote was treated as is usual with macro arguments and the consequence is that one can not use \verb in the LaTeX footnotes.

What are the rationale or implementation constraints which led to this impoverished (in that respect) \footnote concept in LaTeX?

-
There's no note about this in source2e. I guess that Leslie Lamport used a simplified definition and LaTeX2e didn't change it. –  egreg Jan 14 '13 at 17:17
Don't you need to restore the catcode of ASCII92 (the backslash character) back to 0 before the end of the footnote field? (The \footnote command in PlainTeX is defined differently from the one in LaTeX.) –  Mico Jan 14 '13 at 17:18
@Mico no I don't. Plain TeX creates a group, so that for example one can change font in the footnote without contaminating the main text. –  jfbu Jan 14 '13 at 17:23
@egreg I wonder if Leslie was worried that the plain approach means that \footnote is inconsistent with other arguments (where verbatim cannot be used), so was 'tidying up'. I guess we could ask him :-) –  Joseph Wright Jan 14 '13 at 17:24
@jfbu Also in LaTeX the footnote text is typeset in a group. This has nothing to do with the "changing catcodes" problem. –  egreg Jan 14 '13 at 17:31

LaTeX has always done this. Certainly at LaTeX2e it was a concious decision not to change it. The documented behaviour is that \verb does not work in the argument of another command. If you make it work in \footnote you have to explain why it doesn't work in \marginpar or in \footnote that is inside \textrm{...} or any other place it would fail. Basically LaTeX does not support changing catcodes mid-document. There are almost always better more robust techniques that could be used, for example \footnote{\ttfamily\string\world} works in LaTeX even if that is already in the argument of another macro.

-
All rules should admit exceptions. The normal LaTeX user has to read his/her way through complicated stuff of "moving arguments needing protection". Compared to that, explaining that \footnote allows a bit more of flexibility than other commands seems as it should not be that traumatizing to the average LaTeX user. And yes, I often use \ttfamily\string even in the body, although braces then need to be \{ or \string{. I know I can not imagine the globality of the difficulties faced at the time of LaTeX2e, but I am not convinced by that one. –  jfbu Jan 14 '13 at 18:23
but why (apart from the fact that plain does it) would one even think of special casing footnote here, why footnote and not marginpar or parbox or ... Plain has far fewer commands and far less consistency of syntax anyway so it is less of an issue there. For similar reasons \mbox and \sbox parse their arguments as macros in 2e –  David Carlisle Jan 14 '13 at 18:41
well, I would vote for having marginpar and footnote behave similarly to one another, and allow the same freedom therein as in the general text. And with regards to consistency of syntax wouldn't it have required to make footnote an environment, too? –  jfbu Jan 14 '13 at 18:52

FWIW, this snippet works in ConTeXt.

\starttext
Hello\footnote{\tt\catcode92=12 \world}!
\stoptext


but, as pointed out by David, it fails for \inmargin (which is the ConTeXt equivalent of \marginpar). This is not surprising; all catcode changes fail with macros that parse their argument. Having said that, the standard way in ConTeXt to take care of catcode changes is to use buffers.

\starttext
\startbuffer[catcode]
\catcode92=12 \world
\stopbuffer
Hello\inmargin{\tt\getbuffer[catcode]}!
\stoptext


which works correctly in both MkII and MkIV. In LaTeX, the filecontents environment is the closest to buffers. (See LaTeX equivalent of ConTeXt buffers).

-
filecontents (or filecontents*) work indeed. Two defects though: this is preamble-only, and after having been used once they will not overwrite the existing file. This ConTeXt buffer facility indeed is neat. Thanks for the pointer. –  jfbu Jan 15 '13 at 7:52