Reader small image

You're reading from  LaTeX Beginner's Guide - Second Edition

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801078658
Edition2nd Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Chapter 3: Designing Pages

After the previous chapter, formatting text should be easy for us. So, let's turn to whole pages!

In this chapter, we will learn how to structure a document in chapters and sections, and how to modify the general appearance such as the margins, the page orientation, headers, and footers. This gives you control over the whole document design.

We will learn about the following:

  • Creating a book with chapters
  • Defining the margins
  • Using class options
  • Designing headers and footers
  • Using footnotes
  • Breaking pages
  • Enlarging a page
  • Changing the line spacing
  • Creating a table of contents

By working on this, we shall gain a deeper insight into classes and packages.

We will start by writing an example document spanning over several pages. This will be our test object for our modifications.

Technical requirements

You need either a LaTeX installation on your computer, or you can use Overleaf. You can edit and compile all examples online on the book's web page: https://latexguide.org/chapter-03.

The code is also available on GitHub: https://github.com/PacktPublishing/LaTeX-Beginner-s-Guide-2nd-Edition-/tree/main/Chapter_03_-_Designing_Pages.

In this chapter, we will use the following LaTeX packages: babel, blindtext, fancyhdr, geometry, and setspace. If you don't work online, make sure you have them installed if you don't have a full LaTeX installation. We'll also talk about the packages bigfoot, endnotes, footmisc, lipsum, manyfoot, multicol, safefnmark, and scrlayer-scrpage, which you may optionally use.

You can find package information on CTAN at https://ctan.org/pkg/<packagename>, and documentation at https://texdoc.org/pkg/<packagename>.

Creating a book with chapters

We will start to write a book. First, we shall choose a class, and use some filler text to work out the page layout. Let's see how to do it:

  1. Create a new document, and enter the following lines as our preamble:
    \documentclass[a4paper,12pt]{book}
    \usepackage[english]{babel}
    \usepackage{blindtext}
  2. Proceed with writing the document body containing a chapter heading, section and subsection headings, and some filler text:
    \begin{document}
    \chapter{Exploring the page layout}
    In this chapter we will study the layout of pages.
    \section{Some filler text}
    \blindtext
    \section{A lot more filler text}
    More dummy text will follow.
    \subsection{Plenty of filler text}
    \blindtext[10]
    \end{document}
  3. Compile it by clicking on Typeset. Look at the first page:

Figure 3.1 – An example page

We have chosen the document class book. As the name implies, this class is suitable for book-like documents. Books are commonly two-sided...

Defining the margins

A publisher or a supervisor may request that you follow their specifications for a document. Besides font size, interline spacing, and other style issues, there may be specifications for the margins. In this case, you would need to override LaTeX's recommendations, specifying the margins precisely.

There's a package that fulfills these demands, called geometry. We shall load the geometry package and state the exact width and height of all margins:

  1. Extend the preamble of the previous example in this chapter with this command:
    \usepackage[a4paper, inner=1.5cm, outer=3cm, top=2cm, bottom=3cm, bindingoffset=0.5cm]{geometry}
  2. Click on Typeset to compile the code and examine the adjusted margins.

The geometry package takes care of our layout regarding the paper size, margins, and other dimensions. We chose the A4 paper size, an outer margin of 3 cm, and an inner margin of just 1.5 cm.

Inner versus outer margins

When a two-sided book...

Using class options

We already know that a document class is the basis of our document. It provides commands and environments extending the LaTeX standard features. Though the class provides a default style, it's customizable with document class options.

We shall change the orientation of our first example to landscape. We would also like to typeset our text in two columns:

  1. Add the options landscape and twocolumn to the \documentclass statement of our example, as follows:
    \documentclass[a4paper,12pt,landscape,twocolumn]{book}
  2. Load the geometry package:
    \usepackage{geometry}
  3. Click on Typeset to compile, and see how the layout has changed:

Figure 3.2 – A landscape two-column page layout

By using the landscape option, we switched the page orientation from portrait to landscape. By using the twocolumn option, we caused the body text to be divided into two columns.

We loaded the geometry package to get the proper PDF page size...

Designing headers and footers

When we tested the first version of our example, you might have noticed that except for the page where the chapter started, all pages showed the page number, chapter title, and section title in their header. So, in our two-sided layout, on page 2, which is a left-hand page header, the page number is in the outer margin, here on the left side:

Figure 3.3 – The header of page 2

And this is how our right-hand page header on page 3 looks, with the page number in the outer margin, which is on the right side now:

Figure 3.4 – The header of page 3

In a one-sided layout, there would not be such a difference in the header layout. The headers in a one-sided layout are as in Figure 3.4. By default, heading text is on the left side, and the page number is on the right side.

Though these standard headers are already quite useful, we shall see how to customize them to meet our individual requirements...

Using footnotes

