Book Image

LaTeX Beginner's Guide - Second Edition

By : Stefan Kottwitz
4 (1)
Book Image

LaTeX Beginner's Guide - Second Edition

4 (1)
By: Stefan Kottwitz

Overview of this book

LaTeX is high-quality open source typesetting software that produces professional prints and PDF files. It's a powerful and complex tool with a multitude of features, so getting started can be intimidating. However, once you become comfortable with LaTeX, its capabilities far outweigh any initial challenges, and this book will help you with just that! The LaTeX Beginner's Guide will make getting started with LaTeX easy. If you are writing mathematical, scientific, or business papers, or have a thesis to write, this is the perfect book for you. With the help of fully explained examples, this book offers a practical introduction to LaTeX with plenty of step-by-step examples that will help you achieve professional-level results in no time. You'll learn to typeset documents containing tables, figures, formulas, and common book elements such as bibliographies, glossaries, and indexes, and go on to manage complex documents and use modern PDF features. You'll also get to grips with using macros and styles to maintain a consistent document structure while saving typing work. By the end of this LaTeX book, you'll have learned how to fine-tune text and page layout, create professional-looking tables, include figures, present complex mathematical formulas, manage complex documents, and benefit from modern PDF features.
Table of Contents (16 chapters)

Working with logical formatting

Within a LaTeX document, we should not apply physical formatting, for example, making words bold or italic or in a different size. Instead, we should use logical formatting, such as declaring a title and the author and giving a section header. The actual formatting, such as printing the title in big letters and making a section heading bold, is done by LaTeX.

Physical formatting in this book

In some examples later in this chapter, we will use physical formatting commands, such as making words bold or italic. However, that's for practicing font commands. The goal of this chapter is to define our own logical commands with the help of font commands.

In a good LaTeX document, physical formatting is only used within the definition of logical formatting commands. If we need some format style, such as for keywords, we will define a suitable logical command in the document preamble. In the document body text, we should only use the logical formatting commands. This gives us consistent formatting throughout the text, and whenever we change our mind on formatting details, we can modify the logical commands in the preamble. We will go through this in the next sections.

First, to understand the typical document structure, let's start with a short, illustrative example.

Creating a document with a title and heading

