Book Image

LaTeX Cookbook - Second Edition

By : Stefan Kottwitz
Book Image

LaTeX Cookbook - Second Edition

By: Stefan Kottwitz

Overview of this book

The second edition of LaTeX Cookbook offers improved and additional examples especially for users in science and academia, with a focus on new packages for creating graphics with LaTeX. This edition also features an additional chapter on ChatGPT use to improve content, streamline code, and automate tasks, thereby saving time. This book is a practical guide to utilizing the capabilities of modern document classes and exploring the functionalities of the newest LaTeX packages. Starting with familiar document types like articles, books, letters, posters, leaflets, and presentations, it contains detailed tutorials for refining text design, adjusting fonts, managing images, creating tables, and optimizing PDFs. It also covers elements such as the bibliography, glossary, and index. You’ll learn to create graphics directly within LaTeX, including diagrams and plots, and explore LaTeX’s application across various fields like mathematics, physics, chemistry, and computer science. The book’s website offers online compilable code, an example gallery, and supplementary information related to the book, including the author’s LaTeX forum, where you can get personal support. By the end of this book, you’ll have the skills to optimize productivity through practical demonstrations of effective LaTeX usage in diverse scenarios.
Table of Contents (16 chapters)

Writing a letter

Letters have a specific structure. Commonly, they have an addressee field at a fixed position, which should be visible in the envelope window. It also should show a back address of yourself as the sender. An opening text and a closing phrase are usual elements; you may add fold marks and enclosures.

How to do it...

We will use a KOMA-Script class specifically designed for letters named scrlttr2. Follow the following steps:

  1. Use the scrlttr2 class, activate the address field and fold marks via an option, and align the sender’s address to the right:
    \documentclass[addrfield=true, foldmarks=true,
      fromalign=right]{scrlttr2}
  2. Provide your name and your address using the \setkomavar command:
    \setkomavar{fromname}{Thomas Smith}
    \setkomavar{fromaddress}{123 Blvd \\ City, CC 12345}
  3. Write a date, either \today for today or any date as text:
    \date{\today}
  4. Begin the document:
    \begin{document}
  5. Open a letter environment with the recipient’s address as an argument:
    \begin{letter}{Agency \\ 5th Avenue \\
                   Capital City, CC 12345}
  6. Start with an opening, and let your letter text follow:
    \opening{Dear Sir or Madam,}
    the actual content of the letter follows.
  7. End with closing words:
    \closing{Yours sincerely}
  8. End the letter environment and the document:
    \end{letter}
    \end{document}
  9. Compile the document. Here is the upper part of the output:
Figure 1.6 – A letter template

Figure 1.6 – A letter template

That was pretty easy! You got fully fledged formal letter addressing information, envelope window support, today’s date, phrases, signature, and even fold marks.

Now, you can enter real addresses and actual letter text.

How it works...

When loading the scrlttr2 letter class, we activated the address field, switched on fold marks, and set the options for right aligning the sender’s address.

The scrlttr2 class is quite different from others, so it has a unique interface. Using the \setkomavar command, we set the content of class variables, similar to \renewcommand. Here, we put names and addresses. The KOMA-Script manual explains all available variables. As mentioned in the Developing a thesis recipe, you can open it by executing texdoc koma-script at Command Prompt or online at https://texdoc.org/pkg/koma-script.

We used a letter environment for the actual content, including the opening and closing phrases. The address is a mandatory argument for that environment. You can have several letter environments in a single document.

There’s more...

To improve input and hyphenation and change the font, look at the first recipes in Chapter 2, Tuning the Text.

Let’s take a look at some letter-specific options.

Separating paragraphs

Instead of indenting the beginning of paragraphs, you can visualize a paragraph break with an empty line. For this, add the parskip=full option to the comma-separated list of class options at the beginning.

Use parskip=half for less space.

Changing the signature

If you would like to use a signature different from your specified name for the address, you can modify the corresponding variable content in the preamble:

\setkomavar{signature}{Thomas}

It would be indented. You can get it left aligned by specifying the following code:

\renewcommand{\raggedsignature}{\raggedright}

The code just shown also belongs to the preamble.

Adding enclosures

If you would like to add enclosures to your letter, it’s common to mention them. You can do this by inserting an \encl command right before \end{letter}:

\encl{Curriculum vitae, certificates}

You can change the default encl: option if you like by modifying the corresponding variable before calling the \encl command:

\setkomavar*{enclseparator}{Attached}

We used the \setkomavar* starred version, which modifies the description of a variable instead of its content, which actually is : – that is, a colon followed by a space.