[edit] ShareLaTeX guides

[edit] LaTeX Basics

[edit] Mathematics

[edit] Figures and tables

[edit] References and Citations

[edit] Languages

[edit] Document structure

[edit] Formatting

[edit] Fonts

[edit] Presentations

[edit] Commands

[edit] Field specific

[edit] Class files

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.


[edit] Introduction

Below is an example of a book whose sections and user-defined commands are stored in separate files.

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

In the subsequent sections each part will be more clearly explained.

  Open an example of the import package in ShareLaTeX

[edit] 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 file with the .sty extension. Let's see an example:

\usepackage[spanish, english]{babel}
%Header styles
\fancypagestyle{plain}{ %
\fancyhf{} % remove everything
%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 the import package in ShareLaTeX

[edit] Importing files

The standard tool to insert a latex file into another are \input and \include (see below), but these are prone to errors if nested file importing is needed. For this reason the recommended option is the package import.

\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 the import package in ShareLaTeX

[edit] Inputting and including files

Short description of the commands input and include.

input command

Use this command in the document body to insert the contents of another file named filename.tex. 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. 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.

  Open an example of the import package in ShareLaTeX

[edit] Further reading

For more information see: