> 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/zhi-shi-ku/127-set-main-document.md).

# 设置主文档

Overleaf 中的主文档是 LaTeX 将被告知首先编译的文件。它是项目的主文件，所有其他部分（类、宏包、章节等）都会被输入到其中。当没有手动设置时，Overleaf 会在项目中选择一个包含 **`\documentclass[...]{...}`** 命令的文档。因此，如果你有一个单独的导言文件，最好不要把 **`\documentclass[...]{...}`** 命令包含在导言文件中，而应放在主文档的开头。如果不这样做，只有导言部分会被编译。要在项目中设置主文档，只需点击左上角的菜单：

![左侧菜单.PNG](/files/92774bcd1bff9309617ff93edcc3ec422f24a57d)

并转到 **主文档**:

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

参见 [这里](/latex/zh-cn/wen-dang-jie-gou/07-management-in-a-large-project.md) 以了解更多关于在 LaTeX 中管理大型项目的信息。

## 在同一项目中编译多个文档

为了让你能够快速编译同一项目中的不同文档，Overleaf 会将编辑器中当前打开的文件作为主文档来编译，只要它包含 **`\documentclass[...]{...}`** 命令。这对于使用 [**`subfiles`** 宏包](/latex/zh-cn/wen-dang-jie-gou/08-multi-file-latex-projects.md#the-subfiles-package).

## 主文件选择问题

主文件应始终位于根目录中（不在任何文件夹内），以确保所有编译步骤都在正确的目录中运行，并确保所需的辅助文件可用，例如在 [添加索引](/latex/zh-cn/wen-dang-jie-gou/04-indices.md)时。有关重新组织项目以在 Overleaf 上编译的一些提示，参见 [这里](/latex/zh-cn/wen-da/77-my-main-file-is-in-a-subfolder-but-this-is-not-allowed-on-overleaf.-but-i-don-t-want-to-change-my.md).

如果所选的主文件位于某个文件夹中，而根目录中有一个同名文件，那么将编译根目录中的该同名文件，而不是所选文件。例如，若 `version1/main.tex` 被设为主文件，但 `main.tex` 也在项目中， `main.tex` 将被编译。此问题是由于 Overleaf 上 Latexmk 的运行方式所致。为避免由此带来的问题，请给任何你希望在 Overleaf 上编译的文件起一个唯一的名称。


---

# 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/zhi-shi-ku/127-set-main-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.
