How TeX reads input

From Wiki
Revision as of 08:10, 9 March 2006 by Peter (talk | contribs) (New page.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

< TeX and pdfeTeX >

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:

  • Setting \endlinechar to -1 temporarily:
\immediate\write18{echo X >temp.tex}
X{\endlinechar=-1 \input temp }X
  • Writing a percent sign to the end of the line:
\immediate\write18{echo X\letterpercent >temp.tex}
X\input temp X
  • Ending the written line with \relax (or a similar space-gobbling command):
\immediate\write18
     {echo X\letterbackslash\letterbackslash relax >temp.tex}
X\input temp X
  • Changing the catcode of the current \endlinechar to 9 (ignored) also works:
\immediate\write18{echo X >temp.tex}
X{\catcode`\^^M=9 \input temp }X
  • Ignoring and removing the space:
\immediate\write18{echo X >temp.tex}
X\ignorespaces \input temp\relax \removeunwantedspaces X