Getting Started: Subdividing a Large Document

It’s easy and convenient to use just one .tex file when writing short documents. But for anything larger than, say, 10,000 words, I tend to divide the document into smaller parts so each chapter is its own .tex file.

For example, you might be writing a thesis that includes three chapters (introduction, results, conclusion) and an appendix. These are called introduction.tex, results.tex, conclusion.tex and appendix.tex.

These four files are like any other .tex file except you omit the preamble (\documentclass, \usepackage, etc.) and the \begin{document} and \end{document} commands. The reason you don’t include these commands is that they are included in your main file, thesis.tex, ensuring that your formatting is consistent throughout the final output document. This also means that page numbers and footnotes will be numbered correctly all the way through.

Which Commands to Use

There are two commands you can use in your main file, thesis.tex, to insert your four chapter files: \include and \input.

The \input command slots each chapter in straight after the one before it without any pagebreaks, while \include works by starting each new chapter on a new page and ends it with a clearpage command. (Thanks to the latex-community forum for help on explaining this to me!)

In other words, ‘natural candidates for \include are whole chapters of a book but not necessarily small fractions of text,’ for which \input would be more appropriate. (Quotation from The LaTeX Companion.)

Your thesis.tex file would therefore look something like this:

\documentclass{article}
\usepackage{semtrans}

\begin{document}

\title{My Thesis}
\author{John E. Smith}
\maketitle
\clearpage

\include{introduction}
\include{results}
\include{conclusion}
\include{appendix}

\end{document}

Note: your four files (introduction.tex, results.tex, conclusion.tex and appendix.tex) must be saved in the same folder as thesis.tex, otherwise it won’t work.

Omitting Files

If you wanted to produce a PDF from the file thesis.tex without the appendix, all you have to do is comment out that particular \include command (you just add a % at the beginning of the line): %\include{appendix}

Useful Links

2 thoughts on “Getting Started: Subdividing a Large Document”

  1. Notice that the practice of dividing large projects into a few manageable files, so that it’s easier to edit them, is much less useful nowadays, since there exist two things:
    1. Editors with a feature to display only part of the file (e.g., displaying one chapter and hiding the rest); my choice (GNU Emacs) calls this “outline mode” (btw, I personally don’t use it); also LEd (“LaTeX Editor” – a Windows one, more of a `clicking’ kind) has such a feature.
    2. Also, many editors (including Emacs, of course, but probably LEd and others, too) have the possibility to `partially compile’ a file, i.e., they extract the preamble and a given part (say, the selection) into a temporary file and run LaTeX on this file only.
    In other words, if you just want to make your work with a large project easier, there seems to be no point in dividing it into many files.

    However, there still exist *other* reasons why you might want to divide your (La)TeX project into more than one file. One of them is reusable parts – for example, a `short’ and `long’ version of some document (say, a `getting started’ and full manuals). You may then \include the same file file (say, on some basic operations) in *two* larger ones.

    A variant on this (i.e., reusing) is a setup I’ve made for myself some time ago: I prepared a rather large problem book (400+ problems in maths); each problem was described using the following structure:
    \begin{exercise}
    \exerciselabel{…}
    \begin{question}

    \end{question}
    \hint{…}
    \source[…]
    \answer{…}
    \begin{solution}

    \end{solution}
    \begin{remark}

    \end{remark}
    \end{exercise}
    (most of the parts being optional). Then, I first \input them with a setup that typeset only the “question” part, then changed the macros so that only “hints” get typeset, and finally the answers. This gave me a file with: first, many pages of problems, then a page with hints, and finally a few pages of answers. In another file, I made the arrangements so that every detail of every exercise was in the same place, and it was a very handy material for teaching. Without putting the content into a separate file (which was thus \input thrice in the students’ version and once in my one) this wouldn’t be very convenient.

    (Apparently, ConTeXt has some features precisely with such a behaviour in mind, but I didn’t explore them yet. I haven’t switched completely yet;)).

  2. Isn’t it much better to use \includeonly directives to select which bits to actually include at any point instead of commenting specific \include lines out? This way, all the reference information (labels, citations) are still there as well as the page numbers?

    I’m, of course, talking about working on large documents, like a thesis, where you will eventually be including all the sections/chapters but want to concentrate on one bit at a time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s