Skip to content
ImagineWorld

Developing Large Documents

node_idnode-book_latex-beginner-s-guide_chapter-11-developing-large-documents
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num11
chapter_titleDeveloping Large Documents
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

The first chapter of this book mentioned that LaTeX can handle large documents with ease. Once you start creating larger documents, you’ll see that LaTeX continues to work reliably no matter how big the document becomes. For the computer, the source code structure doesn’t matter, but for you, as the author, it’s important to keep things organized. Long documents can span hundreds of pages with thousands of lines, so a manageable structure is essential.

By the end of this chapter, you’ll be able to set up a large document project with multiple files, a title page, and separately numbered front matter and back matter.

In this chapter, we’ll look at the following topics:

  • Splitting the input
  • Creating front and back matter
  • Designing a title page
  • Working with templates
  • Organizing files and folders

That’s a big step toward writing a thesis, a book, or a comprehensive report.

Let’s begin by building a document from several files.

To keep a large document manageable, we can split it into several smaller files. This lets us work on chapters separately while maintaining a clean overall structure.

We’ll separate the preamble from the body text and place each chapter in its own file. As an example project, we’ll build a multi-chapter document on equations and systems of equations, similar in style to a thesis or a book. We can reuse material from the example in Chapter 9, Writing Math Formulas, where we worked with theorems about equations.

We’ll create several files, step by step:

  1. Start a new file, and collect all the package loads and their options, as we did in our preambles in the previous chapters. This can be a lot of packages that we have already learned about, like this:
    \usepackage[english]{babel}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{microtype}
    \usepackage{natbib}
    \usepackage{tocbibind}
    \usepackage{amsmath}
    \usepackage{amsthm}
    \newtheorem{thm}{Theorem}[chapter]
    \newtheorem{lem}[thm]{Lemma}
    \theoremstyle{definition}
    \newtheorem{dfn}[thm]{Definition}
  2. Save this file as preamble.tex.
  3. Start another new document and copy the contents of the Equations chapter from the theorem example of the Writing theorems and definitions section of Chapter 9, Writing Math Formulas:
    \chapter{Equations}
    \section{Quadratic equations}
    \begin{dfn}
    A quadratic equation is an equation of the form
    \begin{equation}
    \label{quad}
    ax^2 + bx + c = 0
    \end{equation}
    where \( a, b \) and \( c \) are constants
    and \( a \neq 0 \).
    \end{dfn}
  4. Save this document as chapter1.tex.
  5. Create another document for the next chapter, and write the chapter heading and some more text, including a few sections. Save it as chapter2.tex:
    \chapter{Equation Systems}
    \section{Linear Systems}
    ...
    \section{Non-linear Systems}
    ...
  6. Now, create the top-level document, equations.tex. This one starts with the \documentclass command and lists the preamble and the chapters for inclusion:
    \documentclass{book}
    \input{preamble}
    \begin{document}
    \tableofcontents
    \include{chapter1}
    \include{chapter2}
    \end{document}
  7. Compile the document twice. Remember that this action is necessary to finally get the table of contents. Check the contents to see whether everything is in its correct place:

Figure 11.1 – Table of contents

Figure 11.1 – Table of contents

We constructed a top-level document named equations.tex. You can choose a meaningful name, since the filename determines the resulting PDF’s name.

This .tex file serves as the framework for our project. It’s a regular LaTeX document, but we reduced it as much as possible and used two commands to import external .tex files:

  • The \input command inserts another file, just as if we had typed it directly
  • The \include command also reads an external file, but automatically inserts \clearpage before it

Let’s compare these commands in detail.

The simplest way to read another file in your document is with this command:

\input{filename}

When LaTeX encounters this command, it reads the contents of the file named filename, exactly as if it had been typed directly there. The LaTeX compiler processes all commands in this file.

If no filename extension is given, LaTeX assumes the.tex extension and inserts filename.tex. You can also specify a path, either relative or absolute. Since a backslash starts a command, use forward slashes (/) instead of backslashes (\) in path names. For example, use \input{chapters/ch01.tex} instead of \input{chapters\ch01.tex}.

Using relative paths makes it easier to move or copy a project. Avoid special characters and spaces in filenames and paths; stick to letters, numbers, -, and _.

Use \input to put your preamble in a separate file. It keeps your main document tidy, and you can easily copy and adapt a separate preamble for your other projects.

