Stuctured documents

Overview:

  • Teaching: 20 min
  • Exercises: 0 min

Objectives

  • Creating titles and abstracts in $\LaTeX$ documents
  • Organising documents into sections
  • Utilising $\LaTeX$'s automatic cross-referencing functionality

Structure is Essential in Anything that Thrives

(Quote: Henry Cloud)

As you might have noticed, so far all we have done is insert text and equations into a document, which is sufficient to produce the main body of text. However we will also be wanting document titles, abstracts, section headers and breaks, author names, and the like in our document too - and all of these will need to be formatted differently to the main body of text. $\LaTeX$ helps us with this by the use of some more predefined commands; these commands impose a common structure and format across the entire document to provide us with that professional finish.

Document Class

There are some commands that are specific to a given document class - for example the abstract environment cannot be used in a book document class (without encoding a workaround) but can be used in the article class. In these lessons we will only be considering the article class, so all the environments you'll see will be compatable with this class of document.

Starting Small: Document Title and Author

Let's start simple and have $\LaTeX$ automatically generate the title of our document, and automatically insert the author. To do this we need to make use of the \maketitle command, and also predefine a few things in the document preamble. In the preamble we need to tell $\LaTeX$ the title of the document (via \title) and the author(s) (via \author). We can also optionally provide $\LaTeX$ with the date the document should be signed with (via \date) or let $\LaTeX$ autofill the date for us (\date{\today}). Then once we \begin{document}, we insert the \maketitle command and $\LaTeX$ will look up the information we added, and render the title in the default style for the document class.

NOTE: You can put the \maketitle command anywhere in the document (and even in multiple times) and $\LaTeX$ will happily render your title in the place it is told, as many times as it's told. Needless to say, this isn't recommended, but you can do it!

Try the following code in your editor and see for yourself:

\documentclass[]{article}
\usepackage[utf8]{inputenc}

\title{On Contact with the Wizarding World}
\author{Phillip D. Student}
\date{\today}

\begin{document}

\maketitle % This line make the title show up at the beginning of your document

This is some text after your title has rendered.

\end{document}
In [1]:
%%pdflatex
\title{On Contact with the Wizarding World}
\author{Phillip D. Student}
\date{\today}
 
\maketitle % This line make the title show up at the beginning of your document
 
This is some text after your title has rendered.
Out[1]:

Source Code

Please note that the syntax we are giving you is different to that which we are running in this notebook with the %%pdflatex magic. Notably the absence of the preamble and \begin{document} syntax; this is because the %%pdflatex command lets us break a few of the rules.

Again, this is a nuiance of our method of generating these lessons. If we provide you with source code and then you see something slightly different being run, you should take the source code we provide rather than copying what we run in the notebook. All the more reason to be working with some of your own work as we go through these lessons!

Abstracts

In scientific documents, it's a common practice to include a brief overview of the main subject of the paper, or an abstract. $\LaTeX$ has an environment, the creatively named abstract environment, for this purpose. The abstract environment will put the text in a special format, typically making the text centre-aligned (rather than fixed-width alignment) and smaller than the main body of text. Abstracts are typically short (no more than a paragraph say) but $\LaTeX$ won't stop you from entering as much as you want into the environment!

NOTE: Again, like the \maketitle command, the abstract environment can be placed anywhere and as many times as you like in a document, so make sure you put it in the right place. In general it is bad practice to have more than one abstract environment in a document, but again, $\LaTeX$ won't stop you (but it might complain).

\documentclass[]{article}
\usepackage[utf8]{inputenc}

\title{On Contact with the Wizarding World}
\author{Phillip D. Student}
\date{\today}

\begin{document}

\maketitle % This line make the title show up at the beginning of your document

\begin{abstract}
The failure of world governments to make contact with the wizarding world, or rather the wizarding world's continued ability to shield themselves from us, has long stifled international development and induced tensions between non-magic users (so called ``muggles") and those with magic capabilities.
The work in this thesis explores methods that could potentially be exploited for removing the barrier that has been so carefully constructed to separate these two societies, by study of sources detailing life in the wizarding world.
The work in this thesis builds off the studies of Rowling, J. K; Lewis, C. S.; and Tolkien, J.R.R in it's investigation into the origin of the magic world and potential avenues to make contact.
\end{abstract}

This is some text after your title and abstract has rendered.

\end{document}
In [2]:
%%pdflatex
\title{On Contact with the Wizarding World}
\author{Phillip D. Student}
\date{\today}

