Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LaTeX Beginner's Guide

You're reading from   LaTeX Beginner's Guide Create visually appealing texts, articles, and books for business and science using LaTeX

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801078658
Length 354 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: Getting Started with LaTeX 2. Chapter 2: Formatting Text and Creating Macros FREE CHAPTER 3. Chapter 3: Designing Pages 4. Chapter 4: Creating Lists 5. Chapter 5: Including Images 6. Chapter 6: Creating Tables 7. Chapter 7: Using Cross-References 8. Chapter 8: Listing Contents and References 9. Chapter 9: Writing Math Formulas 10. Chapter 10: Using Fonts 11. Chapter 11: Developing Large Documents 12. Chapter 12: Enhancing Your Documents Further 13. Chapter 13: Troubleshooting 14. Chapter 14: Using Online Resources 15. Other Books You May Enjoy

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.

You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801078658
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
Banner background image