We will create a short example with some basic formatting. It shall include a title, the author's name, the date, a heading, and some regular text:

  1. Type the following code into your editor to start a small document:
    \documentclass[a4paper,11pt]{article}
  2. Specify the title, author, and date:
    \title{Example 2}
    \author{My name}
    \date{May 5, 2021}
  3. Begin the document:
    \begin{document}
  4. Let LaTeX print the full title, which will include the author and date:
    \maketitle
  5. Make a section heading and add some text:
    \section{What's this?}
    This is our second document. It contains a title and a section with text.
    \end{document}
  6. Save the document by clicking the Save button (or press Ctrl + S). Give it a name, such as example2.tex.
  7. Compile the document by clicking the Typeset button (or press Ctrl + T); this translates your code into a PDF file.
  8. View the output:
Figure 2.1 – Text with a heading

Figure 2.1 – Text with a heading

The TeXworks editor immediately shows the PDF preview after you press the Typeset button. At the same time, a PDF file is also created. In this case, it's called example2.pdf and it's in the same folder as your original code file, example2.tex.

In the first chapter, we talked about logical formatting; let's look at this example from that point of view. We told LaTeX the following:

  • Our document is of the article type. It will be printed on A4 paper using a size of 11 points for the base font.
  • The title of the document is Example 2.
  • It shows the author's name.
  • The document was written on May 5, 2021.

Concerning the content of the document, we stated the following:

  • It begins with a title.
  • The first section includes the heading What's this?
  • Following the heading is the text This is our second document. It contains a title and a section with text..

Note we did not choose the font size of the title or heading, and neither did we make something bold or centered. Such formatting is done by LaTeX but nevertheless, you're free to tell LaTeX how it actually should look.

Auto-saving

Once we have saved a document, we do not need to press the Save button again. TeXworks automatically saves the document when we click the Typeset button.

Exploring the document structure

Let's look at the details of the example we just created. A LaTeX document doesn't stand alone—commonly, the document is based on a versatile template. Such a fundamental template is called a class. It provides customizable features, usually built for a certain purpose. There are classes for books, journal articles, letters, presentations, posters, and many more; hundreds of reliable classes can be found in internet archives, but also on your computer after you've installed TeX Live. In our example, we have chosen the article class, a standard LaTeX class suitable for smaller documents.

The first line starts with \documentclass. This word begins with a backslash; such a word is called a command or a macro. We already used commands to specify the class and to state document properties in our first example in this chapter: \title, \author, and \date. These commands store the properties; they don't print something.

This first part of the document is called the preamble of the document. This is where we choose the class, specify properties, and in general make document-wide definitions.

\begin{document} marks the end of the preamble and the beginning of the actual document. \end{document} marks the end of the document. Everything that follows would be ignored by LaTeX. Generally, such a piece of code that's framed by a \begin and \end command pair is called an environment.

In the actual document, we've used the \maketitle command, which prints the title, author, and date in a nicely formatted manner. With the \section command, we produced a heading, bigger and bolder than normal text. Then, we let some text follow. What we wrote after the preamble, in the document environment, will be printed out. However, the preamble itself will never produce any output.

Now that you've seen what commands look like, let's look at command syntax in detail.

Understanding LaTeX commands

LaTeX commands begin with a backslash, followed by big or small letters, and are usually named in a descriptive way. There are exceptions: you will see some commands consisting of a backslash and just one special character.

Commands can have parameters, that is, options that determine in which way the command does its work. The values that we hand over as parameters are called arguments. They are given in curly braces or square brackets, as we will explain now.

So, calling a command can look like this:

\command

Or, it can look like this:

\command{argument}

Or, it can look like this:

\command[optional argument]{argument}

There could be several arguments, each of them in braces or brackets. Arguments in curly braces are mandatory. If a command is defined to require an argument, one has to be given. For example, calling \documentclass would be futile if we haven't stated a class name.

Arguments in square brackets are optional; they may be given but it's not a must.If no optional argument is provided, the command will use a default one.For instance, in the first example in Chapter 1, Getting Started with LaTeX, we wrote \documentclass{article}. This document was typeset with a base font size of 10 points because this is the class's default base font size. In the second document, we wrote \documentclass[a4paper,11pt]{article}; here, we replaced the default values with the given values, so now the document will be adjusted for A4 paper using a base font size of 11 points.

Commands, macros, and declarations

Most LaTeX commands, including those we define ourselves, consist of other commands. That's why LaTeX commands are also called macros, and the terms macro and command are used interchangeably. A command or macro that doesn't print something but just changes current settings, such as the font shape or text alignment, is also called a declaration.

Now, let's look at the syntax of environments.

Understanding LaTeX environments

LaTeX environments start with \begin and end with \end. Both commands require the name of the environment as their argument.

Simple environments look like this:

\begin{name}
    …
\end{name}

Such environments can be used for each declaration called \name.

Like commands, environments may have arguments. Exactly like in the case of commands, mandatory arguments are written in curly braces and optional arguments in square brackets. So, you will encounter an environment like this:

\begin{name}{argument}
    …
\end{name}

You will also encounter environments like this:

\begin{name}[optional argument]{argument}
    …
\end{name}

Environments are like declarations with a built-in scope. With \begin, the environment introduces a change in layout, font, or other properties. There must be an \end command, where this change will be canceled. The effect of the environment name is delimited to the piece of code between \begin{name} and \end{name}.

Furthermore, the effect of all local declarations used inside an environment will end together with the surrounding environment.

Now that we know the syntax of LaTeX commands and environments, let's see how LaTeX treats what we type.