\maketitle % This line make the title show up at the beginning of your document

\begin{abstract}
The failure of world governments to make contact with the wizarding world, or rather the wizarding world's continued ability to shield themselves from us, has long stifled international development and induced tensions between non-magic users (so called ``muggles") and those with magic capabilities.
The work in this thesis explores methods that could potentially be exploited for removing the barrier that has been so carefully constructed to separate these two societies, by study of sources detailing life in the wizarding world.
The work in this thesis builds off the studies of Rowling, J. K; Lewis, C. S.; and Tolkien, J.R.R in it's investigation into the origin of the magic world and potential avenues to make contact.
\end{abstract}

This is some text after your title and abstract has rendered.
Out[2]:

Further Customisation

If you aren't happy with the default layouts or formatting of the environments $\LaTeX$ provides, you can change them using some more commands in the preamble of your document. We won't be covering this level of customisation here, because most of the time the journal or institution you are writing your document for will be providing you with a template file that will impose their formatting preferances on your document.

Sections and subsections

As mentioned previously, commands to organise and break a document into parts depend on the document class you are using. $\LaTeX$ uses these commands to automatically number and index parts of a document. The most commonly used command to divide up a document is \section; the command \section{title here} will create a new section called title here, a header in your document, and will renumber all the other sections in your document to account for the new one. Using sections also allows $\LaTeX$ to automatically generate a contents page in some documents. Sections will be automatically numbered for you - if you don't want your section labelled, you put an asterisk after the \section command (similarly to environments).

There are also commands for creating subsections, again creatively called \subsection{subtitle here} which work in the same way as the parent \section command. In total, $\LaTeX$ will let you nest all the way into \subsubsubsection - IE three levels below \section! You can also suppress the automatic numbering of \subsections (and below) using an asterisk.

See the example below illustrating the use of sections and subsections.

In [3]:
%%pdflatex
\section{Benefits of Magic}
In this section we consider some of the benefits that having access to magic would bring on mankind.

\subsection{Benefits of Magical Animals}
There are several magical animals that are yet undiscovered that could provide huge boons to our everyday lifestyle and working environment.
Oliphaunts have the potential to eliminate the need for heavy construction machinery and heavy-goods transport, and provide little to no air pollution as an additional benefit.
Phoenixes, with their healing ability and ease of flight, could effectively act quickly to prevent disaster in environmental crises.

\section{Socio-Economic Considerations}
In this section we will explore some of the social and economic considerations that governments should discuss prior to making contact with the wizarding world.

\subsection*{On Responsible Use of Magic}
With great power comes great responsibility, and so it is only natural that the general population should be subject to a vetting process before being granted access to magical abilities and training.
In turn we should ensure that governments are not dominated by magic-using and muggle faction splits, else we create the very divide in society that we are working so hard to eliminate.

\subsection*{On Global Trade and Industry}
With the ability to apparate, several industries will be negatively impacted.
Commercial travel will be particularly affected, with predictions of up to 95\% loss of gross capital.
In turn the oil and gas industries will suffer from the decreased demand for commercially available fuel, but may benefit substantially from use of the alchemical craft to ``go greener", so to speak.latex article
Out[3]:

Note that \section (and the various sub-s) are commands and not environments; this is why we don't \begin and \end them. Also notice the placement of the asterisk on the \subsection* command - it goes between the curly braces and the name of the command itself.

Page Breaks

You may notice that the main body of text in your document starts immediately after your title (and abstract) have been rendered. This is undesirable in several contexts, so we can use the \newpage command to tell $\LaTeX$ to start a new page. This command works similarly to the \newline command we saw in lesson 1, only this one forces a new page rather than a new line!

Labels and cross referencing

In $\LaTeX$ almost anything can be numbered, tracked, and referred back to using the \ref and \label commands. Combined with the fact that $\LaTeX$ automatically handles updating the numbering, this means you can write and restructure your document almost at will. Some of the common objects which can be referenced include:

  • Sections and subsections, as you've seen
  • Footnotes
  • Theorems, lemmas, definitions, and even proofs
  • Equations
  • Figures and tables

The commands are used in the following way:

  • \label{marker}: Gives the object you want to reference a marker, named marker. Use this name when you want to refer back to this object.
  • \ref{marker}: Calls on the object with the specified marker. This will print the number that was assigned (automatically by $\LaTeX$) to the object.

