Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LaTeX Cookbook

You're reading from   LaTeX Cookbook Over 100 practical, ready-to-use LaTeX recipes for instant solutions

Arrow left icon
Product type Paperback
Published in Feb 2024
Publisher Packt
ISBN-13 9781835080320
Length 424 pages
Edition 2nd Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Stefan Kottwitz Stefan Kottwitz
Author Profile Icon Stefan Kottwitz
Stefan Kottwitz
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Chapter 1: Exploring Various Document Classes FREE CHAPTER 2. Chapter 2: Tuning the Text 3. Chapter 3: Adjusting Fonts 4. Chapter 4: Creating Tables 5. Chapter 5: Working with Images 6. Chapter 6: Creating Graphics 7. Chapter 7: Creating Beautiful Designs 8. Chapter 8: Producing Contents, Indexes, and Bibliographies 9. Chapter 9: Optimizing PDF Files 10. Chapter 10: Writing Advanced Mathematics 11. Chapter 11: Using LaTeX in Science and Technology 12. Chapter 12: Getting Support on the Internet 13. Chapter 13: Using Artificial Intelligence with LaTeX 14. Index 15. Other Books You May Enjoy

Writing a short text

While LaTeX is excellent for large documents, it’s 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 spacy. So, we will choose a document class that suits it best.

Class bundles cover commonly used document types. Every LaTeX installation contains a base bundle with standard classes. There is a class file for articles, one for books, one for reports, one for letters, and more. It is stable stuff; it has stayed the same for many years. It can be sufficient if you don’t care about the latest style. It would run even on a 10-year-old LaTeX installation.

In this recipe, we will use a class of the KOMA-Script bundle. This is a set of document classes and packages designed initially to replace the standard classes and provide more features. In contrast to the stable base bundle, KOMA-Script has been extensively developed in recent years. It has become feature-rich and has 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 scr prefix. 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, where you can fill in your own text later.

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 on the book’s page at https://www.packtpub.com and https://latex-cookbook.net:

  1. Create a .tex document in the editor of your choice. Start with a document class. We will use the scrartcl KOMA-Script class with A4 paper size, a base font size of 12 pt, and inter-paragraph space instead of default paragraph indentation:
    \documentclass[paper=a4,oneside,fontsize=11pt,
      parskip=full]{scrartcl}
  2. Begin the document:
    \begin{document}
  3. By running this command, you let LaTeX print a table of contents:
    \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 some text:
    \section{The first section}
    This first text will contain
  7. Add a bulleted list using an itemize environment. Each list item starts with an \item command. Using the \ref{label} command, we will already refer to labels 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 the 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 when we would like to refer to this section:
    \label{sec:maths}
  10. Continue with the text. We start using some math expressions in the text. We mark them by enclosing them in parentheses with a prefixing backslash as follows:
    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 math equation using the equation environment. Again, place a label:
    \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. Do it twice so that the references work. The first page of the output will be as follows:
Figure 1.1 – A document with sections, math, and referencing

Figure 1.1 – A document with sections, math, and referencing

How it works...

In the first line, we loaded the scrartcl document class. In square brackets, we set options for specifying an A4 paper size with the oneside option for one-sided printing and a font size of 11 pt. Finally, we chose to have a full line between paragraphs in the output to distinguish paragraphs easily by adding the parskip=full option.

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

Without further ado, we began the text with a table of contents.

While the \section command starts numbered sections, we can have an unnumbered section by the starred \section* version. However, we used the \addsec KOMA-Script command for the first unnumbered section. That’s because contrary to \section*, the \addsec command generates an entry in the table of contents.

The empty line in step 5 tells LaTeX to make a paragraph break. Note that a simple line break in the LaTeX code doesn’t cause a line break or paragraph break in the output.

As bulleted lists are an excellent way to present points clearly, we used an itemize environment in step 7. Environments start with a \begin command and are finished by an \end command.

Note

If you want a numbered list, use the enumerate environment.

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

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 characters.

In step 10, small formulas within text lines have been enclosed in \( ... \), which provides inline math mode. Dollar symbols, such as $ ... $, can be used instead, making typing easier. However, the parentheses clarify where the math mode starts and ends, which may be beneficial when many math expressions are scattered in the text, and the “dollar syntax” is old TeX syntax.

Why did we have to compile it twice? When you use the \label command, LaTeX writes that position to the .aux file. In the next compiler run, the \ref command can read this and put the correct reference into the text.

For further information on math typesetting, refer to Chapter 10, Writing Advanced Mathematics, specifically to the Fine-tuning a formula recipe.

See also

The part of the document before \begin{document} is called the preamble. It contains global settings. Adding a few lines to our document preamble can improve and modify our document’s general appearance. Chapter 2, Tuning the Text, starts with beneficial additions to the preamble that are also useful with small documents.

In Chapter 3, Adjusting Fonts, you can find recipes for changing an entire document’s fonts or specific elements.

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

You have been reading a chapter from
LaTeX Cookbook - Second Edition
Published in: Feb 2024
Publisher: Packt
ISBN-13: 9781835080320
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime