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}.

Fails
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}]
\begin{document}
Text.
\end{document}
Fixed
\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.

Fails
\documentclass{article}
\usepackage{amsmath}
% \begin{document}
Text.
\end{document}
Fixed
\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.

Fails
We study the effect of temperature on yield.
\end{document}
Fixed
\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

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

Every example on this page was compiled with pdfLaTeX to check that it fails and is fixed as described. All LaTeX errors