> 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/knowledge-base/127-set-main-document.md).

# Set Main Document

The main document in Overleaf is the file which LaTeX will be told to compile first. It is the main file of the project, which all other parts (classes, packages, chapters etc.) will be inputted into. When this is not set manually, Overleaf will choose a document in the project which includes the **`\documentclass[...]{...}`** command. For this reason, if you have a separate file for your preamble, it is better not to include the **`\documentclass[...]{...}`** command in the preamble file, but rather at the start of the main document. If you do not do this, only the preamble will be compiled. To set the main document on a project, simply click into the menu in the top lefthand corner:

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

and go to **Main document**:

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

See [here](/latex/document-structure/07-management-in-a-large-project.md) to learn more about managing large projects in LaTeX.

## Compiling several documents in the same project

To allow you to quickly compile different documents in the same project, Overleaf will compile whichever file is open in the editor as the main document once it includes the **`\documentclass[...]{...}`** command. This is particularly convenient for editing with the [**`subfiles`** package](/latex/document-structure/08-multi-file-latex-projects.md#the-subfiles-package).

## Issues with main file selection

The main file should always be in the root directory (outside of any folders), to ensure that all of the compilation steps will be run in the correct directory and to ensure that the required auxiliary files are available, for instance, when [adding an index](/latex/document-structure/04-indices.md). See some tips on reorganising your project to compile on Overleaf [here](/latex/questions-and-answers/77-my-main-file-is-in-a-subfolder-but-this-is-not-allowed-on-overleaf.-but-i-don-t-want-to-change-my.md).

If the selected main file is contained in a folder and there is a file of the same name in the root directory, the file with that name in the root directory will be compiled rather than the selection. E.g. , if `version1/main.tex` is set as the main file but `main.tex` is also in the project, `main.tex` will be compiled. This problem is due to the way Latexmk is run on Overleaf. To avoid issues from this, give any files you will want to compile on Overleaf a unique name.


---

# 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/knowledge-base/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.
