crazy.txt

This problem arose to me when writing the package mkpattern. Sumarasing, the control sequence \lee (spanish imperative for to read) takes one argument. If the user writes \input myfile.tex, we want \lee to read the very first token in the input file and, when TeX has finished to read myfile.tex, we want \lee to take control again, i.e., to read the next token in the main file.

At first I thougt it to be impossible, but then I had two carzy ideas. The first one is \expandafter\lee\input:

\def\mkinput#1{\expandafter\lee\input #1 } %we are lucky this works

Do you think the previous was daring? It is nothing compared to the next. We cannot write \lee at the end of the file, because we will get a message like

! File ended while scanning use of \lee.

But there is a way to make TeX read a control sequence at the end of one file that has been input and take its arguments form the parent file. We have to... expand the end of the file!!!! That is, not \endinput, but the end of the file itself, so we end the file with a widow expandafter: \expandafter\lee
Further, \endinput may appear in the middle of the file, and we cannot define it to be \expandafter\lee. We have to 1st expand (reaching with expandafter) \endinput and 2nd expand the end of the file, i.e., something like

\expandafter\expandafter\expandafter\lee\endinput %This deserves capital exclamation marks, if they existed.

My actual definition was

\def\mkendinput{\expandafter\lee} %Expand the end of the file before \lee. It works!
\let\@@endinput\endinput
\def\endinput{\expandafter\mkendinput\@@endinput}



%%Here follows a sample:

%File tex0.tex:

\def\lee#1{$<$#1$>$}
\def\mkendinput{\expandafter\lee}

The number \expandafter\lee\input tex1.tex
{crazy} some more words.
\end

%File tex1.tex

8. Two words
\expandafter\mkendinput\endinput
Text that will not appear

%Now say at the command line: tex tex0.tex

Document Actions