# Why does LaTeX throw an error without \begin{document}?

Consider the following "Hello, World!" document.

Hello, world!
\end


If you try to compile this using latex.exe, you get the error**

! LaTeX Error: Missing \begin{document}.


Okay, so on one hand, it should be obvious that one would get this error---this is a bit like trying to compile a C program without a main function. On the other hand, if I am thinking of LaTeX as just TeX with a bunch of macros loaded, one would think that any document that would compile fine using tex.exe should work just as well without latex.exe, at least so long as I'm not using any macros specific to Plain TeX.***

This suggests that my understanding of LaTeX as being effectively equivalent to TeX but with an \input{latex} command at the beginning of the document is incorrect, which thus raises a couple of related questions.

1. My understanding was that loading a TeX format is essentially equivalent to \inputing a file at the beginning of the document, except perhaps that the file has been precompiled. Is this understanding inaccurate?

2. Is a call to latex.exe actually equivalent to a call to tex.exe with the LaTeX format loaded?

3. If in fact running latex.exe is equivalent to just running TeX with the LaTeX format, then how does this format tell TeX to throw an error if it doesn't see a \begin{document}?

** Incidentally, you also get an error related to the fact that \end means something different in LaTeX. I also find this confusing because I didn't think you could redefine TeX primitives, but perhaps this is better saved for a separate question.

*** Actually, I'm not even sure if there are macros specific to Plain TeX that LaTeX doesn't also define.

• latex.ltx : LaTeX must be made using an initex with no format preloaded so the answer to the 1 and 2 is no. Sep 23, 2016 at 22:05
• again latex.ltx : \everypar{\@nodocument} %% To get an error if text appears before the where \@nodocument is error message. and \document redefine \@nodocument to \relax Sep 23, 2016 at 22:11
• @touhami it may say that but it's only a computer program, it can be tricked:-) tex.stackexchange.com/questions/91042/… Sep 23, 2016 at 22:28
• From your question it is not obvious if you want to know the motivation for LaTeX doing this, or how LaTeX technically implements this.
– chtz
Sep 24, 2016 at 22:01
• @chtz Originally I was curious to know how it technically implements this, but I suppose if there is nonobvious motivation for designing it this way, I would also be curious to know. Sep 25, 2016 at 4:15

1. My understanding was that loading a TeX format is essentially equivalent to \inputing a file at the beginning of the document, except perhaps that the file has been precompiled. Is this understanding inaccurate?

More or less true, although you need to interpret that with some care.

1. Is a call to latex.exe actually equivalent to a call to tex.exe with the LaTeX format loaded?

yes but crucially without the plain format being loaded (what was originally called virtex) and ignoring the point that hyphenation patterns can only be loaded in initex, not in a normal run.

1. If in fact running latex.exe is equivalent to just running TeX with the LaTeX format, then how does this format tell TeX to throw an error if it doesn't see a \begin{document}?

latex.ltx sets

 \everypar{\@nodocument} %% To


so every paragraph generates an error, \begin{document} clears this and allows paragraphs to be set.

** Incidentally, you also get an error related to the fact that \end means something different in LaTeX. I also find this confusing because I didn't think you could redefine TeX primitives, but perhaps this is better saved for a separate question.

yes of course you can redefine any primitive

\def\hbox{oops}


will redefine \hbox (not a good idea but ...)

*** Actually, I'm not even sure if there are macros specific to Plain TeX that LaTeX doesn't also define.

yes there are macros defined in plain not defined in latex (\folio for example) and many many commands that are defined with the same name in both formats have different definitions (\loop for example).