> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-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://ayakaleaf-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.