As briefly mentioned in Chapter 2, Formatting Text and Creating Macros, LaTeX provides a command to typeset footnotes. Let's see it in action.

Let's go back to the very first example of this chapter. We shall insert one footnote in the body text and one in a section heading:

  1. Modify the example inserting a footnote, as shown in the highlighted line:
    \documentclass[a4paper,12pt]{book}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \begin{document}
    \chapter{Exploring the page layout}
    In this chapter we will study the layout of pages.
    \section{Some filler text}
    \blindtext
    \section{A lot more filler text}
    More dummy text\footnote{serving as a placeholder}
    will follow.
    \subsection{Plenty of filler text}
    \blindtext[10]
    \end{document}
  2. Compile the code to see how the footnote looks in print:

Figure 3.7 – Text with a footnote

The command \footnote{text} placed a superscripted number at the current position.

Furthermore...

Breaking pages

As you've seen in our example, LaTeX itself took care of the page breaking. There might be occasions where we'd like to insert a page break ourselves before LaTeX does. LaTeX offers several commands to do so, with vertical balance or without.

We will now go back to the first version of our example and we shall manually insert a page break right before subsection 1.2.1:

  1. Insert the highlighted line into our example, which contains the \pagebreak command:
    \documentclass[a4paper,12pt]{book}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \begin{document}
    \chapter{Exploring the page layout}
    In this chapter we will study the layout of pages.
    \section{Some filler text}
    \blindtext
    \section{A lot more filler text}
    More dummy text will follow.
    \pagebreak
    \subsection{Plenty of filler text}
    \blindtext[10]
    \end{document}
  2. Compile the code and take a look at the result:

    Figure 3.9 – A stretched page

  3. Replace \pagebreak with \newpage.
  4. Compile...

Enlarging a page

There may be occasions where we want to put a little more text onto a page, even if the text will be squeezed a bit or the text height increases. There's a command that will help us out: \enlargethispage.

We shall modify our example a bit. This time, we will try to avoid a nearly empty page by squeezing the text on the preceding page:

  1. Remove the \newpage command from our example and switch to 11pt base font. This time, use less filler text in the subsection:
    \documentclass[a4paper,11pt]{book}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage[a4paper, inner=1.5cm, outer=3cm, top=2cm,
    bottom=3cm, bindingoffset=1cm]{geometry}
    \begin{document}
    \chapter{Exploring the page layout}
    In this chapter we will study the layout of pages.
    \section{Some filler text}
    \blindtext
    \section{A lot more filler text}
    More dummy text will follow.
    \subsection{Plenty of filler text}
    \blindtext[3]
    \end{document}
  2. Compile, and the result will consist of two pages...

Changing the line spacing

Without some vertical space between the lines, the readability of our text could suffer. Adding such space would help lead the eye along the line. Though LaTeX already takes care of good readability by choosing meaningful interline spacing, publishers might require different spacing.

We shall modify the very first example of this chapter by adding half of a line height to the line spacing:

  1. Extend the preamble of our example with this command:
    \usepackage[onehalfspacing]{setspace}
  2. Compile the code to see the change:

Figure 3.14 – Additional interline spacing

We loaded the setspace package to adjust the line spacing. We provided the option onehalfspacing, which increases the spacing by half of a line height for the whole document.

The setspace package understands three options:

  • singlespacing is the default. No additional space will be inserted. The text will be typeset with LaTeX's default interline...

Creating a table of contents

A book commonly begins with a table of contents, so let's create one based on our numbered headings:

  1. In our previous document, let's remove the options landscape and twocolumn.
  2. Remove the setspace package, that is, delete this line:
    \usepackage[onehalfspacing]{setspace} 
  3. Add the command \tableofcontents right after \begin{document}.

    Our code shall now look like this:

    \documentclass[a4paper,12pt]{book}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage[a4paper, inner=1.5cm, outer=3cm, top=2cm,
    bottom=3cm, bindingoffset=1cm]{geometry}
    \begin{document}
    \tableofcontents
    \chapter{Exploring the page layout}
    In this chapter we will study the layout of pages.
    \section{Some filler text}
    \blindtext
    \section{A lot more filler text}
    More dummy text will follow.
    \subsection{Plenty of filler text}
    \blindtext[10]
    \end{document}
  4. Compile the code twice. Afterward, the first page of your output will contain this table:
...

Summary

In this chapter, we have worked out how to design the overall layout of a document.

Specifically, we learned about choosing page dimensions, margins, and orientation. We know how to switch to a two-column layout and how to adjust line spacing. Furthermore, we are now able to customize headers and footers, add footnotes, and add a table of contents to our document.

Furthermore, we covered some general topics, such as changing document properties by choosing document class options and package options and by redefining existing commands.

Now it's time to deal with further text structures. In the next chapter, we shall learn how to create lists to present text in an easy-to-read way.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021Publisher: PacktISBN-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.
undefined
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 ₹800/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz