LaTeX Error: Missing \begin{document}
Something that prints text sits above \begin{document}, or that line is missing. Only settings belong in the preamble; find the stray text and move it.
Help centre › LaTeX errors › Document structure · Error: the build stops or the output is wrong
What the log says
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.3 \usepackage{amsmath}]What it means
Everything above \begin{document} is the preamble, where only settings may go: \documentclass, \usepackage, definitions, \title and the like. As soon as LaTeX meets anything that would print, even a single stray character, it stops with this error, because there is no page to print it on yet.
Causes and fixes
Most common first. Each example is a complete document; the first fails with this message, the second is the same document fixed.
1. A stray character or line of text in the preamble
Look at the end of the line in l.<n>: a leftover ] or } after a \usepackage, a word typed in the wrong place, or a paragraph pasted above \begin{document}. Delete it or move the text below \begin{document}.
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}]
\begin{document}
Text.
\end{document}\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\begin{document}
Text.
\end{document}2. \begin{document} is missing, commented out or misspelled
Check that the main file has exactly one \begin{document} line, not preceded by %, after the last \usepackage, and that \end{document} is the last line.
\documentclass{article}
\usepackage{amsmath}
% \begin{document}
Text.
\end{document}\documentclass{article}
\usepackage{amsmath}
\begin{document}
Text.
\end{document}3. The file has no beginning at all
A paper pasted into an empty main file, or a chapter compiled on its own, has no \documentclass and no \begin{document}; LaTeX then repeats this error for every paragraph. Put the beginning back: \documentclass{article} (or your journal's class), its \usepackage lines and \begin{document}, with \end{document} at the very end. If the file is a chapter, compile the main file instead.
We study the effect of temperature on yield.
\end{document}\documentclass{article}
\begin{document}
We study the effect of temperature on yield.
\end{document}4. A command that typesets is used in the preamble
Commands such as \maketitle, \section, \noindent or \vspace print something and belong after \begin{document}. Settings that only store a value (\title{…}, \author{…}, \date{…}) are fine in the preamble.
Finding the line when the line number does not help
- The line in l.<n> is where printing started; the culprit is at the end of that line or on it. If l.<n> is \begin{document} itself, the stray text is in a file loaded in the preamble, such as a custom .sty or an \input file.
In Manulyx
The editor shows the error beside the line where printing started. When the main file has no \documentclass and no \begin{document} at all, it says so first and links every following error to that one cause.
Write in Manulyx for free · Journal templates
More in document structure
- LaTeX Error: Something's wrong--perhaps a missing \itemA list has text before its first \item, or no items at all; an empty thebibliography is a common case. Start every list with \item.
- LaTeX Error: Lonely \item--perhaps a missing list environmentAn \item appears outside any list: the \begin{itemize} or \begin{enumerate} is missing, or the list was closed too early.
- LaTeX Error: \include cannot be nestedA file loaded with \include contains another \include. Only the main file may \include; inside an included file use \input instead.
- Package hyperref Warning: Token not allowed in a PDF string (<encoding>):A heading contains math or a command that cannot go into a PDF bookmark. The page is fine; give the bookmark plain text with \texorpdfstring.
Every example on this page was compiled with pdfLaTeX to check that it fails and is fixed as described. All LaTeX errors