LaTeX error "LaTeX Error: Missing \begin{document}": cause and fix
LaTeX found text, or a command that prints something, before \begin{document}. The preamble may only hold settings and definitions; everything that appears on the page belongs after \begin{document}.
Why it happens
- A word or a stray character stands in the preamble, for example a note without
%in front of it or a letter left behind after a\usepackageline. - You compiled a file that has no
\documentclassand no\begin{document}, such as a chapter that the main file loads with\inputor\include. If that file starts with\section, the log reports Undefined control sequence first. \begin{document}is misspelled, for example\being{document}.- A definition in the preamble lacks the backslash of the new name, as in
\newcommand{name}{...}. LaTeX then treats part of the name as text.
How to fix it
- Go to the line the log names. Move the text below
\begin{document}, or put%in front of it if it is a note. - Compile the main file, the one that contains
\documentclass, and not a file it loads. - Check the spelling of
\begin{document}. - Check every
\newcommandand\newlengthin the preamble: the new name needs a backslash.
A document that produces the message
\documentclass{article}
\usepackage{graphicx}
Draft, 12 March
\begin{document}
The report starts here.
\end{document}The corrected document
\documentclass{article}
\usepackage{graphicx}
% Draft, 12 March
\begin{document}
The report starts here.
\end{document}Both documents were compiled with pdfLaTeX from TeX Live 2023. The first produces the message, the second compiles without an error.
Finding the line in the editor
The editor lists every error and warning of a compile run with file and line. Select an entry and the cursor jumps to that line. See Error list with jump to the line.