shares

## Introduction

Have you ever struggled to configure a book layout with LaTeX—setting the paper size, book page size (trim size) and margins? It can be tricky, even though there are many packages to assist with this task—including the excellent geometry package. (La)TeX uses nearly a dozen values to fully specify the page layout for publications that have different margins for left- and right-hand pages:

• \hoffset, \voffset, \oddsidemargin, \topmargin, \headheight, \headsep, \textheight, \textwidth, \marginparsep, \marginparwidth, \footskip

Typically, these parameters are set through commands provided by packages or templates—such as those distributed by book publishers. However, at times you might want to understand what is going on “behind the scenes”—perhaps you need a greater degree of control for a particular type of document you want to create.

## What is our objective?

The theme of this article, and the purpose of the resources we have provided, is based on the following objectives:

1. We want to implement a layout specification provided by, for example, a book designer or publisher who does not use or support LaTeX—or perhaps we need to follow a specification provided by a print-on-demand company.
2. We want to use the traditional book measurements (paper size, trim size and page margins) contained within the designer’s specification and convert them into parameters and values that LaTeX understands and can use to replicate the designer’s layout.
3. We want our book page to be horizontally and vertically centred on the paper size we have chosen— i.e., centred within the area of the PDF pages created by LaTeX. We also want to choose an arbitrary paper size and not be restricted to a few standard options (A4, US Letter and so forth).

In this post we explore the relationship between LaTeX’s model of the page and the conventional model used within the world of print and design. Using a set of Google Slides, which contain detailed page-layout illustrations, we show, step-by-step, how to formulate some very simple equations which provide a link between LaTeX’s layout model and a typical specification that might be produced by a book designer or print-on-demand company. Using those equations you can, if you wish, directly calculate values for LaTeX’s parameters to implement your preferred book layout. Alternatively, you can use our new LuaLaTeX-based template which uses LaTeX’s book.cls and offers an implementation of those equations to provide visual page guides and rulers for previewing your book’s text area and margins. By using the Overleaf template you can quickly and easily preview various book sizes to help decide which one you prefer—and then continue to explore various settings for the margins. The Overleaf template also displays the text boundary defined by (non-zero) settings for \marginparsep and \marginparwidth.

### Quick reminder: How do you set the paper size?

LuaTeX, XeTeX and pdfTeX provide two commands that let you control the “paper size” of the PDF document they produce:

• \pdfpagewidth
• \pdfpageheight

Knowing that these commands represent the size of our paper we can now draw a diagram to show what we are trying to achieve:

Note that ΔX and ΔY are the values needed to centre the book page on the paper area—they are discussed in the Google Slides. From the diagram above you can see:

• ΔX=½(\pdfpagewidth − book-page-width)
• ΔY=½(\pdfpageheight − book-page-height)

The above diagram is also included in the Google Slides and is used as the starting point for arriving at the page-layout equations.

#### An aside: LuaTeX

LuaTeX is an extremely powerful and versatile TeX engine and was selected to implement the Overleaf template. At the time of writing, Overleaf uses (via TeX Live) LuaTeX version 0.95 which includes some changes made to the core engine earlier in its release cycle. Expert readers will know those changes affected commands which control the generation of PDF files. Strictly speaking, LuaTeX no longer directly supports \pdfpagewidth or \pdfpageheight but now uses two new primitives called \pagewidth and \pageheight. Backwards compatibility is achieved through \let\pdfpagewidth\pagewidth and \let\pdfpageheight\pageheight. Interested readers can find more details in LaTeX News, Issue 24, February 2016 and, of course, the official LuaTeX documentation.

## A range of resources in this post

Here at Overleaf we like to try out new ideas so, in this post, we have included a range of resources which we hope will be useful:

• Two sets of Google Slides: These slide sets contain identical content but one has animations and the other does not (some people prefer to view slides that don’t contain any animation). You can also download them in PDF format. The slides provide a very brief introduction to the basics of book page layout and explore the relationship between LaTeX’s page model and the one used in conventional print and design. The last few slides show how to arrive at simple equations which can be used to use convert a set of traditional measurements (page width/height and margins) into equivalent LaTeX parameter values to reproduce your book’s layout in LaTeX.
• A LaTeX template: We’re delighted to release a new template to accompany this blog post. The template, which uses LaTeX’s book.cls, is implemented with the LuaTeX engine and overlays the book page with visual rulers and guides to help you achieve the layout you are working towards. If there is sufficient interest we will release new versions as time permits.
• Page-layout diagrams: The Google Slides contain detailed page-layout diagrams which are used to formulate some simple equations to calculate various LaTeX parameters. If you would like to study those illustrations in more detail we’ve provided high resolution (vector) PDF versions for you to download.

The readership of this article is likely to have a spectrum of experience in using LaTeX so we felt it was preferable to provide a range of resources for readers to choose from—based on your personal experience, interests or preferences. We hope these resources are useful and meet the needs and interests of Overleaf’s LaTeX community.

There’s no defined order in which to use these resources but our suggestion would be as follows:

