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.
The theme of this article, and the purpose of the resources we have provided, is based on the following objectives:
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
.
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:
The above diagram is also included in the Google Slides and is used as the starting point for arriving at the page-layout equations.
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.
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:
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.
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:
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:
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.
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.
In summary, it offers the following:
mypagesetup.tex
that you can obtain by downloading the template-based project as a ZIP file;It is important to note that the template is not intended to be used for producing your actual book—its main purpose is to:
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
.
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:
\hoffset
and \voffset
to 0
.\marginparsep
\marginparwidth
\headheight
\headsep
\footskip
\evensidemargin
, \oddsidemargin
, \textwidth
, \textheight
and \topmargin
mypagesetup.tex
which looks like this:
\pdfpagewidth=250mm
\pdfpageheight=350mm
\setlength{\hoffset}{0mm}
\setlength{\voffset}{0mm}
\setlength{\evensidemargin}{65.6mm}
\setlength{\oddsidemargin}{44.6mm}
\setlength{\headheight}{18mm}
\setlength{\headsep}{5mm}
\setlength{\footskip}{10mm}
\setlength{\topmargin}{29.6mm}
\setlength{\textwidth}{89mm}
\setlength{\textheight}{200mm}
\setlength{\marginparsep}{3mm}
\setlength{\marginparwidth}{25mm}
\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.
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.
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.
\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.#1=value for \marginparsep (in mm) #2=value for \marginparwidth (in mm) Example: \setmarginpars{5}{20}
\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.#1=value for headheight (in mm) #2=value for headsep in mm (in mm) #3=value for footskip in mm (in mm) Example: \setheadfoot{18}{5}{10}
\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.#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}
\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.\showvisuals %all elements are displayed To switch it off just comment it out: %\showvisuals
\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.\showvalues %Lists the content of "mypagesetup.tex" To switch it off just comment it out: %\showvalues
\showhelp
[optional]: This command does not take any arguments. It typesets some brief documentation describing the template commands.\showhelp %Typeset some brief documentation To switch it off just comment it out: %\showhelp
Here is a simple example of an Overleaf template project:
\documentclass[twoside]{book} % You need to use this...
\usepackage{pagevisuals}% Overleaf's template
\usepackage{fancyhdr} % To add some headers/footers
\usepackage{lipsum} % For generating some sample text
\setmarginpars{5}{25}
\setheadfoot{10}{5}{10}
\setpage{250}{320}{160}{250}{15}{18}{10}{20}
%-------------------------------------------------------
\begin{document}
\showvalues
\showvisuals
\showhelp
% Simple example using fancyhdr.sty
\fancyhead[LE]{{\fontsize{12}{12}\selectfont\thepage}}
\fancyhead[RE]{\textsc{text for left-hand pages}}
\fancyhead[LO]{\textsc{Text for right-hand pages}}
\fancyhead[RO]{{\fontsize{12}{12}\selectfont\thepage}}
\pagestyle{fancy}
\lipsum[1-10]%using lipsum.sty to generate some sample text
\end{document}
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):
mypagesetup.tex
and upload it into your Overleaf project in the usual way.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}
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}
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.
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.
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.
We hope this article is useful and has provided some interesting ideas and solutions for exploring page layout using LaTeX. As always, we are delighted to hear from readers so do please contact us with your comments and feedback.
Happy (La)TeXing!