We can see some examples in the code below.

\documentclass[]{article}
\usepackage[utf8]{inputenc}

\begin{document}

\section{Benefits of Magic} \label{sec:BenefitsOfMagic}
In this section we consider some of the benefits that having access to magic would bring on mankind.

\subsection{Benefits of Magical Animals} \label{sec:Animals}
There are several magical animals that are yet undiscovered that could provide huge boons to our everyday lifestyle and working environment.
Oliphaunts have the potential to eliminate the need for heavy construction machinery and heavy-goods transport, and provide little to no air pollution as an additional benefit.
Indeed, we can even predict the resulting pattern in the concentration of carbon dioxide in the atmosphere ($c_{atm}$) on a timescale of years ($t$) after this switch, using equation (\ref{eq:C02Reduction}):
\begin{equation} \label{eq:CO2Reduction}
    c_{atm} = c_{0} e^{-\sigma t},
\end{equation}
where $c_{0}$ is the current concentration of carbon dioxide in the atmosphere and $\sigma>0$ is the so-called ``Oliphaunt efficiency coefficient".

\section{Socio-Economic Considerations}
In this section we will explore some of the social and economic considerations that governments should discuss prior to making contact with the wizarding world.

\subsection*{On Global Trade and Industry}
In particular, in section \ref{sec:Animals} we highlighted some of the beneficial impacts of oliphaunts.
In particular we demonstrated that the concentration of carbon dioxide in the atmosphere would evolve according to equation \ref{eq:CO2Reduction}; inserting our predicted values for $\sigma$ and reported values for $c_{0}$ yields the conclusion that global carbon dioxide levels will fall to pre-industrial levels in as few as 7.5 years.

\end{document}

Notice how we don't have to label things we don't want to refer back to (so we didn't give \section{Socio-Economic Considerations} a label), nor does $\LaTeX$ mind if we give something a label and then don't ever refer to it (\section{Benefits of Magic}).

There is also no need to include the sec: or eq: parts of the markers in your labels, however we recommend adopting a system like this as it will help you keep track of the content of your document!

Another Note: Parentheses around Equation References

You'll notice that the \ref command for our equation reference is placed in brackets - removing these brackets will simply print the equation number, however it is common practice to reference equation numbers in parentheses. If you have loaded the amsmath package, you can replace the \ref command with the \eqref command; it behaves in exactly the same way but will automatically enclose the reference number in brackets. From here on out, we will tend to use \eqref for equations, and \ref for everything else.

(??) References

If you are using a local $\LaTeX$ editor or a "quick build" on Overleaf; you may notice that your \ref commands are producing question marks, rather than the numbers you expect. For example, the syntax below should produce an equation, with a number, and then refer to this number in the sentence preceeding the equation:

In [4]:
%%pdflatex
We can model the population $P(t)$ of Oliphants using the Logistic Growth Equation \eqref{eq:LogGrowth};
\begin{align} \label{eq:LogGrowth}
    \dfrac{\mathrm{d} P}{\mathrm{d} t} &= rP \left( 1 - \frac{P}{K} \right),
\end{align}
where $r$ is the relative growth rate coefficient, and $K$ is the carrying capacity of the environment.
Out[4]:

But instead, our equation number that should replace \eqref{eq:LogGrowth} prints out as a double question-mark. This is because we need to run BibLaTeX (or BibTex, depending on your install) alongside $\LaTeX$ when we compile our document, to keep track of the references for us. If you are working on Overleaf you likely won't have this problem, because Overleaf usually detects when your references have changed and will automatically update them. If you are working locally though, you'll need to run $\LaTeX$ on your source code first, then run BibTeX (or BibLaTeX) on it next, then run $\LaTeX$ on it again, to get the references to match up. However, there is no need to do this 3-step run every time you want to compile the document - you only need to do it when you move/change or introduce additonal \labels or \refs.

If you have done the 3-stage compile above and still have question marks, this indicates a missing reference (and can also occur for sections, chapters, and external citations too). There's probably a typo in your \label and \ref.

Source Code

You can download the source code (.tex) file for the document that was created in this lesson by clicking here.

Key Points:

  • If we tell $\LaTeX$ the \title, \author, and \date in the preamble, we can use \maketitle to render the document title.
  • We can divide our document into \sections and \subsections.
  • We use \label and \ref to utilise $\LaTeX$'s automatic cross-referencing capabilities.