3. experiment with the LaTeX template.

We appreciate that LuaTeX is, at present, not as widely used as other TeX engines (pdfTeX and XeTeX) so we designed the Overleaf template to write out a text file (mypagesetup.tex) each time you successfully typeset a document. mypagesetup.tex contains all the LaTeX parameter settings required to produce your layout. You can download the Overleaf template project as a ZIP file, extract mypagesetup.tex and include it in another LaTeX project to reproduce the same page layout using pdfLaTeX or XeLaTeX—we provide examples for pdfLaTeX/XeLaTeX and LuaLaTeX.

The final slide (shown below) summarizes the equations formulated during the presentation.

For the convenience of readers we provide three options for viewing the slides:

1. slides with animations;
2. slides without animations;
3. slides in PDF format for offline viewing or printing.

If you would like to explore the page-layout diagrams in more detail, there is a link at the end of this article to download a PDF file which contains a double-page spread (facing pages) version of the diagrams contained in the Google Slides.

#### Step 3: Explore the LaTeX template

The template is extremely easy to use and has six commands you need to become familiar with (detailed below). To use the template you do not need to have viewed the slides or examined the page-layout diagrams. Readers who want, or need, a more in-depth understanding of the page-layout equations can view the slides, others can proceed straight to the template. You don’t need to understand the equations or be concerned with their details because they are implemented in the template code.

## The Overleaf template: What does it do?

In summary, it offers the following:

• the ability to define your book layout using values for paper width/height, book page width/height and the four book-page margins (top, bottom, inner and outer);
• it uses your measurements to calculate corresponding values of the LaTeX parameters needed to achieve your desired layout;
• as noted above, all relevant LaTeX parameter settings are saved into a file called mypagesetup.tex that you can obtain by downloading the template-based project as a ZIP file;
• it provides visual feedback to preview your layout by drawing rulers and guides on the typeset page. The rulers provide tick marks using a mm scale so that you can very quickly check your book’s page width/height, text width/height and the various margins.

It is important to note that the template is not intended to be used for producing your actual book—its main purpose is to:

• provide a convenient visual tool to experiment with various page sizes and margins—test and preview various book sizes and margins to help decide which one you prefer;
• produce the layout settings file, mypagesetup.tex, for use in your own work.

Here’s a screenshot showing how the template overlays typeset pages with rulers and borders—note also that margin paragraph areas are displayed if you choose a non-zero value for \marginparwidth.

### Notes on the page-layout parameters and equations

If you look at the equations presented in the Google Slides you might observe they contain many variables; consequently, there are multiple ways to use the equations depending on which LaTeX parameter values you want to calculate and which ones you’d like to set yourself. Let’s take a look at one of the graphics (and relevant equations) contained in the Google Slides:

Clearly, you need to decide which LaTeX parameter values you want to calculate and which ones you will provide as the starting point. The model that we opted for in the Overleaf template works as follows:

1. We set \hoffset and \voffset to 0.
2. You input values for the following LaTeX parameters which affect the book page width:
• \marginparsep
• \marginparwidth
3. You input values for the following LaTeX parameters which affect the book page height:
• \headheight
• \headsep
• \footskip
4. You input the values for your preferred paper size together with the width and height of the book page and the four book margins you would like to use. Note: all values must be input in units of mm.
5. The template calculates the following LaTeX values to implement your book layout (centred on the paper size you selected):
• \evensidemargin, \oddsidemargin, \textwidth, \textheightand \topmargin
6. The Overleaf template also writes out all values (supplied by the user and calculated by the template) into a file called mypagesetup.tex which looks like this:
7. \pdfpagewidth=250mm
\pdfpageheight=350mm
\setlength{\hoffset}{0mm}
\setlength{\voffset}{0mm}
\setlength{\evensidemargin}{65.6mm}
\setlength{\oddsidemargin}{44.6mm}
\setlength{\footskip}{10mm}
\setlength{\topmargin}{29.6mm}
\setlength{\textwidth}{89mm}
\setlength{\textheight}{200mm}
\setlength{\marginparsep}{3mm}
\setlength{\marginparwidth}{25mm}

8. The template provides the command \showvisuals to display a set of visual guides to check your layout, and \showvalues which lists the current content of mypagesetup.tex. \showhelp typesets some brief documentation listing and describing the template commands you can use.

## How to use the Overleaf template

There are 3 simple setup commands you need to use—all expect input in units of mm. Two further commands (\showvisuals and \showvalues) provide additional feedback on the layout you are working. One additional command, \showhelp, typesets some brief documentation.

### The template commands and their arguments

For each command we indicate whether the command is [optional] or [required]—if a command is [required] then you must set all the values controlled by the relevant command or else the template cannot complete the page-layout calculations.

1. \setmarginpars#1#2[optional]: This command sets values for \marginparsep and \marginparwidth. You do not need to use this command unless you want to create margin paragraphs. The default value for both \marginparsep and \marginparwidth is zero. Note that the border of margin paragraphs will only be displayed by \showvisuals when \marginparwidth is non-zero.

