3

Consider some file file.tex only containing the following:

\hfill Some text

We input this into the main file:

\documentclass{article}
\begin{document}
    \input{file}
    
    \hfill Some other text
\end{document}

Observe that for the input file, some horizontal space has been added on the right: enter image description here

Where does this space come from? How can it be removed?

2 Answers 2

3

I don't know where the space came from, but ending the line with a comment removes it:

\documentclass{article}
\begin{document}
    \input{file}% <- notice the comment
    
    \hfill Some other text
\end{document}

enter image description here

1
  • that end of line is the space Jul 5, 2022 at 17:57
4

TeX always adds a trailing endline to an \input file, which becomes a space in output. The endline after the brace adds another space. Note that tokenization has already been performed, so you get two spaces in output.

On the other hand, \par (generated by the following empty line) does \unskip, which is able to remove one of those spaces and the other one remains.

If you add \endinput to your file, this doesn't happen. Note that the suggested % only works in the particular case.

\begin{filecontents*}[force]{\jobname-in.tex}
\hfill Some text\endinput
\end{filecontents*}

\documentclass{article}

\begin{document}

\noindent X\dotfill X

\noindent\input{\jobname-in}

\noindent\hfill Some other text

\bigskip

\noindent\input{\jobname-in}and other text

\noindent\input{\jobname-in} and other text

\noindent\hfill Some other text

\end{document}

enter image description here

If you remove the \endinput, you get

enter image description here

where you can clearly see two spaces in the middle line of the second group. Here % would be of no use.

Alternative to \endinput is to do \input in the scope of

\everyeof{\relax}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .