STM publishing: tools, technologies and change A WordPress site for STM Publishing

5Jan/11Off

LaTeX page layout parameters (Part 1)

Powered by MathJax

I'm using MathJaX to display the formulae in this post because I need some practice with using it, so please excuse this indulgence. Do give comments or feedback on whether you are able to read everything. Again, you can right-click over the formulae to display the MathJaX menu which will let you zoom the formulae for easier reading.

Introduction

LaTeX’s facilities for typesetting are truly superb but I don't think it’s an understatement to say that the parameters controlling page layout are confusing – actually, they are very confusing and trying to achieve custom layouts can be quite frustrating. There are of course some excellent LaTeX packages, such as geometry.sty, which help you set page layout parameters but if you prefer have greater control, then this is the post for you. To get the most from this post, I suggest you grab a copy of the accompanying diagram (in SVG) which shows everything in much more detail.

On TeX, LuaTeX, LaTeX, pdfTeX, XeTeX...

If you are fairly new to "the TeX world", it can be quite difficult to understand the differences between what appear to be so many "different types of TeX". I'll try to explain this, albeit briefly, and with due apologies to expert readers who would be right to say that this is not the whole story. The true, original TeX invented by Donald Knuth, is a typesetting program which understands a number of fundamental (i.e., very low-level or "primitive") commands, which are built into the executable program. Using these low-level commands you can write so-called macros to combine them into "higher level" commands to do useful things, and so create macro packages which provide end-users with a set of tools to create documents. Very large macro packages have been written which provide a rich set of commands to control document typesetting. LaTeX is one such "very large macro package".  To use a macro package, such as LaTeX, you need to process your document with an executable program to generate the typeset output. This "executable progam" is referred to as a "TeX engine": it runs and executes your document plus the LaTeX macros to generate the typeset result. Over the years, the original TeX engine created by Knuth has been extended and enhanced to add new features and these newer engines are required to be given names which distinguish them from Knuth's original application. Examples of newer TeX engines include pdfTeX and the very latest engine LuaTeX. So, it is important to distinguish between the name of a large macro package (e.g., LaTeX) used to write/prepare documents and the executable "TeX engine" used to process those documents: pdfTeX, LuaTeX, XeTeX and so forth. You may see people write, for example, "I am running LaTeX using LuaTeX" and that's a nice way to clarify the difference.

I hope this helps more than it confuses!

Assumed setup

To put this into context, suppose you are using a vanilla flavour LaTeX document class, such as article.cls, and you want to input some LaTeX code to control the layout, as follows:

\documentclass[11pt,twoside]{article}
%input some LaTeX code to control the layout
\begin{document}
......
......
\end{document}

Hopefully, by the end of this mini-tutorial series you will be able to calculate and set the appropriate LaTeX parameters to create any custom page size you wish.

Problem definition

Suppose that you want to typeset, say, a book (or any other document type, e.g., business card) which has a certain page width and page height, and you would like to typeset and layout the book such that when you print out the pages (e.g., for proofing), the book pages will be horizontally and vertically centred on your paper (which can be any size, too). This is the usual situation when printing designs from commercial applications such as Adobe InDesign or Quark Xpress. The same can easily be achieved with LaTeX using just a few simple formulae to calculate the appropriate parameters. Note that adding "crop marks" (or "printers marks") is not covered here.

There are, of course, LaTeX packages to do this for you but if you want to have access to the full details, the following formulae will give you a great deal of control and allow you to write your own tools, e.g., in Perl, Lua etc. We will implement the following in Lua code and integrate it into LuaTeX via a very simple package.

Because LuaTeX is derived from pdfTeX it also uses the \pdfpagewidth and \pdfpageheight commands to set the width and height of the PDF page.

Some definitions

