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.
The message
Section titled “The message”./main.tex:3: LaTeX Error: Missing \begin{document}.l.3 DThe l.3 line shows the first character LaTeX tried to print, here the D at the start of line 3.
Why it happens
Section titled “Why it happens”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
\maketitleor\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
\documentclassof its own.
Example
Section titled “Example”\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 %:
\documentclass{article}\usepackage{amsmath}% Draft notes for the introduction.\begin{document}Hello.\end{document}If Luzbase compiles the wrong file
Section titled “If Luzbase compiles the wrong file”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.