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:
- ModernTex: when a build fails, the Build button shows the error count and the last good PDF stays on screen. The Build Results panel lists each error, warning and note in plain language, Compile › Next Issue steps through them, and Raw Log shows the full LaTeX log. The root file's name appears in the toolbar next to the project name. If it is not your main file, click it and pick another under Set Root File. See ModernTex.
- Overleaf: the logs panel, opened with the button next to Recompile, lists each error and warning with its file and line, and the editor marks the line in the gutter. The unprocessed log is at the bottom of that panel.
- TeXShop: the console window shows the log as it is written and stops at the error. Goto Error moves the source window to the line. TeXShop's default pdfLaTeX command includes
--file-line-error, so the same error starts with the file and line in place of the!:./main.tex:3: LaTeX Error: Missing \begin{document}.
Related errors
- File `x.sty' not found: when the main file's
\inputpaths are wrong. - Undefined control sequence: what a chapter file gives when it starts with a command.
- Runaway argument: when an unclosed brace in the preamble swallows
\begin{document}.