Sarmate.net Sarmate.net
Home Features Pricing Documentation Contact
Log in Sign up

Missing \begin{document}: what this LaTeX error means

This message means: LaTeX found before \begin{document} something that produces visible text. Two common causes: text in the preamble or a badly encoded accent.

Cause #1: text in the preamble

Everything you want to write goes after \begin{document}. Text before it triggers the error:

LaTeX
\documentclass{article}
Hello!   % text in the preamble -> Missing \begin{document}
\begin{document}
...
\end{document}

Move the text after \begin{document}:

LaTeX
\documentclass{article}
\begin{document}
Hello!
\end{document}

Cause #2: a badly encoded accent

An accent (é, è, à…) in the preamble, combined with an old inputenc declaration, is read as text and triggers the same error. Fix: modern \usepackage[utf8]{inputenc} + \usepackage[T1]{fontenc} (see Old packages).

The reflex Remember the structure: \documentclass\usepackage (preamble) → \begin{document} → your text → \end{document}. Nothing visible before \begin{document}.

Try it yourself

This example compiles. Move the word "Hello" above \begin{document} and recompile to see "Missing \begin{document}".

The method

  1. Check that nothing visible comes before \begin{document} (only \documentclass and \usepackage).
  2. Move any text after \begin{document}.
  3. An accent in the preamble? Modernize inputenc/fontenc to UTF-8.
  4. Is a } missing in a \usepackage[…]{…}? That can also cause this error.
sarmate.net tip Our online editor reads the compilation log and tells you the likely cause in plain words, so you unblock without guessing.

An AI can fix the error — directly in your code

AI assistants (Claude, Gemini, ChatGPT…) know LaTeX very well: show them the preamble and they find the text or accent breaking things before \begin{document}. The usual drawback is the back-and-forth — copy, paste, repeat.

sarmate.net removes this round-trip with its MCP server: you connect the AI directly to your online editor. It reads your files, compiles them, analyzes the error log and fixes the code in place, with no copy-paste. You stay in control: nothing is changed without your approval.

Write and compile LaTeX online

Full editor, instant compilation (pdfLaTeX/XeLaTeX/LuaLaTeX), nothing to install.

Open the online editor