> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/latex-errors/16-latex-error-missing-begin-document.md).

# LaTeX Error: Missing \begin document

This error appears when you have forgotten to include **`\begin{document}`** in your document. It can also appear if you have not written **`\documentclass[...]{...}`** at the beginning of your main document.

## Common Causes

**Forgetting to include \begin{document}:**

The most basic cause of this error occurs when you have forgotten to write **`\begin{document}`** after the preamble in your main document. If you make this mistake, you will generate the error message shown below:

main.tex, line 5

LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ... You're in trouble here. Try typing to proceed. If that doesn't work, type X to quit.

To resolve this error, simply make sure that you have included **`\begin{document}`** after the preamble in your main document. See [here](/latex/latex-basics/01-learn-latex-in-30-minutes.md) to learn more about how to structure documents in LaTeX.

**\documentclass\[...]{...} in wrong place:**

Another common cause of this error is if **`\documentclass[...]{...}`** is written in the wrong place. This must be written at the start of the main document. If you have a separate file for your preamble, you **cannot** include **`\documentclass[...]{...}`** in that file. It still must appear at the top of the main project file, or else only the preamble file will be compiled.

**Main document set incorrectly:**

This error may also appear if you have set the main document incorrectly in the left hand menu. This can happen if **`\documentclass[...]{...}`** is written in the wrong place, so you should correct this first if needed. To change the choice of main document, open the left hand menu:

![Lefthandmenu.PNG](/files/N069lBukH6jO54osMq5e)

and go to the *Main document* drop down menu:

![Maindocument.PNG](/files/Ud4ZORFsRkn1MusL2cs5)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/latex-errors/16-latex-error-missing-begin-document.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
