This lesson is about the structure of a LaTeX file.

A LaTeX document

Every LaTeX document has the following structure:

\documentclass[a4paper]{article}
\begin{document}
\end{document}

In this, article can be replaced by a different document class. The part before \begin{document} is called the preamble. The preamble contains commands which influence the entire document. For example, packages are loaded here. The actual text of your document is between \begin{document} and \end{document}.

Document classes

A document class is a collection of settings corresponding to a specific type of document. The document class is set with

\documentclass[options]{class}

where options is a list of options seperated by commas. Later, we will look further into this.

Packages

Packages are extensions of LaTeX that add functionality. One can think of the possibility to add figures or additional commands for mathematical symbols. Modern TeX distributions (see Installing LaTeX) contain many packages, which can be used by putting

\usepackage[options]{packagename}

in the preamble, with packagename the name of the package and options a list of options. A package which is indispensible in Dutch documents is

\usepackage[dutch]{babel}

The babel package with the option dutch will make sure that LaTeX replaces standard titles such as Chapter and Table of contents by the Dutch equivalents Hoofdstuk and Inhoudsopgave respectively. The package also configures hyphenation so it is essential to use the right language option.

Top matter

The preamble is followed by the document environment, beginning with \begin{document} and ending with \end{document}. In the first lines of the document environment one gives information about the document itself, such as the name of the author, the title and the date. This is called top matter. With the command \maketitle LaTeX will typeset the topmatter following the style of the document class. An example of top matter is

\title{A new proof of an old theorem}
\author{Jane Doe}
\maketitle

A date can be inserted with \date{January 1st, 2017}. If you don’t use \date, LaTeX will typeset the current date. More than one author can be typeset with \and:

\author{Jane Doe \and John Doe}

Note that it is also correct to define the title, author, date, etc. using commands such as \title{} in the preamble. It is then only necessary to include the command \maketitle in the topmatter.

Exercise 1

  1. Writing as little code as possible, create a Dutch document with title “Een bewijs van de Riemannhypothese” with yourself as the author.
  2. Make sure LaTeX typesets the date in Dutch and not in English.

Important

We see that LaTeX determines the layout itself. This is an important concept in LaTeX! We determine the layout of the text by structuring it. By separating layout and content

  • We can focus on the content.
  • We obtain a consistent layout.
  • We (or a publisher) are able to change the layout easily.

Structuring text

In LaTeX we have groups, commands and environments to structure our document.

Groups

A group is a block of code surrounded by { and }. A group indicates that the code belongs together.

Commands

Examples of commands are \documentclass, \usepackage and \author. The name of a command is case sensitive. Some commands require one or more arguments, which are written between { and } after the command. An argument can be optional, which is indicated by [ and ] after the command. The general syntax is:

\commandname[option1,option2,...]{argument1}{argument2}...

Environments

An environment such as document is created by two commands:

\begin{name}
\end{name}

Inside an environment one can write LaTeX code. An environment defines a group automatically.

Exercise 2

In this assignment we will define our own command.

  1. Create a new LaTeX file using the amssymb package and put the following in the preamble:

    \newcommand{\R}{\mathbb{R}}
    
  2. Test your new command:

    The real line $\R{}$.
    
  3. What happens if you remove the empty group {}?

  4. We will now create a command with an argument. Put the following in the preamble:

    \newcommand{\Bb}[1]{\mathbb{#1}}
    
  5. Test it using this code:

    The rationals $\Bb{Q}$.
    
  6. With [1] we indicate that the command expects a single argument. We use this argument with #1. Try to make a command with two arguments.

Remember

  • What is the global structure of a LaTeX file?
  • What do \documentclass, \usepackage, \title, \author, \and, \date and \maketitle do?
  • What does the babel-package do?
  • Why do we separate layout and content?
  • What are groups, commands and environments?
  • How do you make your own command?