Consider the following :





with foo1.tex containing "B", foo2.tex containing "B%" and foo3.tex containing "B\n%".

In the first case, we obtain "AB C", in the second "ABC" and in the third "AB C" and in the fourth "AB C" (with double space).

Up to here we can think that A\input{foo}C is equivalent to A<insert foo>\n C.



with bar.tex containing "a=b" will not produce the expected error (two \n makes an empty line inside the equation).


So my question is : what \input{foo} is equivalent to ? Is there a conditional addition of \n when the file does not finish by % or something ?

I'm not asking how to get rid of the extra space, but, on the contrary, how to conserve this extra space when replacing \input{foo} by the content of foo.bar.

Non equivalence with an extra newline

Let foo.tex containing only "A"


will produce the same as "A\nB" but

\input{foo} B

will produce a larger space between A and B wich is not equivalent to "A\n B".

  • 1
    TeX buffers the lines of the \input file and, when it ends with no empty line, the search for another ^^M (of the usual catcode 5) ends. – egreg Jun 30 '16 at 15:58
  • So if the last character of the inputed file is \n (and maybe some blanks after), it just puts the file as it is. But if the file does not ends with \n, it will add a one if the next character after \input{foo} is not \n. Is it so ? What about a file which finishes by a comment ? – Laurent Claessens Jun 30 '16 at 18:59
  • TeX implicitly adds an end of line at the end of an \input file – egreg Jun 30 '16 at 19:39
  • Not always, as shows the example with the equation. Since there is a end of line after the input, adding a new one would provoke the error of having an empty line inside an equation. I think that TeX adds an end of line if the \input{foo} is not itself followed by an end of line. – Laurent Claessens Jul 1 '16 at 6:33
  • Last two questions. 1 : what kind of space is the newline converted to ? 2 : where can I learn that kind of stuff by myself ? – Laurent Claessens Jul 1 '16 at 16:15

I guess that with \n you are meaning a “new line”.

Since file systems can do different things to files, TeX always adds an implicit end-of-line character at the end of input files. Here I'll talk about the primitive \input.

So your foo1.tex file can be seen to contain B<newline>, foo2.tex contains B%<newline> and foo3.tex has B<newline>%<newline>. The first two files have one record, the third one has two.

The <newline> is substituted by the \endlinechar, in our case ^^M of category code 5.

The action of a category code 5 character is to trigger insertion of a space token in the token input stream and to set TeX in state N. Normally, state N is what triggers insertion of a \par token if, after skipping blanks at the next line, only another ^^M is found.

When TeX starts reading an \input file, it is forced in state N, whatever the state was when \input was scanned. However, TeX resumes the state it was in before \input, when the file has been completely read in.

So you get the space (or the double space), because the last ^^M is not followed by ^^M in state N.

Exception (module 538 of tex.web): if the \input file is empty, it is considered to consist of a single empty line, which triggers \par.

It's the same with LaTeX, because \input eventually calls \@@input, which is the primitive \input.

Thus the behavior in the equation example is essentially the same as the behavior in the example

A\input foo1

that's equivalent to A\input{foo1}C in LaTeX syntax. No \par is inserted.

  • That explains why my try to add an explicit "^^M ^^M" did not worked (the second one is dropped). – Laurent Claessens Jul 1 '16 at 23:15
  • @LaurentClaessens When ^^M is scanned (either automatically or manually inserted), TeX throws away whatever remains on the line. – egreg Jul 1 '16 at 23:16
  • Well. I more or less understand how TeX does. But I still cannot figure out how to simulate the behaviour. Of course an explicit double ^^M ^^M does not work and none of the combinations like \, fits the correct space that is inserted by \input. My motivation is to write a program that replaces the \input by the content. – Laurent Claessens Jul 1 '16 at 23:26

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.