However, splitting a document with \input is not always enough for managing large projects. You might comment out individual \input lines to compile only parts of a large document, especially when complete compilation takes time. But doing so would disrupt the page numbering, section numbering, and cross-referencing. The \include command is designed to handle these issues, so let’s take a closer look at it next.

For bringing in larger sections of a document, such as whole chapters, the following command is more suitable:

\include{filename}

The argument is handled similarly to \input, but there are key differences:

  • \include implicitly starts a new page. Simplified, \include{filename} behaves like this:
    \clearpage
    \input{filename}
    \clearpage

This makes \include useful for page ranges such as chapters or sections. One consequence is that you may use \include only after \begin{document}.

  • \include cannot be nested. You could still use \input in included documents, although adding too many layers can make the structure harder to manage.
  • Most importantly, \include supports a mechanism for choosing which parts of the document you wish to compile, which is specified using another command, namely, \includeonly.

Let’s see how \includeonly works.

Such a partial document, intended for \input or \include, cannot be compiled on its own; it needs a root document that specifies the document class.

However, once you swap out parts of the document using \include while compiling your root document, you may choose which sections or chapters are included with this command:

\includeonly{file list}

The \includeonly command must appear in the preamble, that is, before \begin{document}.

The argument can be a single filename or a comma-separated list of filenames. If a file named name.tex is not specified in this argument, \include{name} will not insert the file; it will act just like a \clearpage command. This allows the exclusion of chunks or whole chapters from compiling. If you work on a large document, this speeds up compilation when you include only your current chapter, while keeping the labels and references intact.

As with the main.tex file, LaTeX writes an .aux file for each included .tex file and reads all of them during compilation, since they contain information such as chapter and page numbers. As long as each included file has been compiled at least once, cross-references and the numbering of pages, chapters, sections, and so on will remain correct, even when you temporarily exclude some chapters.

Try it out by adding the following:

\includeonly{chapter2}

Add it to your preamble in equations.tex and compile. The result will be just the second chapter, keeping the correct numbering. Take a look at the output here with Acrobat Reader:

Figure 11.2 – Our document with Chapter 2 only

Figure 11.2 – Our document with Chapter 2 only

At the top of Figure 11.2, you can see 3/3 pages instead of 5 pages in the first example of the current chapter. On the side, you see three thumbnail pages, and the third is our Chapter 2. This shows us that Chapter 1, Equations, is not included here, but only Chapter 2, as we desired. Also, the page numbers remain the same as in the full document, and the original chapter and section numbers are unchanged.

Compiling time is significantly shorter if you work on a huge document with many chapters and use \includeonly to include just a single chapter that you are working on.

Finally, comment out the \includeonly command to typeset your complete document when you finish your work.

Of course, you can use \include without \includeonly just for splitting a large document into files.

Let’s now return to the structure of a bigger document.

Books often begin with introductory material such as copyright information, a foreword, acknowledgments, or a dedication. Similar for a thesis. This part, including the title page and the table of contents, is called the front matter. A thesis can follow the same structure.

At the end, a book or thesis can include an afterword and supporting material such as a bibliography and an index. This part is called the back matter.

The book class and other similar classes, such as scrbook and memoir, support this sectioning directly. They also support different numbering styles for pages and chapters. Let’s see how it works.

In our example, the book begins with a dedication. The front matter will include the table of contents, the list of tables and figures, and a dedication. All the pages of the front matter will be numbered with Roman numerals. At the end, we add an appendix with supplementary proofs that we like to keep separate from the main chapters:

  1. Create a file called dedication.tex:
    \chapter{Dedication}
    This book is dedicated to one of the greatest
    mathematicians of all time: Carl Friedrich Gauss.
    Without him, this book wouldn't have been possible.
  2. Create a file called proofs.tex:
    \chapter{Proofs}
    ...
  3. Extend the main file, equations.tex, by means of the highlighted lines:
    \documentclass{book}
    \input{preamble}
    \begin{document}
    \frontmatter
    \include{dedication}
    \tableofcontents
    \listoftables
    \listoffigures
    \mainmatter
    \include{chapter1}
    \include{chapter2}
    \backmatter
    \include{proofs}
    \nocite{*}
    \bibliographystyle{plainnat}
    \bibliography{example}
    \end{document}
  4. As you can see in the last highlighted line, we reused the example.bib file from Chapter 8, Managing Contents, Indexes, and Bibliography. Compile, run BibTeX, and compile again. Check out the numbering within the table of contents:

