Vladar's Blog

LaTeX document structure

Today we will cover the basic concepts of the LaTeX document structure you'll need to know when creating a book from scratch.

Contents

Subfiles ↩

Once your document grows to a particular size, it could become somewhat inconvenient to navigate the file. I've mentioned the *.sty files previously, and today we will cover subfiles1 — a handy way to keep parts of your complex projects separated.

So, let's say you want to have each chapter of your project in its own file. Include the following lines into your main project file:

  1. \usepackage{subfiles} (before \begin{document})
  2. \subfile{filename} (inside the document environment, where "filename" is your chapter file)

If you're using the example project from before, now it will look like this:

example.tex

\documentclass[10pt,twoside,twocolumn,openany]{book}
\usepackage{example}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{subfiles}

\title{\scshape\LaTeX~Example}
\author{Vladar}

\begin{document}

\maketitle

\subfile{example_sub1}

\subfile{example_sub2}

\end{document}

Naturally, you'll need to create these two new files. To have some contents to demonstrate the features described below, I've added some tables and figures. These will be described in the future blog posts, so just copy-paste it all as is for now. Also, create an img directory and place a placeholder image into it. I've used this one: pugknight.png

Finally, you need to add these lines into your example.sty file:

\usepackage{graphicx}
\graphicspath{ {./img/} }

Each subfile should start with this line:

\documentclass[project]{subfiles}

With project replaced with your project's name (in our case — example).

example_sub1.tex

\documentclass[example]{subfiles}

\begin{document}

\chapter{First One}

\section{Orcs}

Here's an example of a custom command defined in \verb|example.sty| that formats a monster stat block:

\index{Orcs}

\statblock[d6]{Orcs}{MV 40'; AC 6; HD 1; AB +1; \#AT 1 scimitar (1d8); SV F1; AL C; Int 7; ML 8; XP 10.}{}

\begin{table}[h!]
    \centering
    \caption{Encountering Orcs}
    \vspace{1ex}
    \label{table:1}
    \begin{tabular}{c|l}
    	\textbf{d6} & \textbf{Orcs' Reaction} \\
    	\hline
    	1--3 & immediate attack \\
    	4--5 & hostile, ready to attack \\
    	6 & pretend to be friendly, attack later \\
    \end{tabular}
\end{table}

\end{document}

example_sub2.tex

\documentclass[example]{subfiles}

\begin{document}

\chapter{Second One}

\section{Pug Knights}

\begin{table}[h!]
    \centering
    \caption{Random Encounter}
    \vspace{1ex}
    \label{table:1}
    \begin{tabular}{c|l}
    	\textbf{d6} & \textbf{Encounter} \\
    	\hline
    	1--3 & first pug knight \\
    	4--5 & second pug knight \\
    	6 & two pug knights jousting \\
    \end{tabular}
\end{table}

\subsection{First Pug Knight}

\begin{figure}[h!]
    \index{Pug Knight}
    \centering
    \includegraphics[width=\linewidth]{pugknight}
    \caption{First Pug Knight}
\end{figure}

\clearpage

\subsection{Second Pug Knight}

\begin{figure}[h!]
    \index{Pug Knight}
    \centering
    \scalebox{-1}[1]{\includegraphics[width=\linewidth]{pugknight}}
    \caption{Second Pug Knight}
\end{figure}

\clearpage

\index{Lorem ipsum}
\lipsum[1-7]

\end{document}

Now, if you compile your main project file, it will include the contents from both subfiles.

If you use a LaTeX editor, you probably already know what button to press to assemble the PDF. Otherwise, consult your TeX distribution's documentation. For example, if you are using TeX Live2, just run the following command:

pdflatex example.tex

Table of Contents ↩

Now, for the fun part. If you have your document structure in order, creating the table of contents takes a single command:

\tableofcontents

If you want to limit the depth of your ToC, use this command beforehand:

\setcounter{tocdepth}{1}

The number corresponds to the depth level:

  1. chapter
  2. section
  3. subsection
  4. subsubsection
  5. paragraph
  6. subparagraph

We don't have anything deeper than subsection currently, but try to add some and test it. Also, keep in mind, that sometimes you'll need to run the build process twice to see the changes in your ToC.

There are multiple ways to customize the ToC. Here are the most basic ones.

Changing the title

\renewcommand*\contentsname{Table of Contents}

Disabling hyphenation

\hyphenchar\font=-1
\tableofcontents
\hyphenchar\font=`\-

Multiple columns

This is easily done with the multitoc3 package:

\usepackage[toc,lot,lof]{multitoc}

You can control the number of columns like this:

\renewcommand{\multicolumntoc}{3}

List of Tables and Figures ↩

Similarly to the ToC, you may have a list of tables and figures:

\listoftables

\listoffigures

To be in these lists, your table and figure environments must have captions:

\caption{Name}

Just like the ToC, you can change their titles:

\renewcommand*\listtablename{Tables}

\renewcommand*\listfigurename{Figures}

Or the number of columns (through the multitoc package):

\renewcommand{\multicolumnlot}{3}

\renewcommand{\multicolumnlof}{3}

Index ↩

Finally, you can have an index (or even multiple ones). I recommend using the imakeidx4 package for this.

\RequirePackage{imakeidx}

% default index
\makeindex

% another index
\makeindex[name=anotherindex, title={Another Index}]

To add an entry to the index, use the index command:

\index{Entry}

\index[anotherindex]{Entry}

For more options, check out sophisticated indexing reference.

And to print it all out:

\printindex

\printindex[anotherindex]

To adjust the style, you can use custom style files (options parameter from imakeidx) and idxlayout5 package.

For example, here are the ones similar to Into the Dungeon: Revived:

\usepackage{imakeidx}
\makeindex[options=-s example]
\usepackage[totoc,columns=3,columnsep=1em,font=small,indentunit=1em,rule=0px]{idxlayout}

Add the example.ist file to your project's directory:

heading_prefix "{\\bfseries\\hfil "
heading_suffix "\\hfil}\\nopagebreak\n"
headings_flag 1
delim_0 "\\dotfill"
delim_1 "\\dotfill"
delim_2 "\\dotfill"

As a homework, I'll leave it to you to discover what each parameter does.


That's all for today. See you next time for more LaTeX goodness!

Discuss this post on Reddit

end illustration

  1. https://www.ctan.org/pkg/subfiles↩

  2. https://tug.org/texlive/↩

  3. https://www.ctan.org/pkg/multitoc↩

  4. https://www.ctan.org/pkg/imakeidx↩

  5. https://www.ctan.org/pkg/idxlayout↩

#gamedev #guide #latex #software #ttrpg