For my project to bring scripting to (Xe)(La)TeX, I need to execute command lines with write18, redirect output to a temporary file, and then insert the produced text using \input. I've noticed some extraneous whitespace that occurs right after each output, and I managed to hunt down one source down to the most basic part of the equation (I'm observing three different amounts of whitespace; the one discussed here is the smallest amount, so it seems that there is more than a single source for that phenomenon).

here is a 'sub-minimal' MWE:

xxxx xxx

Assuming that the contents of said file consists of a single x (which I can easily prove by running cat /tmp/out.txt:

$ cat /tmp/out.txt
x$ ▌

See how the x is directly followed by the command line prompt), I still get a space after the fourth x in the second line of the resulting pdf file. The second line is matching perfectly with the third line, which does contain a deliberate space; it should for all intents and purposes match with the first line, of course.

This looks as if — contrary to the descriptions I've read — the command \input actually inserts the file contents plus (at least) a single (?) \x20 character into the document.

Can this observation be corroborated? And, importantly: how do i get rid of that extraneous space?

  • 1
    Try xxx\input{/tmp/out.txt}\ignorespaces xxx – Sigur Jan 26 '14 at 17:26

that is correct, your external file has one line and the lineend is replaced by a space.

Use \input{/tmp/out.txt}\unskip or insert a % at the end of the x in the external file: x%

  • thx a lot! that was real fast! – flow Jan 26 '14 at 18:01
  • @Herbert What if I only want one space after the \input{} object? A \input{file.txt} B produces two spaces before B, whereas A \input{file.txt}\unskip B produces no space before B. How can I get one space? – Dr. Beeblebrox May 27 '16 at 12:45
  • Answering my own question: A \input{file.txt} \unskip \ B – Dr. Beeblebrox May 27 '16 at 12:52

If the files you input in that way have a single line, then you can define

  \begingroup\endlinechar=-1 \input{#1}\endgroup

which would remove the trailing end of line. Then


will not have the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.