Figure 11.3 – Table of contents of a complex document

Figure 11.3 – Table of contents of a complex document

We saw that LaTeX printed the page number of the contents page in Roman numerals. This applies to all front-matter pages. Furthermore, all the chapters in the front and back matter are unnumbered, even though we did not use the starred command, \chapter*.

This behavior is controlled by three commands, namely, \frontmatter, \mainmatter, and \backmatter. Each starts a new page and adjusts both the page and chapter numbering as follows:

  • \frontmatter: Pages are numbered with lowercase Roman numerals. Chapters generate a table of contents entry, but don’t get a number.
  • \mainmatter: Page numbers switch to Arabic numerals. Chapters are numbered and produce table-of-contents entries.
  • \backmatter: Pages continue with Arabic numerals. Chapters appear again in the table of contents entry but are not numbered.

Like the book class, the scrbook and memoir classes provide the same commands with very similar behavior.

A large document usually begins with a title page, so let’s look at how to create one in LaTeX.

You can create a good-looking title page quickly using the \maketitle command, as we did in Chapter 2, Formatting Text and Creating Macros. Most document classes offer this command to generate a reasonable, pre-formatted title page. If you want complete control over the layout, you could use a titlepage environment instead. So, let’s design a nice title page for our book of equations.

In Chapter 2, we already used some formatting commands, such as \centering, and font size and shape commands, such as \Huge and \bfseries, to format a title. We will do it similarly within a titlepage environment:

  1. Create a file, title.tex, with the following content:
    \begin{titlepage}
    \raggedleft
    {\Large The Author\\[1in]}
    {\large The Big Book of\\}
    {\Huge\scshape Equations\\[.2in]}
    {\large Packed with hundreds of examples and solutions\\}
    \vfill
    {\itshape 2011, Publishing company}
    \end{titlepage}
  2. Add this line right after \frontmatter:
    \include{title}
  3. Our final book will be in A5 format, and so will the title page. Therefore, let’s add that to the preamble:
    \usepackage[a5paper]{geometry}
  4. Compile; now we’ve got a title page:

Figure 11.4 – A title page

Figure 11.4 – A title page

The titlepage environment places its contents on a separate page. Although this title page will be numbered like any other page, the page number won’t be printed on that page.

In this environment, we used basic LaTeX font commands to modify the font size and style. By grouping with curly braces, we limited the scope of those commands. In square brackets after line breaks such as \\[.2in], we added a bit of extra vertical space before the following line. The \vfill command inserts a flexible vertical space that stretches as far as possible to fill the entire page. This way, we pushed the last line toward the bottom of the page.

Note that this page has the same page dimensions as the other pages in the document. That means, in a double-sided book, that it’s a right-hand page. Thus, you may notice the unequal left and right margins, which might look undesirable, especially if your title is in the center. However, the explanation is simple: this title page is intended to be an inner title, not the cover page. The inner title page is, of course, a right-hand page.

A cover page is different. Such a page should be one-sided, and thus it should have equal left and right margins. A cover page is often produced as a standalone document and printed separately. For an electronic document, you may use the pdfpages package. Refer to the Including whole pages section in Chapter 6, Including Images, or the Combining PDF files section in Chapter 9, Optimizing PDF files, in the LaTeX Cookbook, and to https://texdoc.org/pkg/pdfpages.

The titling package offers features to create sophisticated title pages. To get some ideas on how title pages may be designed, you could look at Some Examples of Title Pages, by Peter Wilson, available from texdoc titlepages and at https://texdoc.org/pkg/titlepages.

A document framework consisting of files, headings, a title page, and style settings is called a template. We will learn how to use templates in the next section.

When creating a document, we specify the document class, choose appropriate packages and options, and establish a structure for the content. Repeating these steps for every new document quickly becomes tedious.

If you plan to produce multiple documents of the same type, you may create a template. This could be a .tex file containing the following:

  • A declaration of a suitable document class together with meaningful options
  • Routinely used packages that are most suitable for your document type
  • A predefined layout for the header, footer, and main text
  • Self-made macros to streamline your work
  • A framework of sectioning commands, where you fill in the headings and the body text
  • A framework containing \include or \input commands, for which you can create the body text chunks later

