Typevia
Document Structure

Introduction to Large Documents

When you first start with LaTeX, it’s common to keep everything in a single .tex file. However, as you begin writing longer documents—such as a thesis, a book, or a comprehensive technical report—a single file becomes difficult to manage. Navigating through thousands of lines of code is tedious, and finding specific sections for editing becomes a chore.

Managing large documents in LaTeX involves modularization: breaking your project into several smaller, manageable files and linking them together. This approach improves organization, allows for faster troubleshooting, and even speeds up the compilation process.


The Main File Structure

The "Main File" (often named main.tex) acts as the conductor of your project. It contains the preamble—where you load packages and define settings—and the framework that pulls in your content from external files.

A typical main file for a large project looks like this:

\documentclass[12pt, a4paper]{report}

% --- Preamble ---
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{biblatex} % For references
\addbibresource{references.bib}

\title{Developing Large-Scale LaTeX Projects}
\author{Jane Doe}
\date{\today}

% --- Document Body ---
\begin{document}

\maketitle
\tableofcontents

% Content is pulled from external files here
\include{chapters/introduction}
\include{chapters/literature-review}
\include{chapters/methodology}

\printbibliography

\end{document}

Note: Notice that we do not put the actual chapter text in this file. Instead, we use the \include command to reference other files. This keeps the structure clean and high-level.


Understanding \input vs. \include

There are two primary commands used to merge files in LaTeX: \input and \include. While they seem similar, they serve different purposes.

1. The \input Command

The \input{filename} command is the simplest way to insert content. When LaTeX encounters \input, it literally copies the contents of the specified file into the current file at that exact position.

  • Best for: Small snippets of code, such as a complicated table, a shared preamble, or a specific mathematical derivation.
  • Behavior: It does not start a new page. You can use \input anywhere, even inside other files.
% Example of using \input for a table
\section{Experimental Data}
Below is the data collected during the first trial:

\input{tables/trial-results.tex}

2. The \include Command

The \include{filename} command is designed for major sections, like chapters.

  • Best for: Main chapters or large sections of a book/thesis.
  • Behavior: It always starts a new page before inserting the content. Most importantly, it creates a separate .aux file for the included file, which allows for a powerful feature called "Partial Compilation."

Warning: You cannot nest \include commands. You cannot use \include inside a file that has already been included. Use \input for sub-sections within a chapter instead.


Speeding Up Work with \includeonly

One of the biggest advantages of using \include is the ability to compile only the section you are currently working on while still maintaining correct page numbers and cross-references.

If your document has 10 chapters and takes 30 seconds to compile, waiting for it every time you change a comma is frustrating. You can use the \includeonly command in your preamble to focus on one file:

\documentclass{report}

% Only compile the introduction and methodology
% All other \include commands in the document will be ignored
\includeonly{chapters/introduction, chapters/methodology}

\begin{document}
    \include{chapters/introduction}
    \include{chapters/literature-review} % This will be skipped
    \include{chapters/methodology}
\end{document}

When you use \includeonly, LaTeX uses the .aux files from previous full compilations to "remember" what the page numbers and section numbers were for the skipped chapters. This allows your Table of Contents and references to remain accurate without having to process the entire document.


Organizing Your Project Folder

As your project grows, your folder will quickly become cluttered with .tex, .pdf, .log, .aux, and image files. A well-organized directory structure is essential for long-term sanity.

A professional LaTeX project structure usually looks like this:

  • main.tex (The master file)
  • references.bib (Your bibliography file)
  • chapters/ (A folder containing intro.tex, results.tex, etc.)
  • figures/ (A folder containing all images and diagrams)
  • styles/ (Optional: for custom .sty files)

To include an image from the figures folder, you would use:

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.8\textwidth]{figures/experimental-setup.png}
    \caption{The layout of the experiment.}
\end{figure}

Tip: You can use the command \graphicspath{{figures/}} in your preamble. This tells LaTeX to look in the figures folder automatically, allowing you to just write \includegraphics{experimental-setup.png}.


Managing Structural Parts: Front and Back Matter

For very large documents using the book class, LaTeX provides specific commands to handle different "zones" of the document. These commands automatically adjust page numbering (e.g., Roman numerals for the intro, Arabic for the body).

  • \frontmatter: Used for the title page, table of contents, and preface. Pages are numbered with Roman numerals (i, ii, iii).
  • \mainmatter: Used for the actual chapters. Page numbering resets to 1 and switches to Arabic numerals (1, 2, 3).
  • \backmatter: Used for appendices, bibliographies, and indices.
\documentclass{book}

\begin{document}

\frontmatter
\maketitle
\tableofcontents
\include{chapters/preface}

\mainmatter
\include{chapters/chapter1}
\include{chapters/chapter2}

\backmatter
\include{chapters/appendix}
\printbibliography

\end{document}

Best Practices and Common Pitfalls

To ensure your large project remains stable and easy to edit, follow these industry best practices:

  1. Unique Labels: Use a prefixing system for labels to avoid duplicates across chapters. For example, use \label{fig:chap1:results} instead of just \label{results}.
  2. Keep the Preamble Clean: If your preamble becomes very long (50+ lines), move it to a separate file (e.g., preamble.tex) and use \input{preamble} at the very top of your main file.
  3. Use Comments: Even though the files are separate, use comments to mark the end of chapters or to leave "TODO" notes for yourself.
  4. Check File Names: Avoid spaces in your filenames (e.g., use chapter_one.tex instead of chapter one.tex). Some LaTeX compilers can be finicky with spaces.
  5. Path Consistency: Always use relative paths (like chapters/intro) rather than absolute paths (like C:/Users/Name/Documents/...). This ensures the project still compiles if you move the folder or share it with a collaborator.

By breaking your document into logical pieces, you transform a daunting writing task into a collection of small, achievable goals. This modular workflow is the standard used by researchers and authors worldwide to produce high-quality, professional documents.