> 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/document-structure/08-multi-file-latex-projects.md).

# Multi-file LaTeX projects

## Introduction

In [large LaTeX documents](/latex/document-structure/07-management-in-a-large-project.md) one usually has several `.tex` files, one for each chapter or section, and then they are joined together to generate a single output. This helps to keep everything organized and makes easier to debug the document, but as the document gets larger the compilation takes longer. This can be frustrating since one is usually only interested in working in a particular file each time.

The natural approach to overcome this is to compile each file separately. There are two main packages that allow compilation of single files in a multi-file project. The choice you make depends on what you need.

* With [the **subfiles** package](#the-subfiles-package) you can compile every subfile independently and each subfile will automatically use the preamble in the main file.
* With [the **standalone** package](#the-standalone-package) every subfile works as an independent file, subfiles can be latter joined in a main document that will pull the preambles from each one of them. Especially useful if you need to reuse the same file in more than one document, a tikz picture is a good example of this.

## Notes on \documentclass in Overleaf .tex files

As demonstrated in the following video clip, even if you have set your project's main `.tex` file you can select another file to compile—provided it contains a `\documentclass` declaration. However, to ensure all elements of your project are compiled correctly—such as glossaries, accessing paths to `.bib` files etc.—we strongly recommend that you only compile files contained in the root directory of your project.

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/U2nGfRx9NwNGX0vWXNebP/1ca4b9a4b2f36d5ca9c0dbaa87f44bfa/maincompile.mp4>" %}

## The subfiles package

This package is suitable for most of the situations, it's really easy to use.

The examples in this section have the next hierarchical file structure:

![CompileSubfilesEx1OverleafV2.png](/files/GA6NwQgf7SnIr06Ziq3k)

### The main file

In the main file two special commands are needed.

```latex
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}

\usepackage{blindtext}

\usepackage{subfiles} % Best loaded last in the preamble

\title{Subfiles package example}
\author{Overleaf}
\date{ }

\begin{document}

\maketitle

\section{Introduction}

\subfile{sections/introduction}

\section{Second section}

\subfile{sections/section2}

\end{document}
```

![CompileSubfilesEx1aOverleafV2.png](/files/x1Hbbg8OHHDS1yzF2mBu)

Per-file compilation requires the line

```latex
\usepackage{subfiles}
```

in the preamble, this enables the `subfiles` package. Then each external sub-file must be imported with the command `\subfile{}`. In the example the files `introduction.tex` and `section2.tex` are imported into the main file from the `sections` folder. Notice that the file extension is not mandatory.

[Open a `subfiles` package example in Overleaf](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### The subfiles

Once you have set up your main file, each subfile must have a special structure.

```latex
\documentclass[../main.tex]{subfiles}
\graphicspath{{\subfix{../images/}}}
\begin{document}
\textbf{Hello world!}
\begin{figure}[bh]
\centering
\includegraphics[width=3cm]{overleaf-logo}

\label{fig:img1}
\caption{Overleaf logo}
\end{figure}

Hello, here is some text without a meaning...

\end{document}
```

![CompileSubfilesEx2OverleafV2.png](/files/oP85yIhYjF64dVxyndo0)

Now this file can be compiled as a standalone file, the document class and the rest of the preamble will be the same defined in the main document.

The first command here is

```latex
\documentclass[../main.tex]{subfiles}
```

the parameter inside brackets, `../main.tex`, is the relative path to the main document. In this case the file `introduction.tex` is inside the folder `sections`, hence the file `main.tex` is one level up the current folder (this is what `../` means).

You will also need to use the `\subfix` command with the relative folder path when specifying `\graphicspath` in `introduction.tex`:

```latex
\graphicspath{{\subfix{../images/}}}
```

Then the actual contents is typed inside `\begin{document}` and `\end{document}`. Everything outside this environment will be ignored, or more specifically, will be considered as part of the preamble. Avoid leaving blank lines at the top and bottom of the file.

[Open a `subfiles` package example in Overleaf](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## The standalone package

The package **standalone** provides the same functionality as **subfiles** and is more flexible, but the syntax is more complex and prone to errors. The main difference is that each subfile has its own preamble.

The examples in this section have the next hierarchical file structure:

![CompileSubfilesEx3OverleafV2.png](/files/KHUZRXhCHBaqTt5is7xX)

### The main file

The main file is very similar to that of any other project with multiple files.

```latex
\documentclass{article}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}

\title{Standalone package example}
\author{Overleaf}
\date{May 2021}

\begin{document}

\maketitle

\section{First section}
\import{sections/}{introduction}

\section{Second section}
\import{sections/}{section2}

\end{document}
```

![CompileSubfilesEx3Overleaf.png](/files/cvHrZm5z9f7jemXdIP0e)

The line

```latex
\usepackage[subpreambles=true]{standalone}
```

enables the **standalone** package, it should be placed early in the document. The parameter inside brackets tells LaTeX to import the preamble from each subfile (packages are imported only once), if omitted, make sure you have all the necessary commands in the main document preamble for the subfiles to work. One must be careful because of possible incompatibilities in the different preambles.

In the body of the main document, each subfile is imported with `\import{}{}`. The standard `\input{}` command can also be used, but the one in this example is recommended to [manage large projects](/latex/document-structure/07-management-in-a-large-project.md) because it prevents errors in nested files.

[Open an example of the `standalone` package in Overleaf](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

### The subfiles

Each subfile must have its own preamble and import all packages needed to work as standalone document.

```latex
\documentclass[class=article, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}
\usepackage{blindtext}

\begin{document}
A TikZ figure will be rendered below this line

\begin{figure}[ht]
\centering
\subimport{../}{diagram.tex}
\label{fig:tikzexample}
\caption{A nice simple diagram}
\end{figure}

\blindtext
\end{document}
```

![CompileSubfilesEx4OverleafV2.png](/files/OxhTvX8TPEadQANXIjJH)

The first line in the subfile is

```latex
\documentclass[class=article, crop=false]{standalone}
```

This declares that this is a file to be used with the **standalone** package, there are two optional parameters inside the brackets.

* `class=article`. Sets `article` as underlying class, any other class can be used: book, report, etc. (except beamer).
* `crop=false`. If this option is omitted the output will be cropped to a minimum size.

The next command is not mandatory

```latex
\usepackage[subpreambles=true]{standalone}
```

but hast to be used in this example because there's a nested **standalone** file here. A [tikz picture](/latex/figures-and-tables/05-tikz-package.md) is inserted with [`\subimport{../}{diagram.tex}`](/latex/document-structure/07-management-in-a-large-project.md#importing-files), you can see the contents of the file "diagram.tex" below:

```latex
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[
roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm},
squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=5mm},
]
%Nodes
\node[squarednode]      (maintopic)                              {2};
\node[roundnode]        (uppercircle)       [above=of maintopic] {1};
\node[squarednode]      (rightsquare)       [right=of maintopic] {3};
\node[roundnode]        (lowercircle)       [below=of maintopic] {4};

%Lines
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);

\end{tikzpicture}
\end{document}
```

[Open this Standalone TikZ example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Standalone+TikZ+example\&snip=%5Cdocumentclass%7Bstandalone%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cusetikzlibrary%7Bpositioning%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Btikzpicture%7D%5B%0Aroundnode%2F.style%3D%7Bcircle%2C+draw%3Dgreen%2160%2C+fill%3Dgreen%215%2C+very+thick%2C+minimum+size%3D7mm%7D%2C%0Asquarednode%2F.style%3D%7Brectangle%2C+draw%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%2C+minimum+size%3D5mm%7D%2C%0A%5D%0A%25Nodes%0A%5Cnode%5Bsquarednode%5D++++++%28maintopic%29++++++++++++++++++++++++++++++%7B2%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28uppercircle%29+++++++%5Babove%3Dof+maintopic%5D+%7B1%7D%3B%0A%5Cnode%5Bsquarednode%5D++++++%28rightsquare%29+++++++%5Bright%3Dof+maintopic%5D+%7B3%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28lowercircle%29+++++++%5Bbelow%3Dof+maintopic%5D+%7B4%7D%3B%0A%0A%25Lines%0A%5Cdraw%5B-%3E%5D+%28uppercircle.south%29+--+%28maintopic.north%29%3B%0A%5Cdraw%5B-%3E%5D+%28maintopic.east%29+--+%28rightsquare.west%29%3B%0A%5Cdraw%5B-%3E%5D+%28rightsquare.south%29+..+controls+%2B%28down%3A7mm%29+and+%2B%28right%3A7mm%29+..+%28lowercircle.east%29%3B%0A%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

![CompileSubfilesEx5.png](/files/hwftYtrDyu8zUxCgxYUs)

This is the main feature in **standalone**, you can import this file in any other document and recycle the code. For instance, this diagram can later be used in a presentation without further changes.

[Open an example of the `standalone` package in Overleaf](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

## Further reading

For more information see

* [Management in a large project](/latex/document-structure/07-management-in-a-large-project.md)
* [Cross referencing sections and equations](/latex/document-structure/03-cross-referencing-sections-equations-and-floats.md)
* [Indices](/latex/document-structure/04-indices.md)
* [Glossaries](/latex/document-structure/05-glossaries.md)
* [Hyperlinks](/latex/document-structure/09-hyperlinks.md)
* [Page numbering](/latex/formatting/03-page-numbering.md)
* [Single sided and double sided documents](/latex/formatting/08-single-sided-and-double-sided-documents.md)
* [Multiple columns](/latex/formatting/09-multiple-columns.md)
* [Paragraph formatting](/latex/formatting/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [Page size and margins](/latex/formatting/07-page-size-and-margins.md)
* [Counters](/latex/formatting/10-counters.md)
* [Margin notes](/latex/formatting/15-margin-notes.md)
* [Bold, italics and underlining](/latex/latex-basics/03-bold-italics-and-underlining.md)
* [Font sizes, families, and styles](/latex/fonts/01-font-sizes-families-and-styles.md)
* [Font typefaces](/latex/fonts/02-font-typefaces.md)
* [Supporting modern fonts with XeLaTeX](/latex/fonts/03-xelatex.md)
* [International language support](/latex/languages/03-international-language-support.md)
* [Font sizes, families, and styles](/latex/fonts/01-font-sizes-families-and-styles.md)
* [Compiling big projects](/latex/knowledge-base/038-fixing-and-preventing-compile-timeouts.md#compiling-big-projects)
* [Inserting Images](/latex/more-topics/27-inserting-images.md)
* [TikZ package](/latex/figures-and-tables/05-tikz-package.md)
* [Writing your own package](/latex/class-files/03-writing-your-own-package.md)
* [Writing your own class](/latex/class-files/04-writing-your-own-class.md)
* [The **subfiles** package documentation](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/subfiles/subfiles.pdf)
* [The **standalone** package documentation](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/standalone/standalone.pdf)


---

# 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/document-structure/08-multi-file-latex-projects.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.
