How TeX reads input

From Wiki
Jump to navigation Jump to search

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


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 ...