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

# 設定主文件

Overleaf 中的主文件是 LaTeX 會被指示首先編譯的檔案。它是專案的主檔案，所有其他部分（類別、套件、章節等）都會被輸入其中。當未手動設定時，Overleaf 會在專案中選擇一個包含以下內容的文件 **`\documentclass[...]{...}`** 指令。因此，如果您有一個獨立的前言檔案，最好不要在 **`\documentclass[...]{...}`** 前言檔案中包含此指令，而是將其放在主文件的開頭。如果您不這麼做，將只會編譯前言部分。要在專案中設定主文件，只需點擊左上角選單：

![Lefthandmenu.PNG](/files/5a5a818ebbfe23e845db47fcaa03d2e4f5a3e7df)

並前往 **主文件**:

![Maindocument.PNG](/files/353c7dc83110650f20853205a530151f01e139fd)

請參閱 [這裡](/latex/zh-tw/wen-jian-jie-gou/07-management-in-a-large-project.md) 以了解更多有關在 LaTeX 中管理大型專案的資訊。

## 在同一個專案中編譯多份文件

為了讓您能快速編譯同一個專案中的不同文件，Overleaf 會在編輯器中開啟的檔案一旦包含 **`\documentclass[...]{...}`** 指令後，將其作為主文件進行編譯。這對搭配 [**`subfiles`** 套件](/latex/zh-tw/wen-jian-jie-gou/08-multi-file-latex-projects.md#the-subfiles-package).

## 主檔案選擇的問題

主檔案應始終位於根目錄中（任何資料夾之外），以確保所有編譯步驟都會在正確的目錄中執行，並確保所需的輔助檔案可用，例如在 [新增索引](/latex/zh-tw/wen-jian-jie-gou/04-indices.md)時。請參閱一些關於重新組織專案以在 Overleaf 上編譯的建議 [這裡](/latex/zh-tw/wen-yu-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` 將會被編譯。這個問題是由 Latexmk 在 Overleaf 上執行的方式所造成。為避免因此產生問題，請為您想在 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-tw/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.
