\endinput: what is it for?
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.
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.
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.