As your LaTeX skills grow, your templates will evolve and become better and more sophisticated. Many users publish their elaborate templates online. Many universities, institutes, journals, and publishers do the same, offering official templates for documents such as theses, papers, journal articles, and books that match their formatting requirements.

You can find a carefully curated collection of templates, arranged by document types such as theses, reports, letters, and presentations, along with sample outputs, in a template gallery at https://latextemplates.com.

You may download a template and start to fill in your text. Alternatively, you could start a document with a predefined template offered by your editor. Let’s try that first.

LaTeX editors often provide templates to start with. TeXworks offers some as well. So, we’ll test this feature. Let’s take one, open it, modify it, and compile it:

  1. In the TeXworks main menu, click File, then New from template. A window will open, allowing you to choose a template:

Figure 11.5 – TeXworks template selection

Figure 11.5 – TeXworks template selection

  1. In the lower part of the window, you can read the template’s source. Here’s an example of KOMA-Script (KOMA-letter.tex):
    % !TEX TS-program = pdflatex
    % !TEX encoding = UTF-8 Unicode
    % An alternative to the standard LaTeX letter class.
    \documentclass[fontsize=12pt, paper=a4]{scrlttr2}
    % Don't forget to read the KOMA-Script documentation,
    % scrguien.pdf
    \setkomavar{fromname}{} % your name
    \setkomavar{fromaddress}{Address \\ of \\ Sender}
    \setkomavar{signature}{} % printed after the \closing
    \renewcommand{\raggedsignature}{\raggedright} % make
    % the signature ragged right
    \setkomavar{subject}{} % subject of the letter
    \begin{document}
    \begin{letter}{Name and \\ Address \\ of \\ Recipient}
    \opening{} % eg. Hello
    \closing{} %eg. Regards
    \end{letter}
    \end{document}
  2. Click on Open. Fill in the gaps and edit the filler text of the example:
    \documentclass[fontsize=12pt, paper=a4]{scrlttr2}
    \setkomavar{fromname}{My name} % your name
    \setkomavar{fromaddress}{Street, City}
    \setkomavar{signature}{Name} % printed after the \closing
    \setkomavar{subject}{Invoice 1/2021} % subject of the letter
    \setkomavar{place}{Place}
    \setkomavar{date}{January 1, 2021}
    \begin{document}
    \begin{letter}{Customer Name\\ Street No. X \\ City \\ Zipcode}
    \opening{To whom it may concern} % eg. Hello
    Text follows \ldots
    \bigskip
    \closing{With kind regards} %eg. Regards
    \end{letter}
    \end{document}
  3. Compile the document. Have a look at our test letter:

Figure 11.6 – A letter document

Figure 11.6 – A letter document

That was easy! We just opened the template and modified the placeholder text. By reading the KOMA-Script documentation, we can learn that the \setkomavar command is used to specify values for template parameters such as name, address, and subject. We used that to declare date and place as well.

Once you’ve written your personal data into this template, you may save that for later use instead of typing our address for each letter.

The KOMA-Script documentation, available via texdoc koma-script and at https://texdoc.org/pkg/koma-script, describes the features of the scrlttr2 class well. Using this, you can create a professional-looking business letter template.

Imagine putting a job application letter created with the LaTeX layout and fonts together with the microtype package next to an application letter produced with some other word processing software. Which one will create a better impression?

While looking for LaTeX templates, code, and tips on the internet, you will encounter a lot of information and code. This code might be outdated, and the information might be obsolete.

When you develop your own template, you probably want to use the best packages, options, and modern solutions available today. How can you be sure?

Both questions can be answered by studying the l2tabu document. This name is the common shortcut for An essential guide to LaTeX2e usage. This document highlights obsolete commands and packages and demonstrates the most common and severe mistakes LaTeX users make. As LaTeX has developed over many years, some packages and techniques are still available and described in online resources, but they may no longer be recommended. Read this guide. It will help you evaluate the templates and code you find online and ensure that you produce optimal code yourself.

Just type texdoc l2tabuen (en stands for English) at the command prompt or visit https://texdoc.org/pkg/l2tabuen.

