The Main document

Every Overleaf project has a Main source document (.tex file) that is the file which is submitted to the compiler by default.

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.

Setting the Main document

To set the main document on a project, click into the menu in the top lefthand corner:

Lefthandmenu.PNG

and go to Main document:

Maindocument.PNG

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.

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, word count and scripts like makeglossaries 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.

  • 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.

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

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

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.

The document that I want to choose is not in the Main document list.

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 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.

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.

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:

\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 in the Gallery here.

Last updated

Was this helpful?