LaTeX error · Environments, lists and tables
LaTeX Error: Missing \begin{document}.
Quick answer
The preamble, everything between \documentclass and \begin{document}, may only contain definitions and settings. As soon as LaTeX meets something it would typeset (a word, a stray character after a command, or an invisible character) it complains that the document has not started yet. Find the printable text on or just before the l. line in the log and move it below \begin{document} or delete it.
The error in the log
This is how the message appears in the .log file (and in the compile output). The line starting with l. is the line number where TeX noticed the problem; the real mistake is there or just before it.
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.3 D
raft versionWhy it happens
- Text written in the preamble, such as a title or a note, instead of after
\begin{document}. - A stray character after a preamble command, for example
\usepackage{amsmath}.or a leftover]from a\usepackage[...]{...}line. - An invisible character such as a byte order mark (BOM) or non-breaking space at the start of the file, typical after copying from Word or a web page.
- A broken file read in the preamble, for example a
.bibfile passed to\input, or a.texfile with a\begin{document}of its own included from the preamble.
How to fix it
Look at the l. line in the log: LaTeX shows the text it was about to print. Move it below \begin{document}, or delete the stray character.
If the line shown looks empty, the culprit is an invisible character. Retype the line, or save the file as UTF-8 without BOM.
Broken: reproduces the error
\documentclass{article}
\usepackage{amsmath}
Draft version
\begin{document}
Hello.
\end{document}Try this in Octree — free LaTeX editor with AI, compiles in your browser
Fixed: compiles cleanly
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Draft version
Hello.
\end{document}Related errors
- \begin{x} ended by \end{y} – An
\enddoes not match the most recent open\begin: a typo or an environment left open. - Unicode character not set up for use with LaTeX – pdfLaTeX met a pasted Unicode symbol (≈, →, emoji, CJK) that it has no definition for.
- Command \x already defined –
\newcommandused a name that a package or LaTeX already defines. - All LaTeX errors and warnings – The full list, grouped by cause
Frequently Asked Questions
Why do I get "Missing \begin{document}" when my file has \begin{document}?
Because something printable appears before it. LaTeX does not search for \begin{document}; it stops at the first character it would typeset. The l. line in the log shows that character.
Can a package cause "Missing \begin{document}"?
Rarely, but yes: a corrupt or wrongly named file loaded in the preamble, such as a .bib loaded with \input or a .sty saved as HTML, prints text and triggers the error. The log shows which file was open.
How do I find an invisible character in the preamble?
Open the file in an editor that shows invisible characters, or delete and retype the first lines. A byte order mark at the very start is the most common one; saving as "UTF-8 without BOM" removes it.
Paste your document into Octree — compile errors get a Fix with AI button
When a compile fails, Octree shows the error next to the line that caused it and offers a one-click fix that you can review before applying. Free, nothing to install.
Fix it in Octree →