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

Custom page layout with LuaTeX (using LaTeX)

About 10 years ago, when working freelance, I produced some diagrams which mapped LaTeX page layout parameters to the equivalent descriptions used by regular book designers. I’ve redrawn everything into SVG using the excellent open-source vector drawing software Inkscape. I’ve uploaded the resulting diagram here in advance of writing a tutorial to explain everything in a bit more detail, including a simple Lua script for use with LuaTeX. Please note that I have only tested viewing the SVG via FireFox (3.6.13 on Windows). Apologies if it does not work for you, feel free to download the diagram and view it offline. In the meantime, I hope you find it useful — I have switched on comments for this post, in case anyone happens to spot an error and would be kind enough to let me know.

Linotype: The Film

I just discovered a fascinating project, a feature-length documentary film centered around the Linotype typecasting machine invented by Ottmar Mergenthaler in 1886. View a trailer and read more about it here Linotype: The Film.

To me, this reflects the sentiment of one of my favourite quotes, from Daniel Boorstin, who warned that planning for the future without a sense of history is like planting cut flowers.

LuaTeX: turbocharged TeX

About a year ago I was Googling for information on how I could typeset my Arabic study notes (I’d been studying Arabic in my spare time for a few years). During my searches I came across a couple of videos published on the fabulous River Valley web site. The videos in question were presented by Professor Idris Hamid:

Those talks demonstrated some truly incredible typesetting of Arabic, using OpenType fonts with Hans Hagen’s amazing ConTeXt system and a new TeX engine, called LuaTeX. It is not overstating the case to say that I was totally blown away; in short, being a geek, it completely “pinged my geek genes” and I just had to find out more about how this was being done. Little did I know that I would be parking my Arabic studies in favour of resurrecting my C programming.

So, what makes LuaTeX so exciting? It is hard to say in just a few lines but, for me, the most exciting things are:

  1. It is being very actively developed by an amazing team of people, including Taco Hoekwater, Hans Hagen and others.
  2. The integration of a real scripting language into the TeX engine. This language is called Lua (hence Lua + TeX).
  3. The ability to use OpenType fonts, including fonts with the MATH table.
  4. Unicode UTF8 for input.
  5. Access to TeX’s “innards”, such as node structures, linebreaking, page building and (PDF) a gazillion other things.
  6. You can extend it with plugins (i.e., DLLs on Windows, .so files on Linux etc).
  7. The source code can be downloaded at any time (via SVN) so you can build it yourself, yes including on Windows!

Over the course of the next few weeks and months I will post some tutorials on LuaTeX, based on what I’ve learnt over the last year or so.

Why I admire MathJax

It is, in some ways, a deep irony that the community which spawned the web (i.e., physicists and mathematicians) should, until recently, have had such a hard time using the web to fluently and easily communicate in the natural language of their disciplines: mathematics. Certainly, domain-specific solutions have evolved through the use and combination of tools and techniques such as jsMath, MathML, SVG, bitmaps, LaTeX text and so forth. The author of this post wrote a COM-based (i.e., Windows!) DVI-to-SVG conversion tool 6 years ago but, at the time, the ultra-specific environment in which it worked precluded its mainstream use. Anyone recall Adobe’s SVG plug-in for Internet Explorer…? It has taken nearly 20 years for the cluster of associated technologies to catch up and coalesce into providing a viable and mainstream solution: MathJax, which delivers into the browser the exquisite beauty of the typesetting algorithms developed by Donald Knuth in the late 1970s. Anybody who has read the infamous Appendix G of the TeXBook or the article Appendix G illuminated will, or should, have profound respect and admiration for the work of jsMath and MathJax. Replication of the TeX math-typesetting algorithms in JavaScript (or any language!) is an incredible achievement for which the authors and financial sponsors of MathJaX should be applauded, for they have made a major contribution to the future of scientific communication via the web ecosystem. Three cheers to them!

Just in passing, from deciding to set-up this WordPress blog, to downloading + installing WordPress and MathJax installation was about 1.5 hours in total, which just goes to show how easy it is, even for a non-expert like me! Here’s a simple formula typeset in TeX/MathJax. Assuming you’re using a modern browser, right-click over the formula to see the options presented by MathJax.

\[\vec{F}_g=-F\frac{m_1 m_2}{r^2} \vec{e}_r\]

MathJax, you totally rock!