To test a template, you may use the blindtext package and its commands, \blindtext and \Blinddocument. The \blindtext command generates a paragraph of dummy text, and the \Blinddocument command generates dummy content for a big document, including sections and lists. That will demonstrate the template’s output quality. When using this package, we should load the babel package with a language option, for example, just with a basic minimal article template:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\begin{abstract}
\blindtext
\end{abstract}
\Blinddocument
\end{document}

That gives us a document starting like this:

Figure 11.7 – An article with dummy text

Figure 11.7 – An article with dummy text

If you use the TeXworks editor, as we did in the previous example, you can choose from some ready-to-use templates or download a template from https://latextemplates.com. However, if you work online with https://overleaf.com, you have even more choices. Basically, you can click on New Project and choose one of several basic templates:

Figure 11.8 – Opening a template in Overleaf

Figure 11.8 – Opening a template in Overleaf

If you click on View All, you can browse a comprehensive catalog:

Figure 11.9 – The Overleaf template catalog

Figure 11.9 – The Overleaf template catalog

The Overleaf template collection contains several thousand ready-to-use templates for filling in your text. Institutions and users contribute a lot of them. Quality may vary, but you can see screenshots of the contents or titles while browsing and try out those templates yourself.

You can enter keywords such as the document type, university or college name, features, or package names in the Search bar.

You can click on Open as Template and get a compilable document with some filler text. That lets you try out about 10 templates in around 10 minutes until you find the perfect one.

Some templates are fairly complex, with content split across multiple files and arranged in folders. Let’s look at how we can set up such a structure ourselves next.

Earlier in this chapter, we learned how to split a document into multiple files. As a project grows (think of a thesis or a book), you will quickly accumulate many of them: chapter .tex files, appendices, front and back matter, bibliography files, figures in various formats, and perhaps files with custom macros as well.

Keeping everything in a single folder quickly becomes hard to manage. A better approach is to group related files into folders and subfolders. This makes the project easier to navigate and maintain as it grows, especially because LaTeX also creates auxiliary files in your project folder.

Here’s a simple example structure with placeholder names to show how you might set things up:

project/
preamble.tex
macros.tex
main.tex
university-style.sty
frontmatter/
titlepage.tex
abstract.tex
preface.tex
acknowledgements.tex
chapters/
chapter01-intro.tex
chapter02-methods.tex
...
figures/
logo.png
chapter01/
plot.png
diagram.png
chapter02/
...
tables/
...
appendices/
appendixA.tex
appendixB.tex
backmatter/
bibliography.tex
indexes.tex
declaration.tex

Such a project structure gives you better control over all parts of your content. Good naming conventions, such as numeric prefixes and descriptive filenames, help too.

Your main.tex file may then look something like this:

\documentclass[12pt,a4paper]{report}
\usepackage{university-style}
\input{preamble}
\input{macros}
\usepackage{graphicx}
\graphicspath{
{figures/}
{figures/chapter01/}
{figures/chapter02/}
}
\begin{document}
\input{frontmatter/titlepage}
\input{frontmatter/abstract}
\input{frontmatter/preface}
\input{frontmatter/acknowledgements}
\include{chapters/chapter01-intro}
\include{chapters/chapter02-methods}
\appendix
\include{appendices/appendixA}
\include{appendices/appendixB}
\input{backmatter/bibliography}
\input{backmatter/indexes}
\input{backmatter/declaration}
\end{document}

The \graphicspath command specifies where LaTeX looks for image files. By defining these paths once in the preamble, you can include graphics without repeating long directory names.

Now, you can concentrate on most of your work in the chapter files.

From time to time, make a backup of the entire project folder, such as a project.zip file, and store it elsewhere in case your computer is damaged. For larger projects, it’s a good idea to use version control, such as Git, to keep track of changes over time. If you’re new to it, the official Git documentation at https://git-scm.com is a clear, beginner-friendly starting point.

The techniques introduced in this chapter help you create and maintain bigger projects. Many people learn LaTeX mainly because they plan to write longer texts, such as a thesis or a book. However, splitting documents and using templates are helpful for short pieces of writing as well, such as letters; think of the header, footer, and address fields.

In this chapter, we created and managed large documents consisting of several files, including front and back matter and a separate title page, and organized them in a good folder structure.

Now that we can develop and handle large documents, we will see how to improve them further in the next chapter.