2. 
#1=value for \marginparsep (in mm)
#2=value for \marginparwidth (in mm)

Example: \setmarginpars{5}{20}

3. \setheadfoot#1#2#3[required]: This command is used to set values for three LaTeX parameters—these are required for calculating the remaining parameters that control the layout.

4. 
#2=value for headsep in mm (in mm)
#3=value for footskip in mm (in mm)


5. \setpage#1...#8[required]: This is the most important command. You use it to input values for all the key dimensions that determine your book layout. It must only be used after you have set values using the [optional]\setmarginpars command and the [required]\setheadfoot command.

6. 
#1=value for paper width (in mm)
#2=value for paper height (in mm)
#3=value for book page width (in mm)
#4=value for book page height(in mm),
#5=value for book outer margin (in mm)
#6=value for book inner margin (in mm)
#7=value for book top margin (in mm)
#8=value for book bottom margin (in mm)

Example: \setpage{250}{350}{160}{250}{18}{25}{5}{12}

7. \showvisuals[optional]: This command does not take any arguments. It switches on the display of visual rulers and guides that help you check your layout. To switch them off simply comment it out.

8. 
\showvisuals %all elements are displayed

To switch it off just comment it out:

%\showvisuals


9. \showvalues[optional]: This command does not take any arguments. It lists (typesets) the content of the file mypagesetup.tex—the LaTeX code needed to implement your layout. Use it as another option for checking your layout. To switch it off simply comment it out.

10. 
\showvalues %Lists the content of "mypagesetup.tex"

To switch it off just comment it out:

%\showvalues


11. \showhelp[optional]: This command does not take any arguments. It typesets some brief documentation describing the template commands.

12. 
\showhelp %Typeset some brief documentation

To switch it off just comment it out:

%\showhelp



### Minimal working example

Here is a simple example of an Overleaf template project:

\documentclass[twoside]{book} % You need to use this...
\usepackage{pagevisuals}% Overleaf's template
\usepackage{lipsum} % For generating some sample text
\setmarginpars{5}{25}
\setpage{250}{320}{160}{250}{15}{18}{10}{20}
%-------------------------------------------------------
\begin{document}
\showvalues
\showvisuals
\showhelp
% Simple example using fancyhdr.sty
\pagestyle{fancy}
\lipsum[1-10]%using lipsum.sty to generate some sample text
\end{document}


### How to download and use mypagesetup.tex

As noted above, each time you successfully typeset a document the Overleaf template generates a file called mypagesetup.tex which contains the LaTeX settings needed to produce your page layout. Here is a screenshot showing a typical example of mypagesetup.tex being listed in your document using the template command \showvalues:

If you want to use the settings contained in mypagesetup.tex for another project then here is how to do that (note the specific instructions for LuaLaTeX in Step 3):

• Step 1: Download the template project as a ZIP file—make sure you select the “Input and Output Files” option:
• Step 2: Using your preferred ZIP file tools/software, extract mypagesetup.tex and upload it into your Overleaf project in the usual way.
• Step 3 (For pdfLaTeX or XeLaTeX): Include it into your main TeX file: e.g., \input mypagesetup.tex before \begin{document}. For example, a minimal pdfLaTeX or XeLaTeX project might look like this—note the use of \documentclass[twoside]{book}:
• \documentclass[twoside]{book}
\usepackage[utf8]{inputenc} % Remove this line if you use XeLaTeX
\usepackage{lipsum} % Package to generate text for testing
\input mypagesetup.tex
\begin{document}
\lipsum[1-50] % Text for testing. You can, of course, delete this
\end{document}

• Step 3 (For LuaLaTeX): As noted above (An aside: LuaTeX), the developers of the LuaTeX engine implemented changes to its primitives which mean that LuaTeX no longer directly supports the commands \pdfpagewidth and \pdfpageheight for setting the size of the PDF document pages. To use LuaLaTeX you may need to add \let\pdfpagewidth\pagewidth and \let\pdfpageheight\pageheight just before \input mypagesetup.tex—again, note the use of \documentclass[twoside]{book}
\documentclass[twoside]{book}
\usepackage{lipsum} % Package to generate text for testing
\let\pdfpagewidth\pagewidth \let\pdfpageheight\pageheight %only needed for LuaLaTeX
\input mypagesetup.tex
\begin{document}
\lipsum[1-50] % Text for testing. You can, of course, delete this
\end{document}


## Article resources

Here we list a number of resources that we have prepared to help you better understand the LaTeX page-layout parameters. The Google Slides provide details of the simple equations that you can, if you wish, use directly to calculate parameter values to suit the needs of any document.

### Overleaf LaTeX template

We have written and published a LaTeX template which offers an implementation of the equations discussed in this post and provides visual page guides and rulers for previewing your book’s text area and margins.

### Google Slides: Converted to PDF

If you prefer to view the slides as a PDF document, or wish to print them out, you can download the slides in PDF format.

If you want to study the page-layout diagrams in more detail, here is a double-page spread (i.e., facing pages) in PDF format.