# [texhax] What's the problem? ( \input vs. token list )

Hartmut Henkel hartmut_henkel at gmx.de
Tue May 20 01:23:48 CEST 2003

Ok, I tried it again: Several possibilities:

1. You fill a token list by reading a file line by line, using a
recursive \read <channel> to <macro> method:

%----- first line of file xx.tex
\newtoks\mytoks
\def\depar#1\par{\edef\tmp{\the\mytoks#1}}%

\ifeof #1
\else
\ifeof #1
\expandafter\depar\foo % remove final \par. TeXbook p. 217.
\else
\edef\tmp{\the\mytoks\foo}% Salomon: Advanced TeXbook p. 195.
\fi
\mytoks=\expandafter{\tmp}%
\let\tmp\relax
\fi
}

\openin 5 yyy.tex

% The following recalls the file contents without reading the file:
\the\mytoks \the\mytoks \the\mytoks \the\mytoks

\bar % Macro defined in the file yyy.tex works
\bye
%----- last line of file xx.tex

And this is the example file to read:

%----- first line of file yyy.tex
1st line of file yyy.tex%
2nd line of file yyy.tex%
\def\bar{A macro}%
%----- last line of file yyy.tex

2. Or you put your file contents to read in into brackets, e. g.

{%----- first line of file yyy.tex
1st line of file yyy.tex%
2nd line of file yyy.tex%
\gdef\bar{A macro}%
}%----- last line of file yyy.tex

This file you can read in by a SINGLE \read <channel> to <macro>, since
\read wants to read until bracket pairs are complete (TeXbook p. 217).
Unfortunately macros are not global anymore, therefore the \gdef.

3. Or you write something like

\setbox0=\vbox{\input yyy.tex}\unvcopy0\unvcopy0

This in fact fills the \vbox by the \input stuff; the file is only read
once. Again macros have to be global, and there are some side effects,
as the \vbox contents is already typeset.

4. Or you put a \def\blabla{...all lines of your file...} into your file
and read it once by \input yyy.tex. Then you can use the \blabla macro
as often as you like. You might have more than one \def in your file.

But any attempt to coerce \input with whatever number of \expandafters
to put some file stuff into a \toks register has failed here. I believe
it's not possible (why?).

IMHO the no. 1 or something similar but more elaborated, is the
favorable solution for your question, if the yyy.tex file has arbitrary
contents. But no. 4 might be the basic way how most users do it; the
input file simply is the macro repository.

Have fun!

Hartmut

On Sat, 17 May 2003, ky anh wrote:

>   \newtoks\xxx        |
>   \xxx={\input yyy}   | <-- "\the\xxx" is not the
>   \the\xxx            |     content of "foo.tex"
>   \the\xxx            | <-- recall "\input"