# Multi-file LaTeX project

In order to achieve a multi-file $$\LaTeX$$ project, there are two main packages that allow compilation of single files in a multi-file project, each has its pros and cons.

* With `subfiles` package, you can compile every subfile independently and each subfile will automatically **use the preamble in the main file**.
* With `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.

### `import` package

*Even though this is not one of the main topics in the articles, it's important to know!*

Although $$\LaTeX$$ has its own built-in `\import` command, but it's prone to error if nested file importing is needed. For this reason, we need to use the package `import`.

In order to import a file into a document, we use:  `\import{ }{ }`. The first parameter inside braces is the directory where the file is located, the second one is the filename. This is how the main file looks like:

```latex
\chapter{First chapter}
\import{sections/}{section1.tex}
\import{sections/}{section2.tex}
```

This is the folder tree of the example:

```
.
├── main.tex
└── sections
    ├── img
    │   └── plot1.tex
    ├── section1.tex
    └── section2.tex
```

There is also the command `\subimport` that has the same syntax, but if used in one of the files that are imported in the main file, the path will be relative to that sub-file. For instance, below is the contents of the file that was imported into the main documents:

```latex
\begin{figure}[h]
\centering
\subimport{img/}{plot1.tex}
\caption{Caption}
\label{fig:my_label}
\end{figure}
```

In this file, it imports a plot file called "plot1.tex". The location of the file is `img/plot1.tex`. If `\import` were used instead, the path `img/` would be relative to the main file, instead of the folder "sections" where "section1-1.tex" is saved.

{% hint style="info" %}
If all of that was too difficult to understand, remember:

* `\import` - only use in the main file
* `\subimport` - use when have nested imports, usually use in imported file
  {% endhint %}

### `subfiles` packages

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

In my example, my document structure looks like this:

```
.
├── main.tex
└── sections
    ├── diagram.tex
    ├── section1.tex
    └── section2.tex

1 directory, 4 files
```

The content of `main.tex`:

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

\begin{document}

\section{Introduction}

\subfile{sections/introduction}

\section{Second section}

\subfile{sections/section2}

\end{document}
```

In this main file, I have `blindtext` imported to create dummy, place-holder text; `subfiles` for managing multiple files. Then each external sub-file must be imported with the command `\subfile{path to file}`.

The content of subfiles:

```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}
```

This file can now be compiled as a standalone file, the preamble will directly inherit from the main document. That's the reason why we don't import `graphicx` but still be able to compile this document.

The document class is a bit different:

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

the parameter inside brackets, `../main.tex`, is the relative path to the main document. (`../` means up 1 directory).&#x20;

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

`\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. So, avoid leaving blank lines at the top and bottom of the file.

### `standalone` package

As mentioned before, the main difference between `standalone` and `subfiles` is the fact that each subfile can use its own preamble.

There is only a slight difference between 2 main files:

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

\begin{document}

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

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

\end{document}
```

We use `\usepackage[subpreambles=true]{standalone}` to enable, it is advised to put this line early in the beginning of the preamble.

{% hint style="info" %}
Since this package allow the usage of different preambles, you should be careful with possible incompatibilities in the different preambles.
{% endhint %}

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 using the `import` package because it prevents errors in nested files.

Each subfile will have its own preamble and packages imported to work as a 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}
```

* `\documentclass[class=article, crop=false]{standalone}` - declaring that this is a file to be used with the `standalone` package
  * `class=article`. Sets `article` as underlying class, any other class can be used: book, report, etc.
  * `crop=false`. If this option is omitted the output will be cropped to a minimum size.
* `\usepackage[subpreambles=true]{standalone}` - I once again use this command because there is a nested standalone file `diagram.tex` . The content of this file is a `TikZ` graphics with its own configuration.

\--> The ability to import multiple files, taking advantage of different preambles is the killing feature of this package.

### Packaging your preamble

If the preamble of your document has many user-defined commands or term definitions for the [glossary](https://teebow1e.gitbook.io/latex/document-structure#glossaries), you can put it in a separate file. The right way is to create a custom package, which is a file with the .sty extension. Let's see an example:

```latex
% config.sty
\ProvidesPackage{teebowieconfig}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[utf8]{vietnam}
\usepackage{graphicx}
\usepackage{blindtext}

%Header styles
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textbf{\textit{\nouppercase{\leftmark}}}}
\fancyhead[LO]{\textbf{\textit{\nouppercase{\rightmark}}}}
\fancypagestyle{plain}{ %
\fancyhf{} % remove everything
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}}

%makes available the commands \proof, \qedsymbol and \theoremstyle
\usepackage{amsthm}

%Ruler
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}

%Definition counter
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]

%Example environment
\theoremstyle{remark}
\newtheorem{examle}{Example}

%Example counter
\newcommand{\reiniciar}{\setcounter{example}{0}}
```

Above is an example configuration I have, its main task is importing packages, defining headers and footers, defining new commands as well as definitions.

Notice that the first line of the example is `\ProvidesPackage{example}`; this means that we have to import this package as *example* in the main file, with the command `\usepackage`.

If you'd like to delve deeper into the topic, feel free to explore the following links for additional information:

* <https://en.wikibooks.org/wiki/LaTeX/Modular_Documents>
* <https://ctan.org/pkg/subfiles>
* <https://ctan.org/pkg/standalone>