\[\begin{aligned}
\mathrm{B_{PW}}& =\mathrm{\mbox{width of the book page}} \\
\mathrm{B_{PH}}& =\mathrm{\mbox{height of the book page}} \\
\mathrm{B_{OM}}& =\mathrm{\mbox{the Book Outer Margin}} \\
%; i.e., the outer white space margin between the edge of the book page and the start of the text area
\mathrm{B_{IM}}& =\mathrm{\mbox{the Book Inner Margin}} \\
%; i.e., the inner white space margin between the spine (fold) of the book page and the start of the text area}\\
\mathrm{B_{TM}}& =\mathrm{\mbox{the Book Top Margin}} \\
%; i.e., the top white space margin between the edge of the book page and the start of the text area}\\
\mathrm{B_{BM}}& =\mathrm{\mbox{the Book Inner Margin}} \\
%; i.e., the bottom white space margin between the edge of the book page and the start of the text area}\\
\end{aligned}\] \[\begin{aligned}
\Delta \mathrm{X} & =\frac{1}{2}(\mathrm{pdfpagewidth} - \mathrm{B_{PW}})\\
\Delta \mathrm{Y} & =\frac{1}{2}(\mathrm{pdfpageheight} - \mathrm{B_{PH}})\\
\end{aligned}\]

If you follow this diagram you should see that the following formulae can be used to calculate the LaTeX parameters such that your desired book page is centred horizontally and vertically on the PDF paper area. Note that although I use the term "book" you can use these formulae to centre any document type/page within a larger PDF document area (see PDF samples, below).

Formulae for the width of the PDF page

  • Starting with left-hand (even-numbered book pages)
\[\begin{aligned}
\mathrm{pdfpagewidth} & = \Delta \mathrm{X} + \mathrm{B_{OM}} \\
& + \mathrm{marginparwidth} \\
& + \mathrm{marginparsep} \\
& + \mathrm{textwidth} \\
& +\mathrm{B_{IM}}+ \Delta \mathrm{X}
\end{aligned}
\]

and

\[\begin{aligned}
\mathrm{pdfpagewidth} & = \mathrm{1 inch} + \mathrm{hoffset}\\
& + \mathrm{evensidemargin} \\
& + \mathrm{textwidth} \\
& +\mathrm{B_{IM}}+ \Delta \mathrm{X}
\end{aligned}
\]
  • For right-hand (odd-numbered book pages)
\[\begin{aligned}
\mathrm{pdfpagewidth} & = \Delta \mathrm{X} + \mathrm{B_{IM}} \\
& + \mathrm{textwidth} \\
& + \mathrm{marginparsep} \\
& + \mathrm{marginparwidth} \\
& +\mathrm{B_{OM}}+ \Delta \mathrm{X}
\end{aligned}
\]

and

\[\begin{aligned}
\mathrm{pdfpagewidth} & = \mathrm{1 inch} + \mathrm{hoffset} \\
& + \mathrm{oddsidemargin} \\
& + \mathrm{textwidth} \\
& + \mathrm{marginparsep} \\
& + \mathrm{marginparwidth} \\
& +\mathrm{B_{OM}}+ \Delta \mathrm{X}
\end{aligned}
\]

Formulae for the height of the PDF page

\[\begin{aligned}
\mathrm{pdfpageheight} & = \mathrm{1 inch} + \mathrm{voffset} \\
& + \mathrm{topmargin} \\
& + \mathrm{headheight} \\
& + \mathrm{headsep} \\
& + \mathrm{textheight} \\
& + \mathrm{footskip} \\
& +\mathrm{B_{BM}}+ \Delta \mathrm{Y}
\end{aligned}
\]

and

\[\begin{aligned}
\mathrm{1 inch} + \mathrm{voffset} + \mathrm{topmargin}= \Delta \mathrm{Y} + \mathrm{B_{TM}} \\
\end{aligned}
\]

In Part 2 I'll show how to use these formulae via some examples, but till then here are some random demos (PDFs) of the formulae in action.

  1. Demo 1
  2. Demo 2
  3. Demo 3