Purplelink
← All LaTeX errors

Missing \begin{document}

LaTeX found something to print before \begin{document}. The line is usually there; something printable sits above it.

What it means

LaTeX found something to typeset, a letter, a digit or any other printable character, before \begin{document}. Only declarations belong in the preamble: \documentclass, \usepackage, \newcommand, \title and the like. The message does not mean the \begin{document} line is absent. It means text arrived before it. The usual causes are stray words in the preamble, a preamble command that lost its backslash, or compiling a file that is not the main file of the project.

The log line

This small file triggers the error:

\documentclass{article}
\usepackage{amsmath}
Draft of the introduction.
\begin{document}
Body text.
\end{document}

Compiled with pdfLaTeX (TeX Live 2026), it writes this to the .log file:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.3 D
     raft of the introduction.
You're in trouble here.  Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

The l.3 line breaks after the first character LaTeX could not place, here the D of Draft. Line 3 of this file is a sentence sitting between \usepackage{amsmath} and \begin{document}.

To try a fix without touching your project, save the example as a .tex file and compile it online with the LaTeX to PDF tool.

Why it happens

The causes below are in order of likelihood. Each has a before and an after you can compare with your own source.

1. Text in the preamble

A note, a title typed without \title{...}, or a line that was meant to be a comment. Move it below \begin{document} or put a % in front of it.

Before

\usepackage{amsmath}
Draft of the introduction.

After

\usepackage{amsmath}
% Draft of the introduction.

2. A preamble command without its backslash

Without the backslash, usepackage{booktabs} is just a word, and LaTeX tries to print it.

Before

usepackage{booktabs}

After

\usepackage{booktabs}

3. Compiling a chapter or section file

A file pulled in with \input or \include has no preamble of its own. If the editor compiles that file, the first word in it raises this error at l.1. Compile the main file. Many editors also accept a root-file comment at the top of the chapter.

Before

% chapters/review.tex, compiled directly
This chapter reviews prior work.

After

% !TEX root = ../main.tex
This chapter reviews prior work.

4. \begin{document} is commented out or was deleted

If the line is really gone, the first sectioning command or word of the body triggers the error.

Before

% \begin{document}
\section{Introduction}

After

\begin{document}
\section{Introduction}

5. A package file that is really a web page

Saving a .sty file from a website sometimes saves the HTML page around it. LaTeX loads the file in the preamble and trips over the first character of the HTML. Open the .sty file: it should start with TeX code. Download the raw file again.

Before

% journalstyle.sty starts with <!DOCTYPE html>
\usepackage{journalstyle}

After

% journalstyle.sty starts with \ProvidesPackage
\usepackage{journalstyle}

How to find the line

The l. number is reliable here, but check which file it refers to. The log lists each file in parentheses as it opens it, and the last one opened before the error is the file with the stray text. If that is a chapter file, you are compiling the wrong file. If it is a .sty or .cls file, the problem is inside that file.

When you run pdflatex in a terminal without -interaction=nonstopmode, TeX stops at the error and prints a ? prompt. Press Return to carry on past the error, type x to quit, or type h for TeX's own help text, which is the paragraph you see after the l. line in the log. Editors run TeX in non-stop mode, so you get the log and never see the prompt.

What each editor shows:

Related errors

Frequently asked questions

I have \begin{document} in my file. Why does LaTeX say it is missing?
The message means something printable came before \begin{document}, not that the line is absent. Look at the line number in the log: the stray text or mistyped command is in the preamble at that line.
Why do I get this error when I compile a chapter file?
A chapter or section file pulled in with \input or \include has no \documentclass or \begin{document} of its own. Compile the main file, or tell your editor which file is the root.
Can a package file cause Missing \begin{document}?
Yes. If a .sty or .cls file contains plain text, LaTeX tries to typeset it while it is still in the preamble. This happens when a download saved a web page under the .sty name. Open the file: it should start with TeX code, not HTML.