[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 LaTeX documents 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 you can compile every subfile independently end each subfile will automatically use the preamble in the main file.
  • With 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.


[edit] The subfiles package

This package is suitable for most of the situations, it's really easy to use. To learn how to import this package into a ShareLaTeX project see the compilation in ShareLaTeX.

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


[edit] The main file

In the main file two special commands are needed.

\title{Subfile Example}
\author{Team Learn ShareLaTeX}
\date{ }
\section{Second section}


Per-file compilation requires the line


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 images folder. Notice that the file extension is not mandatory.

An important note here regarding the images. For the graphicx package to work properly on each subfile, you must add each relative path to folder containing the images. For instance, the command


Declares two paths to the same folder images, the first path relative to the main file, the second path relative to the sections folder (The ../ means "one level up"). If only the first one is provided you will see the error "image not found" when compiling the subfiles, but the main file will work fine.

  Open an example of the subfiles package in ShareLaTeX

[edit] The subfiles

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

\textbf{Hello world!}
\caption{ShareLaTeX learn logo}
Hello, here is some text without a meaning.  This... 


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


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

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 an example of the subfiles package in ShareLaTeX

[edit] 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: CompileSubfilesEx7.png

[edit] The main file

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

\author{Team Learn ShareLaTeX}
\date{August 2014}
    \section{First section}
    \section{Second section}


The line


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 because it prevents errors in nested files.

  Open an example of the standalone package in ShareLaTeX

[edit] The subfiles

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

\documentclass[float=false, crop=false]{standalone}
A TikZ figure will be rendered below this line
\caption{A nice simple diagram}


The first line in the subfile is

\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


but hast to be used in this example because there's a nested standalone file here. A tikz picture is inserted with \subimport{../}{diagram.tex}, you can see the contents of the file "diagram.tex" below:

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},
\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};
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);


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 ShareLaTeX

[edit] Compilation in ShareLaTeX

By now, to use the subfiles package in ShareLaTeX you must add it to the root directory of your project.

Create a new file by clicking the New file icon Newfileicon.png, name this new file subfiles.sty and copy-paste the next code into it:

%% This is file `subfiles.sty',
%% generated with the docstrip utility.
%% The original source files were:
%% subfiles.dtx  (with options: `package')
%% Copyright 2002 Federico Garcia
\ProvidesPackage{subfiles}[2002/06/08 Federico Garcia]
\DeclareOption*{\PackageWarning{\CurrentOption ignored}}
%% End of file `subfiles.sty'.

You can also download the class file subfiles.sty and upload it to your project.

After that, follow the process described in the first section to typeset your document. From now on if you click Recompile you will see the output corresponding to the file you are currently editing.

The standalone package is already included in ShareLaTeX. No further steps are needed to compile the document.

  Open an example of the subfiles package in ShareLaTeX

[edit] Further reading

For more information see