58

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.

6 Answers 6

56

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.

2
  • 7
    use of \endinput at the end of a file avoids pollution added by some mailers to unzipped text files. it is useful there. Sep 24, 2011 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, 2011 at 17:36
62

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 \ifstudents, with

\newif\ifstudents
%\studentsfalse % this is implicit
%\studentstrue % uncomment for the students' version

in the preamble, and prepare my chapters as

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

\ifstudents\endinput\fi

\section*{Solutions}
<solutions>

Now it suffices to switch \studentsfalse (for the teachers' edition) and \studentstrue (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.

3
  • 2
    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. Jul 19, 2011 at 5:32
  • Since you are using \ifstudents, shouldn't the switches be \studentsfalse and \studentstrue (i.e., with "s" added)? And it might be worth noting you should have a \newif somewhere.
    – Tom Loredo
    Nov 10, 2020 at 17:34
  • @TomLoredo You're right in both respects.
    – egreg
    Nov 10, 2020 at 18:04
13

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 \included 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.

2
  • @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, 2011 at 19:26
  • Yes, it felt a little silly :). I have fixed this but the vote up remains. Jul 18, 2011 at 6:22
12

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 \included 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.

0
9

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

3
  • 1
    Thanks. Are you referring to the file url.tex or url.pdf? I have searched through url.tex but couldn't find \endinput. Jul 15, 2011 at 6:33
  • 5
    look for url.sty. LaTeX packages have the extension .sty
    – user2478
    Jul 15, 2011 at 7:16
  • 1
    Thanks. I have been able to find the command \endinput in url.sty Jul 16, 2011 at 6:41
9

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.

2
  • abc% would have the same effect, wouldn't it? Jul 15, 2011 at 8:25
  • Yes, @MartinScharrer. It has the same effect for that precise example. Then, if you want to comment beneath \endinput you must be sure to add a % to all empty lines too, or all newlines would be included.
    – mazunki
    Sep 1, 2019 at 23:46

You must log in to answer this question.

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