Multi-file LaTeX project

In large projects, keeping parts of your document in several .tex files makes the task of correcting errors and making further changes easier. This article explains how to manage big projects.

In order to achieve a multi-file LaTeX\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\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:

\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:

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.

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

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:

The content of main.tex:

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:

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

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:

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

circle-info

Since this package allow the usage of different preambles, you should be careful with 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 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:

  • \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, 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:

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:

Last updated