Skip to content


In large projects, such as books, keeping parts of your document in several .tex files makes the task of correcting errors and making further changes easier. It's simpler to locate a specific word or element in a short file. For this purpose this article explains how to manage big projects.

Inputting and including files

The standard tools to insert a LaTeX file into another are \input and \include.

input command

Use this command in the document body to insert the contents of another file named filename.tex; this file should not contain any LaTeX preamble code (i.e. no \documentclass, \begin{document} or \end{document} ). LaTeX won't start a new page before processing the material in filename.tex. \input allows you to nest \input commands, in files that are already being inputted by the root file.

include command

Use this command in the document body to insert the contents of another file named filename.tex; again this file should not contain any LaTeX preamble. LaTeX will start a new page before processing the material input from filename.tex. Make sure not to include the extension .tex in the filename, as this will stop the file from being input (the extension can optionally be included with input and import). It is not possible to nest include commands. Each file that gets \included has its own .aux file storing information of created labels and contents for the table of contents, list of figures, etc. You can use \includeonly with a comma separated list of file names (make sure that there are no leading or trailing spaces). If you do this LaTeX will only process the files contained in that list. This can be used to enhance compilation speed if you're only working on a small part of a bigger document. Page numbers and cross references will however still work, as the .aux files of left out files will still be processed.

 Open an example of a large project on Overleaf

Preamble in a separate file

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:


\usepackage[spanish, english]{babel}


%Header styles
\fancypagestyle{plain}{ %
\fancyhf{} % remove everything
\renewcommand{\headrulewidth}{0pt} % remove lines as well

%makes available the commands \proof, \qedsymbol and \theoremstyle


%Lemma definition and lemma counter

%Definition counter

%Corolary counter

%Commands for naturals, integers, topology, hull, Ball, Disc, Dimension, boundary and a few more

%Example environment

%Example counter

All the commands in this file could have been put in the preamble, but the main file would have become confusing because of this large amount of code, and to locate the actual body of the document in such large file would be a cumbersome task.

This file could also be put in a normal .tex file and imported with the command import (see the next section), but a .sty file prevents possible errors if the file is accidentally imported more than once.

Notice that the first line of the example is


this means that we have to import this package as example in the main file, i.e. with the command


as shown in the introduction.

Note: A .sty file is far more flexible, it can be used to define your own macros and optional parameters can be passed, see Writing your own package.

 Open an example of a large project on Overleaf

Using the import package

As mentioned above The standard tools to insert a LaTeX file into another are \input and \include, but these are prone to errors if nested file importing is needed. For this reason you may want to consider the package import.

Below is an example of a book whose sections and user-defined commands are stored in separate files, and where the image files for each chapter are stored in separate folders along with the .tex file for each chapter.







\chapter{First chapter}

\chapter{Additional chapter}

\chapter{Last chapter}




As you see, the example is a book with three chapters and several sections in a neat main file that pulls external files to generate the final document. The command \frontmatter in the book document class is used for the first pages of the document, the page numbering style is set to Roman numerals by this command; the command \mainmatter resets the page numbering and changes the style to Arabic, \backmatter disables the chapter numbering (suitable for the bibliography and appendices).

\chapter{First chapter}

First, add this line to the preamble of your document:


Then use \import{ }{ }. The first parameter inside braces is the directory where the file is located, it can be relative to the current working directory or absolute. The second parameter is the name of the file to be imported

There is also available 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 "section1-1.tex" that was imported in the previous example:

\section{First section}

Below is a simple 3d plot



As you see, this file imports a pgf plot file called "plot1.tex" that creates a 3d plot. This file is imported by


from the "img" folder, contained in the folder "sections".

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.

 Open an example of a large project on Overleaf

Further reading

For more information see:

Overleaf guides

LaTeX Basics


Figures and tables

References and Citations


Document structure





Field specific

Class files

Advanced TeX/LaTeX