Book Image

LaTeX Cookbook

By : Stefan Kottwitz
Book Image

LaTeX Cookbook

By: Stefan Kottwitz

Overview of this book

LaTeX is a high-quality typesetting software and is very popular, especially among scientists. Its programming language gives you full control over every aspect of your documents, no matter how complex they are. LaTeX's huge amount of customizable templates and supporting packages cover most aspects of writing with embedded typographic expertise. With this book you will learn to leverage the capabilities of the latest document classes and explore the functionalities of the newest packages. The book starts with examples of common document types. It provides you with samples for tuning text design, using fonts, embedding images, and creating legible tables. Common document parts such as the bibliography, glossary, and index are covered, with LaTeX's modern approach.You will learn how to create excellent graphics directly within LaTeX, including diagrams and plots quickly and easily. Finally, you will discover how to use the new engines XeTeX and LuaTeX for advanced programming and calculating with LaTeX. The example-driven approach of this book is sure to increase your productivity.
Table of Contents (19 chapters)
LaTeX Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing a short text


While LaTeX is great for big documents, it's just as useful for smaller ones, and you get all the features to work with. Writing down homework or producing a seminar handout, for example, doesn't need book-like chapters, and the layout would not be very spacious. So we will choose a document class that suits the task at hand best.

There are class bundles that cover commonly used document types. Every LaTeX installation contains the base bundle with standard classes. There are class files for articles, books, reports, letters, and more. It is stable stuff; it has not really been changed for many years. If you don't care about the latest style, this can be sufficient. It would even run on a ten-year-old LaTeX installation.

In this recipe, we will use a class of the KOMA-Script bundle. This is a set of classes that were originally designed with the aim of replacing the standard classes and providing more features. In contrast to the rather static base bundle, KOMA-Script has been extensively developed during recent years. It became feature-rich and got an excellent user interface. Parts of its functionality are provided in packages that can be used together with other classes as well. You can identify KOMA-Script classes and packages by the prefix scr. This prefix stands for script, which was the initial name of this bundle.

How to do it...

We will start with a complete small document, already using various features. This can be your template, into which you can add your own text later on.

While we go through the document step by step, you may open the complete code directly with your editor, so you don't need to type it. It is contained in the code bundle available at the book's page https://www.packtpub.com and at the book's page http://latex-cookbook.net. Perform the following steps to create a small document:

  1. Start with a document class. We will use the KOMA-Script class scrartcl, with A4 paper size, a base font size of 12 pt, and interparagraph spacing instead of the default paragraph indentation:

    \documentclass[paper=a4,oneside,fontsize=12pt,
      parskip=full]{scrartcl}
  2. Begin the document:

    \begin{document}
  3. Let LaTeX print a table of contents using this command:

    \tableofcontents
  4. Start a section without numbering:

    \addsec{Introduction}
  5. Add some text:

    This document will be our starting point for simple documents. It is suitable for a single page or up to a couple of dozen pages.
    The text will be divided into sections.
  6. Start an automatically numbered section with text:

    \section{The first section}
    This first text will contain
  7. Add a bulleted list using an itemize environment. Each list item starts with \item. Using \ref{label}, we will refer to labels, which we will create later:

    \begin{itemize}
      \item a table of contents,
      \item a bulleted list,
      \item headings and some text and math in section,
      \item referencing such as to section \ref{sec:maths} and
            equation (\ref{eq:integral}).
    \end{itemize}
  8. Continue with text and start another numbered section:

    We can use this document as a template for filling in our own content.
    \section{Some maths}
  9. Set a label so that we can refer to this point whenever we want to refer to this section:

    \label{sec:maths}
  10. Continue with the text. We will start using some mathematical expressions in the text. We mark this by enclosing them in parentheses with a prefixing backslash, that is, \( … \).

    When we write a scientific or technical document, we
    usually include math formulas. To get a brief glimpse
    of the look of maths, we will look at an integral
    approximation of a function \( f(x) \) as a sum with
    weights \( w_i \):
  11. Write a mathematical equation using the equation environment. Again, place a label by using the following commands:

    \begin{equation}
    \label{eq:integral}
      \int_a^b f(x)\,\mathrm{d}x \approx (b-a)
      \sum_{i=0}^n w_i f(x_i)
    \end{equation}
  12. End the document:

    \end{document}
  13. Compile the document. The first page of the output will look like this:

How it works...

In the first line, we loaded the document class scrartcl. In square brackets, we set options for specifying an A4 paper size with one-sided printing and a font size of 12 pt. Finally, we chose to have a full line between paragraphs in the output so that we can easily distinguish paragraphs.

Note

The default setting is no space between paragraphs and a small indentation at the beginning of each paragraph. Uncheck the parskip option to see it. We chose a paragraph skip because many people are used to it when working with e-mails, while indentation costs line space, which is a precious resource on small electronic devices.

Without further ado, we began the text with a table of contents. While numbered sections are started by the \section command, we can start unnumbered sections by using the starred version \section*. However, we used the KOMA-Script command \addsec for the first unnumbered section. That's because, in contrast with \section*, the \addsec command generates an entry in the table of contents.

An empty line tells LaTeX to make a paragraph break.

As bulleted lists are a good way to clearly present points, we used an itemize environment for this. All environments start with the \begin command and are ended with the \end command.

Tip

If you would like to have a numbered list, use the enumerate environment.

An equation environment has been used to display a formula that is automatically numbered. We used the \label command to set an invisible anchor mark so we could refer to it using its label name by the \ref command and get the equation number in the output.

Tip

Choosing label identifiers:

It is a good practice to use prefixes to identify kinds of labels, such as eq:name for equations, fig:name for figures, tab:name for tables, and so on. Avoid special characters in names, such as accented alphabets.

Small formulas within text lines have been enclosed in \( ... \), which provides inline math mode. Dollar symbols, $ ... $, can be used instead of \( ... \), which makes typing easier. However, the use of parentheses makes it easier to understand where the math mode starts and where it ends, which may be beneficial when many math expressions are scattered throughout the text.

For further information on math typesetting, refer to Chapter 10, Advanced Mathematics, specifically to the recipe Finetuning a formula.

See also

The part of the document before the \begin{document} command is called the preamble. It contains global settings. By adding a few lines to our document preamble, we can improve our document and modify the general appearance. Chapter 2, Tuning the Text, starts with additions that are beneficial for small documents as well. They enable the direct input of accented characters and unicode symbols, and they improve justification and hyphenation.

In Chapter 3, Adjusting Fonts, you can find recipes for changing the fonts of either the whole document or of certain elements.

For further customization tasks, such as modifying page layout, and adding a title page, refer to the recipe Designing a book in the current chapter. We will take a look at such settings on the occasion of a book example.