# The Main document

The Main document in Overleaf is the .tex 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 incorporated into.&#x20;

## Setting the Main document

{% stepper %}
{% step %}

### **Click into the Settings in the bottom lefthand corner**

<div align="right"><figure><picture><source srcset="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F3o0WdNKpAzS4XHYbOALk%2FSettings-Dark-NewEditor.png?alt=media&#x26;token=dd13e042-4594-4ae0-a4fb-dbc8a12eaf0b" media="(prefers-color-scheme: dark)"><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FiCL8H3rZYqo737dNuZkb%2FSettings-Light-NewEditor.png?alt=media&#x26;token=fddfd9c1-0db2-4dc0-b217-b19eb4a3e9cc" alt="Image of the Settings icon in the bottom-left corner of the editor" width="188"></picture><figcaption><p>Option 1: Gear icon</p></figcaption></figure></div>

<div align="left"><figure><picture><source srcset="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F8DhtZJqY5ebYXAIKNN6f%2Fimage.png?alt=media&#x26;token=b17c7762-98ab-4d17-b0e1-88a9aec2ca9b" media="(prefers-color-scheme: dark)"><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FxhX2Svoz99YC8ZdWDjMB%2Fimage.png?alt=media&#x26;token=3dbe3dcc-5b6f-4687-ab97-4af2c527d8c8" alt="Image of the Settings option in the File menu" width="188"></picture><figcaption><p>Option 2: File menu</p></figcaption></figure></div>
{% endstep %}

{% step %}

### **Go to Compiler > Main document:**

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FeHI3sSW2u9eBVUFV4lTw%2FMainDocument-NewEditor.png?alt=media&#x26;token=5291a211-3d31-4b34-8b44-6ca71881b913" alt="The Compiler tab of the Settings, with Main document option displayed" width="563"><figcaption><p>Main document location</p></figcaption></figure>
{% endstep %}

{% step %}

### Alternatively, you may also set the main document by selecting the file within the File tree

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FpyfbnzntjcI1oX24ptjn%2Fimage.png?alt=media&#x26;token=7837b971-05e4-4f64-9d41-87cf248b2d8e" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### Best Practices

The Main document **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.&#x20;

If the Main document is placed in a folder, several features and packages can fail to work as expected. Affected features include [jump-to-location](https://docs.overleaf.com/navigating-in-the-editor/working-with-the-pdf-viewer/moving-between-the-editor-and-pdf), [word count](https://docs.overleaf.com/writing-and-editing/using-word-count) and scripts like [makeglossaries](https://www.overleaf.com/learn/latex/Glossaries) etc.

The Main document must have a `\documentclass` declaration. If you provide a preamble file, input this file after the `\documentclass` declaration in your Main document.

### Compiling other files in the same project

While it is possible to place multiple independently compilable files within the same Overleaf project, it is recommended that, in most cases, an Overleaf project should correspond to a single output document. In general, related documents should not be placed in the same project. There should not be multiple "Main documents" within a project.

* Conceptually related projects can be grouped together using tags.
* Common files can be shared between projects using the "input from another project" feature.&#x20;
* Multiple versions of a project can be tracked using the History feature (rather than keeping old versions of files in folders within the project).

However, there are some cases where it is helpful to be able to compile files other than the Main document within a 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](https://learn.overleaf.com/learn/Multi-file_LaTeX_projects#The_subfiles_package).

Any project file that contains a `\documentclass` command can be compiled when opened in the editor by selecting the **Recompile** button. This allows you to compile multiple documents inside one Overleaf project without changing the project's main file setting.

## Troubleshooting&#x20;

<details>

<summary>I have files with the same names in different folders, and the wrong file is being compiled.</summary>

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. For example , 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.

</details>

<details>

<summary>The document that I want to choose is not in the Main document list.</summary>

The Main document must have a `\documentclass` declaration. If you provide a preamble file, input this file after the `\documentclass` declaration in your Main document.

The Main document must be an editable plain text file. Therefore if a .tex file is larger than 2 MB, it's [*categorized as non-editable*](https://docs.overleaf.com/managing-projects-and-files/adding-files-to-a-project/uploading-files-to-a-project#i-cant-edit-some-of-the-files-that-i-have-uploaded) on Overleaf, and you won't be able to set such a file as your project's Main document. You can break it up into smaller .tex files, and use `\input` or `\include` on them.

</details>

## Keeping the Main document in a folder

In some cases it's possible to maintain a file and folder structure that has the Main document in a folder while avoiding the problems that usually occur with this structure on Overleaf.\
\
If the actual Main document is in a folder, a "proxy" Main document can be provided at the top level that points to the actual Main document.&#x20;

Provide an extra `main.tex` file in the Project's top level, and use the `import` package to pull in the real main file from the subfolder:

* Project Top Level
  * `main.tex`
  * paper/
    * `manuscript.tex`
    * `refs.bib`
    * `appendix.tex`
  * output/
    * `chart.png`
    * `results-table.tex`

Contents of `main.tex`:

```latex
\RequirePackage{import}
\import{paper/}{manuscript.tex}
```

The original `\inludegraphics`, `\input` etc. in `manuscript.tex` would continue to work with respect to your project file structure.

You can find [an example in action](https://www.overleaf.com/read/hpqnttjpvmkn) in the Gallery here.
