10

Is there a technical reason why the \bye TeX command was replaced in LaTeX by \begin{document} and \end{document}?

I suspect there must be a good technical reason behind this, as otherwise it would be just window dressing. I cannot imagine otherwise replacing a 4 character command placed at the end of the document with two separate commands, one 16 characters, and the other a 14 character command, having to be placed at the beginning and end.

8
  • 8
    \bye and \end{document} do completely different things, and environments are the main syntactic construct introduced by latex, so why do you find this strange? Commented Apr 12, 2016 at 16:20
  • 1
    Also what syntax would you have used for \begin{document}? Something close to \bye is available in latex namely \stop but it should never be used in normal documents, it's useful in some test cases and error trapping code. Commented Apr 12, 2016 at 16:25
  • 2
    @egreg I suspect that even you would manage \def\bye{\end{document}} :-) Commented Apr 12, 2016 at 16:32
  • 3
    @DavidCarlisle You're forgetting \outer. :-P Commented Apr 12, 2016 at 16:34
  • 3
    Totally honored to have both of you commenting on my question. I've arrived. Commented Apr 12, 2016 at 16:58

2 Answers 2

14

Let's look at how \enddocument is defined (see latex.ltx, the LaTeX "kernel"):

\def\enddocument{%
   \let\AtEndDocument\@firstofone
   \@enddocumenthook
   \@checkend{document}%
   \clearpage
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse
       \makeatletter \@@input\jobname.aux
     \fi
     \@dofilelist
     \ifdim \font@submax >\fontsubfuzz\relax
       \@font@warning{Size substitutions with differences\MessageBreak
                  up to \font@submax\space have occurred.\@gobbletwo}%
     \fi
     \@defaultsubs
     \@refundefined
     \if@filesw
       \ifx \@multiplelabels \relax
         \if@tempswa
           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}%
         \fi
       \else
         \@multiplelabels
       \fi
     \fi
   \endgroup
   \deadcycles\z@\@@end}

A full discussion of what all is being executed would be very tedious. Among the more interesting activities, in my view..., are (a) the \clearpage instruction, which flushes all pending floats, (b) the closing of the aux file, and (c) a check if some cross-references are still unresolved; if there are still-unresolved references, the following famous warning message about needing to rerun LaTeX is generated:

           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}%

The definition of \bye (see p. 357 of the Texbook) follows a completely different model:

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

Finally, here's the definition of \stop, a macro mentioned in a comment by David Carlisle:

\def\stop{\clearpage\deadcycles\z@\let\par\@@par\@@end}

(\@@end is a LaTeX-internal version of the "primitive" command \end.) Can you tell why David added that "it should never be used in normal [LaTeX] documents"?

7
  • I guess it should not be used because all your references would be off, as well as other not so good things would happen. Good answer. I looked for a \def\begindocument line in the kernel and didn't find it. Is there a similar function defined for \begin{document}? Commented Apr 12, 2016 at 18:55
  • @AFeldman - Thanks. Look for \document, lines ca 1619 to 1668 of the current version of latex.ltx. :-) Commented Apr 12, 2016 at 19:00
  • 1
    I don't see \bye as "completely different model". (a) All inserts are flushed by \end primitive too and \dosupereject mcro does a little more. (b) all opened files are closed automatically when TeX ends. (c) is .aux specific and plain TeX does not provide .aux. Commented Apr 12, 2016 at 19:12
  • 1
    @user1717828 - \stop only performs \clearpage (which is done early by \enddocument) and \deadcycles\z@\@@end (the very last few instructions performed by \enddocument). That's why David C. wrote that \stop may be suitable for "some test cases and error trapping code" -- but not for normal documents which, almost certainly, need to do write to and read back from the aux file. Commented Apr 12, 2016 at 19:40
  • 1
    @AFeldman the macro \begin checks whether its argument is defined, while \end requires an explicit \end<something> command, to ensure that the group is closed. So for some environments you can state \myenv ... \endmyenv, but that would take away the syntactic sugar of the current LaTeX environment mechanism. Commented Apr 13, 2016 at 17:46
16

There are syntactical reasons. LaTeX has the concept of environments, not present in plain TeX. The environment document is the outer environment of the document contents.

The setup/configuration of the document is separated from the actual document contents and this is done in the preamble before the environment document starts. The preamble loads the class and packages, which is not allowed in environmentdocument.

Also \begin{document} introduces the concept of document initialization, again not present in plain TeX. For example, the .aux file is read in \begin{document} (and again checked in \end{document}.

The environment form enforces proper nesting of environments:

\begin{document}
  \begin{table}
    \begin{quote}
      \begin{tabular}{l}
        % Compare \bye vs. \end{document} here
      \end{tabular}
    \end{quote}
  \end{tabular}
\end{document}

If \bye is the normal form of ending a document, then it can be written inside other environments in a syntactically correct appearance, see the previous code. But the wrong place becomes obvious, when written as \end{document}.

5
  • I guess I am sincerely confused. I thought that Plain Tex has environments using \def\begin and \def\end Commented Apr 12, 2016 at 17:12
  • 1
    Plain TeX does not have environments. \begin is not defined and \end is a low level primitive command for ending the TeX job (LaTeX saves and internally uses it as \@@end). Commented Apr 12, 2016 at 17:16
  • So a \begingroup \endgroup in Plain Tex is not declaring the beginning and end of an environment. I'm struggling with that idea. Commented Apr 12, 2016 at 18:31
  • \begingroup and \endgroup is a different concept. They start and end a group, where assignments are local. Environments are more, definable code at the begin and end (even with arguments for the begin part), extended syntax checking, ... Commented Apr 12, 2016 at 18:36
  • 1
    Thanks for your patience with me. I think that I have a ways to go before I really understand the distinction. But that is not the fault of your explanation, more that I need to learn more before the explanation makes sense to me. Commented Apr 12, 2016 at 18:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.