50

In plain TeX, \bye marks the end of the TeX document. Yet why is such a command even needed? TeX should be quite able to see for itself that here, the file stops, and there is nothing more to find. I do not see why it needs a command to tell it so.

I realise that LaTeX also has such a command, \end{document}; but that one seems natural because it creates symmetry with \begin{document}.

Gaussler
  • 12,801
  • 5
    You don't need a file, you can input all the stuff using STDIN. At the end of a compile information is written to the auxiliary files and they are properly saved. \end{document} is the LaTeX equivalent. – Johannes_B Jun 07 '15 at 20:09
  • 11
    I think originally DEK just typed away at the TeX prompt, and then typed \bye when he was done... :-) The primitive command that ends the input is \end, and clearly it's helpful to be able to end a complex file at various points depending on what you are doing. – Thruston Jun 07 '15 at 20:10

1 Answers1

57

The reason is in the definition:

\outer\def\bye{\par\vfill\supereject\end}

So \bye doesn't just issue \end (that would by itself issue \par, but not \vfill): it also performs \supereject, which is

\def\supereject{\par\penalty-\@MM}

Why a -20000 penalty? The reason is in the output routine:

\def\plainoutput{\shipout\vbox{\makeheadline\pagebody\makefootline}%
  \advancepageno
  \ifnum\outputpenalty>-\@MM \else\dosupereject\fi}

If the penalty that triggers the output routine is greater than -20000, nothing special is done after advancing the page number. Otherwise \dosupereject comes into action:

\def\dosupereject{\ifnum\insertpenalties>\z@ % something is being held over
  \line{}\kern-\topskip\nobreak\vfill\supereject\fi}

The macro ensures all pending insertions are shipped out, by repeated call of \supereject until \insertpenalties is not positive any more.

One can call \supereject at any time (which is the reason for \par at the beginning), for instance when a chapter is beginning.

Some more ideas from comments to the question and to the answer

TeX can be run interactively and it goes interactive if the main input file ends without an \end command (or makes an “Emergency stop” if the running mode is not interactive). Due to this possible interactivity, an implied \end command would be undesirable.

Only a few operating systems add an implied <EOF> marker at the end of a file, which could be ^^D (EOT) or ^^Z. The ^^Z character used to be added in MS-DOS files, but not all text editors were compliant.

Depending on the operating system and the shell, hitting Control-D at the interactive prompt can stop the execution, but no \end command is executed.

Since TeX keeps a stack of the \input files, it knows when it has reached the end of the main file, so a possible feature could be “insert the token \end when you reach the end of the main file”. However, there should also be a “disable automatic \end” feature to allow interaction at the end of the file. There is a similar feature in TeX: when switching from horizontal to vertical mode due to finding a <vertical command>, TeX adds a \par token (which could not be the primitive \par any more). However, the situation for an “automatic \end” is very different: breaking a paragraph into files is a common TeX activity, while the end of the main file just happen once.

Having the terminating \end allows for adding structure to it. One could redefine \end (after keeping a copy of the primitive) to do other bookkeeping business like \bye does. So

\let\TeXend\end
\outer\def\bye{\par\vfill\supereject\TeXend}

could be thought of, but it was not Knuth's choice (and it wouldn't be mine as well).

Finally, having \end (or \bye or \end{document}) at the end of the main file allows for putting comments and additional material after the end marker, which wouldn't be possible otherwise. Since most programming languages require programs to be explicitly terminated, I don't see why TeX should be an exception.

egreg
  • 1,121,712
  • 8
    And here I thought \bye was a primitive. ;-) – Gaussler Jun 07 '15 at 20:21
  • 1
    I'm surprised the plain TeX output routine is such a short piece of code. OR is supposed to be one of the most difficult aspects of TeX. – Gaussler Jun 07 '15 at 20:46
  • 7
    @Gaussler You should look at \makeheadline, \pagebody and \makefootline. ;-) – egreg Jun 07 '15 at 20:48
  • Why does this need to be a command that is present in documents, though, rather than something (whether a primitive or a macro) that the interpreter does automatically on encountering EOF from input and which authors are not expected to include in their documents? – Random832 Jun 08 '15 at 01:49
  • 1
    @Random832 If it were built into TeX, then TeX could only process plain TeX or macro packages compatible with plainTeX's idea of what to do at the end of input. – Theodore Norvell Jun 08 '15 at 02:16
  • 1
    Requiring every macro package to redefine something like \ateof or \bye or whatever (which would be run automatically on EOF from input) isn't exactly a huge burden. – Random832 Jun 08 '15 at 02:17
  • @Random832 Some people would like to do different things at the end. And actually they do: for instance LaTeX closes the auxiliary file, inputs it and does other bookkeeping. The end of the main file does not issue the \end command, even in non-interactive mode. The choice would be between not issuing \end automatically, or providing a command \donotend for the cases when an automatic \end is not wanted. I prefer having \end, which is in line with most programming languages. – egreg Jun 08 '15 at 06:38
  • 1
    @egreg then they could define the hypothetical \ateof to do what they want, or to do nothing. What I don't understand is why \end or \bye or \end{document} or whatever else is an explicit command that appears in the file, and I think that this is what the question OP is asking really is and this doesn't actually answer it. – Random832 Jun 08 '15 at 11:09
  • @Random832 Only a few operating systems had an implied <EOF> token at the end of files. In particular, systems with fixed record length didn't. Sure, Knuth could have added the feature, so instead of the * prompt when the file ends (better, there are no more records in it) the \end command would be issued. He decided against this. – egreg Jun 08 '15 at 11:39
  • 2
    It's not TeX Live that does something special when Ctrl-D is pressed, but the terminal driver in Unix which (in the default "cooked" mode) converts the Ctrl-D into an ordinary end-of-file event before the TeX process even sees it. TeX will then make the same kind of emergency stop as when an actual input file ends. (Note, for example, that typing ^^D at the * prompt -- that is, hat-hat-D -- does not do this). – hmakholm left over Monica Jun 08 '15 at 13:52
  • @egreg, are you sure \@MM is a 20000 penalty and not a 2000 penalty? That would make more sense, Roman number-wise. – Gaussler Jun 08 '15 at 19:12
  • 2
    @Gaussler \mathchardef\@MM=20000 (M stands for myriad). Note that a penalty of –2000 wouldn't force a page break. – egreg Jun 08 '15 at 19:13
  • Seems Knuth is not as Roman as I thought. ;-) – Gaussler Jun 08 '15 at 19:15
  • 1
    @Gaussler There are also \chardef\@cclv=255 \mathchardef\@cclvi=256 \mathchardef\@m=1000 \mathchardef\@M=10000 – egreg Jun 08 '15 at 19:18
  • 2
    In other words, small letters are Roman. – Gaussler Jun 08 '15 at 19:20