LaTeX root file - main.tex

A LaTeX root file main.tex tells the LaTeX typesetting how to build the document. The title page, stylesheets, table of contents, chapters, and appendices are references in the root file using \include commands.

Simplified root file

The following is a simplified root file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass[a4paper,12pt,draft,twoside]{book}
\usepackage{styles/mystyles}
\begin{document}
\frontmatter
\include{tex/title}
\tableofcontents
\mainmatter
\include{tex/preface}
\include{tex/intro}
\include{tex/chap01}
\include{tex/chap02}
\include{tex/summary}
\backmatter
\end{document}

let’s go through this file section by section.


Note: Commands included before the \begin{document} command apply to the entire document. They are outside any scope, or fixed section of the document stream.


Define the document class

The first line tells the LaTeX generator the type of document it is about to generate (or typeset) and what styles are to be used.

1
\documentclass[a4paper,12pt,draft,twoside]{book}

All these elements are defined in separate class files (more about these classes later).

Reference the stylesheet

The next line tell the typesetter to use the styles defined in an external document called mystyles located in the styles directory.

1
\usepackage{styles/mystyles}

Begin the document scope

Like the <body> tag in HTML, the actual content follows this tag. The \begin tag tells the typesetter that what follows is the document.

1
\begin{document}

You put meta information and additional instructions before this tag such as \title, \author, \date, and so on.

The document section is divided into three sections:

Insert the title page and table of contents

Within the \frontmattersection, you include a title page. Alternatively, the {book} class has a default title page which can be referred to simply as titlepage.

Also, in this section you can add a table of contents, which is then automatically generated during output.

1
2
3
\frontmatter
\include{tex/title}
\tableofcontents

Insert the document content

The document itself, namely, the contents, is contained within the \mainmatter section.

1
\mainmatter

You then follow this tag with references to the topics, or chapters, of the document.


Note: To keep this simple and to allow chapters to be toggled off or on (included/excluded) as required, my practise is to place topic contents in sets of files contained within a subdirectory called \tex.


The \include command tells the typesetter to include the file referenced within the curly braces in the output stream at the location in the list of includes. You can easily rearrange the topics by rearranging these \include lines in this list.

1
2
3
4
5
6
    \include{tex/preface}
    \include{tex/intro}
    \include{tex/chap01}
    \include{tex/chap02}
    \include{tex/summary}
    \include(tex/appendix)

Insert an index, if required

A \backmatter section can also be included, which typically includes the Index.

1
    \backmatter

Note: You would also define your headers and footers here, but I excluded them in this example to keep it simple. I will get to them in due time.


End the Document Scope

You need to tell the typesetter when the document is complete and that there are no more files to generate. In other words, you need to close your document, as with html </html>.`

1
    \end{document}

This simple command indicates the end of the document and go ahead and generate the output.