latex-skript/content/basic-document-structure.tex

144 lines
10 KiB
TeX

\chapter{Basic document structure}
\label{sec:basic-document-structure}
In essence, every \LaTeX{} document is composed of two parts:
We call the first commands within our \LaTeX{} document the \emph{preamble}.
It specifies global properties of our document, such as the document class, the encoding, the language, the page format, and additional packages that we want to use.
The \emph{document environment}, on the other hand, contains the actual content of our document, i.\,e., the things that we will later see in our generated \acro{PDF} file.
\Example{lst:latex-document-basic-structure}{basic-document-structure/hello-world}{basic-document-structure/hello-world_crop}{Exemplary structure of a simple \LaTeX{} document with preamble and document environment}
\section{Preamble}
Let's take a closer look at the preamble.
A minimal preamble should contain the following specifications:
\subsection{Document class}\label{sec:document-class}
We can define a document class by using the command \code{latex}{\textbackslash documentclass[<para- meter>]\{<document class>\}}.
The most commonly used document classes that are supported by default are \pkg{article} for short documents, and \pkg{report} for longer ones.
Furthermore, you can use \pkg{book} for books, \pkg{beamer}\footnote{We do not cover making presentations in \LaTeX{} in this tutorial. However, if you are interested in the topic, we recommend this introduction on Overleaf: \url{https://www.overleaf.com/learn/latex/Beamer}} for presentations, and \pkg{letter}\footnote{We also do not cover letters in this script. An introduction can be found on WikiBooks: \url{https://en.wikibooks.org/wiki/LaTeX/Letters}} for letters.
In addition to the standard document classes, the \acro{KOMA} script classes have been developed.
They provide alternatives to the document classes mentioned above:
In lieu of \pkg{article} you can use \pkg{scrartcl}, \pkg{report} is replaced by \pkg{scrreprt},\footnote{Those vowels are indeed missing, do not try to insert them.} and \pkg{scrbook} can be used instead of \pkg{book}.
As a replacement for \pkg{letter}, one can use \pkg{scrlttr2}.
A complete list of all \acro{KOMA} script classes is available online.\footnote{Available at: \url{https://komascript.de/komascriptbestandteile}}
By using \acro{KOMA} document classes, the layout of the generated \acro{PDF} document is changed.
On top of that, they provide additional functionalities.
The standard document classes are designed according to US-American conventions
whereas \acro{KOMA} classes adhere to European norms, e.\,g., for
writing letters.
Each \code{latex}{\textbackslash documentclass} command can hold optional parameters in
square brackets.
\code{latex}{\textbackslash documentclass[10pt,a5paper,landscape]scrartcl\}},
for instance, configures a \acro{KOMA} script article and sets its font size to
10\,pt,\footnote{The standard font size is 12\,pt.} the page size to
A5,\footnote{The default case would be A4.} and the orientation of the page to
landscape.
The language can be passed as an optional parameter, too (cf. \cref{sec:language}).
\subsection{Digression: packages}
\label{sec:packages}
\begin{minted}{latex}
\usepackage[<options>]{<packagename>}
\end{minted}
Packages provide additional commands and functionalities that we can use within our \LaTeX{} source code.
There are numerous packages for different use cases (e.\,g., typesetting formulas, lists, \textellipsis).
In order to use a package, it must be included within the preamble.
To do so, the above-mentioned command is used.
The most important \LaTeX{} packages can be found in the Comprehensive \TeX\ Archive Network, short: \acro{CTAN}.\footnote{Available at: \url{https://www.ctan.org/}}
You can also find documentation for the packages there.
\subsection{Encoding}
\begin{minted}{latex}
\usepackage[utf8]{inputenc}
\usepackage[t1]
\end{minted}
One use case for packages is specifying the encoding of our \LaTeX{} document.
The character encoding\footnote{cf. \url{https://en.wikipedia.org/wiki/Character_encoding}} determines the available character set.
The standard encoding in \LaTeX{} is \acro{ASCII}.\footnote{cf. \url{https://en.wikipedia.org/wiki/ASCII}}
It is an American character encoding and therefore does not contain German umlauts or most other special characters, which makes it unsuitable at least for non-english use cases.
Instead, \acro{UTF-8}\footnote{cf. \url{https://en.wikipedia.org/wiki/UTF-8}} can be used as a universal character encoding.
In \LaTeX{}, we need to specify two character encodings:
The input encoding (\mono{inputenc}), which refers to our source code, and the font encoding (\mono{fontenc}), which determines what the content of our \acro{PDF} document looks like.\footnote{Details on \mono{fontenc} can be found at: \url{https://tex.stackexchange.com/questions/108417/font-encoding-in-latex}}
\pkg{T1} is an encoding that tries to cover most European languages with a limited number of characters.
\subsection{Language}\label{sec:language}
\begin{minted}{latex}
\usepackage[ngerman]{babel}
\end{minted}
The package \pkg{babel} provides language-specific information (e.\,g., on hyphenation, special characters, changing fonts, translation of labels\footnote{cf. \cref{sec:references}} like \enquote{Chapter,} \enquote{Table of Contents,} or \enquote{Figure.}
The desired language can be passed as an optional parameter.
\pkg{ngerman}, for instance, is used for the new German spelling.
Some packages require that the language is already passed as an optional parameter in the \code{latex}{\textbackslash documentclass} command.
In this case, just leave out the optional parameter for the language within the \pkg{babel} inclusion command.
We can also use multiple languages in our document.
To do so, we pass the languages, separated by commas, as an optional parameter to the babel inclusion command.
Within our document, we can switch between languages with the \code{latex}{\textbackslash selectlanguage\{<language>\}} command.
Alternatively, foreign-language text can be declared by using the following command:
\begin{minted}{latex}
\foreignlanguage{<language>}{<text>}
\end{minted}
\section{Document environment}
The actual content of the \acro{PDF} document needs to be put between \code{latex}{\textbackslash begin\{document\}} and \code{latex}{\textbackslash end\{document\}}.
\subsection{Continuous text}
The easiest content that we can integrate into the document environment is continuous text.
We can write it directly into our source code.
Line breaks and multiple consecutive spaces are ignored by \LaTeX{}.
Blank lines create a new paragraph, that is indented by default.\footnote{The automatic indentation of new paragraphs can be prevented by using the command \code{latex}{\textbackslash noindent}.}
Manual line breaks can be enforced with two backslashes (\textbackslash\textbackslash).
This should be avoided, though.
\subsection{Comments}
Some characters are reserved for \LaTeX-specific commands, for instance, the percent sign.
Using a percent sign tells the \LaTeX{} compiler to ignore the rest of the line, so the text after the percent character will not appear in the generated \acro{PDF} document.
This is called a \emph{comment}\textit{,}
and it can be useful to take notes while working on a document without affecting the document itself.
There are a few more of these reserved characters, as we will see and learn to deal with in \cref{sec:more-special-characters}.
\subsection{Sections and chapters}
Continuous text can be structured by headings that divide the document into sections and chapters.
Needless to say, \LaTeX{} provides us with commands for that.
The commands that are depicted in \cref{lst:headlines} can be used with any document class.
\Example{lst:headlines}{basic-document-structure/headlines}{basic-document-structure/headlines_crop}{Heading Levels}
Some document classes provide additional commands. In a \pkg{report}, you get \code{late}{\textbackslash chapter\{Chapter\}}, and in a \pkg{book}, additionally \code{latex}{\textbackslash part\{Part\}}.
You can mark the command with an asterisk if you want to omit the numbering of a section and exclude it from the table of contents:\footnote{cf. \cref{sec:table-of-contents}}
\begin{minted}{latex}
\section*{This section is excluded from the table of contents}
\end{minted}
An alternative title for the table of contents can be declared as an optional parameter in square brackets between the command and the actual title:
\begin{minted}{latex}
\section[Title in the TOC]{Actual Chapter Title}
\end{minted}
\subsection{Front matter}
A simple front matter can be created by using the command \code{latex}{\textbackslash maketitle}.
The values to be inserted into the front matter must be specified within the preamble.
Multiple authors are joined by \code{latex}{\textbackslash and}.
If the date is not specified by the \code{latex}{\textbackslash date} command, the current date will be inserted by default.
The design of the front matter depends on the specified document class.
\Example{lst:titles}{basic-document-structure/titles}{basic-document-structure/titles_crop}{The front matter}
\subsection{Indices}\label{sec:table-of-contents}
The command \code{latex}{\textbackslash tableofcontents} generates an automatically numbered table of contents by making use of the above-mentioned commands for dividing our text into sections and chapters (this can bee seen in \cref{lst:main-file} on \cpageref{lst:main-file}).
The numbering style and depth, and many other options can, of course, be specified manually.\footnote{We recommend the following blogpost: \url{https://texblog.org/2011/09/09/10-ways-to-customize-tocloflot/}}
For \LaTeX{} to create our table of contents properly, the project has to be compiled twice.
Besides the table of contents, you can also generate a \code{latex}{\textbackslash listoffigures} (list of figures) and a \code{latex}{\textbackslash listoftables} (list of tables).
The captions of your figures and tables will appear within those indices.\footnote{cf. \cref{sec:graphics} (Graphics) and \cref{sec:tables} (Tables) for more information on captions}