How TeX reads input

Unofficial ConTeXt Wiki mirror

Last modified: 2020-06-08

Including the contents of another file with TeX is done with \input. But you have to be careful: TeX appends a character with the current value of \endlinechar to each line of an inputed file, and that character is later converted to a space. That means, that the following 2 examples give the same results:

\starttext
\immediate\write18{echo -n X >temp.tex}
X\input temp X
\stoptext
\starttext
\immediate\write18{echo X >temp.tex}
X\input temp X
\stoptext

There are at least 5 solutions, to get rid of the space before the last X:

\immediate\write18{echo X >temp.tex}
X{\endlinechar=-1 \input temp }X
\immediate\write18{echo X\letterpercent >temp.tex}
X\input temp X
\immediate\write18
     {echo X\letterbackslash\letterbackslash relax >temp.tex}
X\input temp X
\immediate\write18{echo X >temp.tex}
X{\catcode`\^^M=9 \input temp }X
\immediate\write18{echo X >temp.tex}
X\ignorespaces \input temp\relax \removeunwantedspaces X

Piping

From Taco's post on the mailing list:

Latest pdfTeX allows piping:

\input "|ruby ./myscript.rb \vartest XXXXXXXXX"

the magic trick is the | symbol. With the current tex:

\writ18{ruby ./myscript.rb \vartest XXXXXXXXX > whatever.tex}
\input whatever % or something using \openin ...