Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've noticed a significant issue with the \input command, it seems. For some reason, I can't seem to prevent it from adding a space after it reads its input. Even if there is no newlines or spaces at the end of the file, LaTeX seems to be just driven to add a space.

Minimal working example is this:

\documentclass{article}
\begin{document}
A\input{TestInput.out}B
\end{document}

Where TestInput.out contains only the text "SomeText" and no special characters

This provides the output:

ASomeText B

Does anybody know how to prevent this? For a typesetting program, it's kind of weird to just arbitrarily add spaces IMO. I'd like to think there's an easy fix, but I'll take any portable, robust fix if it works.

share|improve this question

2 Answers 2

up vote 13 down vote accepted

TestInput.out probably contains a new line at the end of the file (text files usually do, even when created with echo 'abc' > file). TeX interprets new lines as spaces.

To remove that space, simply add \unskip directly afterwards, i.e. use A\input{TestInput.out}\unskip B (this will also remove any intentional space at the end of the file, should there be one).

If you have control over TestInput.out, you can alternatively add % to the end of the last line of text in the file. This will comment out the final newline character so that TeX will ignore it. One more way is to end the file with \endinput.

share|improve this answer
2  
Files are usually \input when they contain full paragraphs or definitions, so the problem of an empty line at the end shows rarely. One might do with A\input{test.out}\unskip B. –  egreg May 11 '11 at 22:54
    
@egreg: It seems I edited the answer while you were writing the comment. No idea, why I only thought of \unskip after pressing “Post Your Answer”. –  Caramdir May 11 '11 at 23:00
    
Strangely enough, I was pretty sure there shouldn't be a newline at the end of the file- at least according to Notepad++ with special characters displayed. With that said, both the % and the \endinput work like a charm. I'll also try to keep the unskip in mind, but I don't want to wipe out any intentional whitespace when reading the file. –  Namey May 12 '11 at 0:40
    
@Namey: It only removes whitespace at the end of the file (eg, if the file contains some words , then the result will be some words). –  Caramdir May 12 '11 at 0:43
3  
@Namey: It doesn't matter if the final line ends with a newline or not. The point is, TeX sees the file as consisting of a bunch of lines. As part of the tokenization process, a character whose code is the value of \endlinechar is inserted at the end of each line (except when the line ends with a comment or a control sequence). –  Harald Hanche-Olsen May 12 '11 at 7:00

For easy access you can define a new function \inputy (for example)

\documentclass{article}
\newcommand{\inputy}[1]{\input{#1}\unskip}
\begin{document}
A\inputy{TestInput.out}B
\end{document}

I used this a lot in my dissertation, to introduce obtained numbers from output txt files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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