> 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/zh-cn/latex-cuo-wu/16-latex-error-missing-begin-document.md).

# LaTeX 错误：缺少 \begin document

当你忘记包含 **`\begin{document}`** 在你的文档中。如果你没有写入，也可能会出现 **`\documentclass[...]{...}`** 在主文档的开头。

## 常见原因

**忘记包含 \begin{document}：**

此错误最基本的原因是你忘记写入 **`\begin{document}`** 在主文档的导言区之后。如果你犯了这个错误，就会生成如下所示的错误信息：

main.tex，第 5 行

LaTeX 错误：缺少 \begin{document}。

请参阅 LaTeX 手册或 LaTeX Companion 以获得解释。输入 H 可立即获得帮助。... 你现在遇到麻烦了。请尝试输入以继续。如果那不行，输入 X 退出。

要解决此错误，只需确保你已经包含了 **`\begin{document}`** 在主文档的导言区之后。参见 [这里](/latex/zh-cn/latex-ji-chu/01-learn-latex-in-30-minutes.md) 以了解更多关于如何在 LaTeX 中构建文档的信息。

**\documentclass\[...]{...} 放在了错误的位置：**

此错误的另一个常见原因是 **`\documentclass[...]{...}`** 被写在了错误的位置。它必须写在主文档的开头。如果你的导言区有单独的文件，你 **不能** 包含 **`\documentclass[...]{...}`** 在那个文件中。它仍然必须出现在主项目文件的顶部，否则只会编译导言区文件。

**主文档设置错误：**

如果你在左侧菜单中错误地设置了主文档，也可能会出现此错误。这种情况可能发生在 **`\documentclass[...]{...}`** 被写在了错误的位置，因此如有需要，你应先纠正这一点。要更改主文档的选择，请打开左侧菜单：

![Lefthandmenu.PNG](/files/92774bcd1bff9309617ff93edcc3ec422f24a57d)

并前往 *主文档* 下拉菜单：

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


---

# 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/zh-cn/latex-cuo-wu/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.
