Skip to content
Get started

Fix "Missing \begin{document}" in LaTeX

This error means LaTeX found something to print before the document started. You will learn where to look and how to fix it.

./main.tex:3: LaTeX Error: Missing \begin{document}.
l.3 D

The l.3 line shows the first character LaTeX tried to print, here the D at the start of line 3.

The preamble, everything between \documentclass and \begin{document}, may only hold settings such as \usepackage and \newcommand. LaTeX raises this error when it finds printable content there:

  • Plain text or notes left in the preamble.
  • A stray character, such as a letter typed by accident.
  • A command that prints something, such as \maketitle or \section, placed before \begin{document}.
  • The file being compiled is not your main file. For example, a chapter file that starts with text and has no \documentclass of its own.
main.tex (broken)
\documentclass{article}
\usepackage{amsmath}
Draft notes for the introduction.
\begin{document}
Hello.
\end{document}

Delete the text, move it below \begin{document}, or turn it into a comment with %:

main.tex (fixed)
\documentclass{article}
\usepackage{amsmath}
% Draft notes for the introduction.
\begin{document}
Hello.
\end{document}

Luzbase picks the main file automatically. There is no setting to choose it in a project, so the most reliable setup is to have your main file called main.tex at the top level of the project. See The main file for how it is chosen.

Not on Luzbase yet? It compiles LaTeX in your browser and links each error to its line, often with the fix. Start writing free.