# \endinput: what is it for?

I am working on a big document and I am using the `\include` command to put chapters one after the other. On the internet I came to know about the existence of the `\endinput` command. However I do not understand how to use it. My questions:

1. If I have a LaTeX chapter file named `chapter1.tex`, should I place the `\endinput` command on the last line in the file `chapter1.tex`?

2. If yes for Question 1, then should I do the same for every other chapters that I have?

I have never used `\endinput` before and everything has worked fine so far.

-

No, you don't need it.

`\endinput` is used for terminating the input process in the middle of a file. A `\endinput` at the end of a file is useless (and harmless).

Some people like to use `\endinput` to show the end of file explicitly. IMHO, it make no sense.

-
Thanks for your answer. 1 vote up. – yCalleecharan Jul 15 '11 at 5:32
use of `\endinput` at the end of a file avoids pollution added by some mailers to unzipped text files. it is useful there. – barbara beeton Sep 24 '11 at 15:36
@babara: Thanks, I didn't know that. In this case, it is indeed useful. I know some usage of `\endinput`, e.g. for pure text documentation as Herbert said. This is a strange usage for me. – Leo Liu Sep 24 '11 at 17:36

Here's a case where `\endinput` might be useful. Suppose I'm writing a textbook and that each chapter ends with problems followed by their solutions. I want to build two separate editions: one for students, without solutions, one for teachers, with solutions.

I can define a conditional, say `\ifstudent`, and prepare my chapters as

``````<text>
\section*{Problems}
<problems>

\ifstudents\endinput\fi

\section*{Solutions}
<solutions>
``````

Now it suffices to switch `\studentfalse` (for the teachers' edition) and `\studenttrue` (for the students' edition).

An important feature of `\endinput` is that TeX continues to read to the end of the line where `\endinput` appears (when it is expanded), so something can follow it and be read nonetheless. The following would be equivalent

``````\ifstudents
\expandafter\endinput
\fi
``````

Forgetting the `\expandafter` would cause an "incomplete conditional" error message.

-
You have indeed shown a very good example. Is it possible that you make a more complete example of this to show how it works. I have tried with the command fragments that you gave and it is not working. Thanks. – yCalleecharan Jul 19 '11 at 5:32

if you are sending a multi-file manuscript to a publisher that uses a different production platform than you do, `\endinput` at the end of chapter files to be `\include`d will serve to avoid possible problems with different end-of-line characters or addition of spurious garbage at the end of a file to fill out a fixed-length block. these conditions are rare, but quite nasty when they do occur.

-

Beside allowing comments at the end of the file `\endinput` (or any other command) can also be useful to swallow spaces introduced by the end-of-file. E.g. if your input file contains `abc` `x\input{file}y` will give `xabc y`. If the input file is `abc\endinput` you will get `xabcy`.

-
`abc%` would have the same effect, wouldn't it? – Martin Scharrer Jul 15 '11 at 8:25

if one has the documentation of a package inside the package itself he can use `\endinput` before the documentation text starts, e.g. package `url`

-
Thanks. Are you referring to the file url.tex or url.pdf? I have searched through url.tex but couldn't find \endinput. – yCalleecharan Jul 15 '11 at 6:33
look for `url.sty`. LaTeX packages have the extension `.sty` – Herbert Jul 15 '11 at 7:16
Thanks. I have been able to find the command \endinput in url.sty – yCalleecharan Jul 16 '11 at 6:41

The `\include` macro uses the `\input` macro internally to read the given file. The `\endinput` macro simply ends the input of this file, i.e. allows you to have everything afterwards ignored and return to the parent file immediately. As Herbert already stated this is useful for package or similar files which have its documentation in them and place them just after `\endinput` so is isn't interpreted it as LaTeX code.

For own chapters inside `\include`d file `\endinput` can be useful if you want to temporary ignore a trailing portion of this chapter. Together with `\includeonly` it allows you to produce a partial PDF just holding e.g. the first section of a chapter. This can be very useful in the writing phase e.g. if you are supposed to sent such a part of your document to another person for review.

AFAIK the guide for LaTeX package authors states to end class and package files with an explicit `\endinput`. I personally find it good programming style. I also remember hearing about some tricks which require an explicit `\endinput` in the file they process, which I think is then temporary redefined.

In summary, you don't have to worry about `\endinput`. It's totally fine if you don't use it at all.

-
@yCalleecharan: It's nice but not necessary to write 5 of such nearly identical comments. Upvoting shows thanks to a good answer. Just imagine everybody would do it - comments with additional information or queries would be harder to see. It would be great if you use the comment feature just if you want to add also information. – Stefan Kottwitz Jul 16 '11 at 19:26
Yes, it felt a little silly :). I have fixed this but the vote up remains. – yCalleecharan Jul 18 '11 at 6:22