Designing a book's layout with LaTeX

21 min. read

Caution! This article is 9 years old. It may be obsolete or show old techniques. It may also still be relevant, and you may find it useful! So it has been marked as deprecated, just in case.

I just re-designed and old PDF lab guide I made long time ago for a course I was teaching. It was typeset in LaTeX, but it used the defaults, so it looked a bit dull. This is how I designed my first book ever with LaTeX.

Check out the portfolio post to see screenshots of the lab guide.

Set up

The lab guide is part of the repository of the course, where there are other files like macros, etc.

You can find the TeX files in the repo sub-folder called zlabguide.


The first thing I wanted to change was the font. LaTeX default font is awesome in many senses, but it desperately shouts "I was made with LaTeX". A font that I really like a lot is Alegreya, from Huerta Tipográfica (which, very conveniently, was designed for printed books). I think they do a great job with design, and their other fonts are super awesome too (specially robots). I have it installed in my system and in my TeX distribution.

Since I wanted to use Alegreya, I decided to compile with XeLaTeX, which has support for modern font technologies.

For the snippets of code, I used DejaVu Sans Mono, a font that comes with Ubuntu and is very similar to Consolas. It has the four basic families: normal, oblique, bold and bold oblique.

For the equations, I used mathpazo, which are a set of fonts to typeset mathematics to match Palatino. Alegreya is not a LaTeX default font, but Palatino looks very similar (it's also a font I love and I've used it in other projects, but Alegreya wins in my heart). See how mathpazo looks with Palatino:

Preview of Palatino with mathpazo

To use them, I load their packages and fontspec, a package that XeLaTeX uses to manage fonts:


Then I set them as defaults:

% **************************************************
% Fonts
% **************************************************
\setsansfont[Numbers=Lining]{Alegreya Sans}
\setmathsf{Alegreya Sans}
\setboldmathrm[BoldFont={Alegreya Bold}]{Alegreya}

Organization of files

I have my layout settings and preamble in a main file called cosmicrad.tex. Then I divided the guide in chapters and stored the contents of each chapter in its own TeX file. I include them in the main file with \input{filename.tex}. Compiling the main file joins the others together to make the guide.

Since the steps for compiling are repetitive, I put them in a file called cosmicrad, and then run $ . cosmictad in a terminal.

To avoid certain errors, I first delete some files before compiling, but that's just because I'm a control freak. I have to compile three times because I have several references and lists that are build in several steps. Finally, I move the generated PDF guide to the parent directory. The contents of cosmicrad are then:

rm cosmicrad.aux cosmicrad.log cosmicrad.out ../cosmicrad.pdf cosmicrad.toc
XeLaTeX cosmicrad.tex
XeLaTeX cosmicrad.tex
XeLaTeX cosmicrad.tex
mv cosmicrad.pdf ../


To avoid one of the most annoying habits of LaTeX, which is, positioning images and tables where it wants, I used the float package. Then I just have to write [H] next to my image/table/whatever, to place it f*cking HERE. Or, as the manual says much more elegantly, "Here, definitely".

User commands

To make my life easier, I defined several handy new commands for those LaTeX commands that I use the most, for example:


so now, instead of \begin{center} Centered text \end{center} I can do \bc Centered text \ec. I also defined an easier way of typing powers of ten:

\providecommand{\e}[1]{\ensuremath{\times 10^{#1}}}

custom separators:

\newcommand{\separator}{\bc\noindent\rule[2pt]{5mm}{0.1pt}$\sim \star \sim$ \rule[2pt]{5mm}{0.1pt}\ec}

and much more. My favorites are the "graybox", which makes a gray box to highlight special concepts with just a \graybox{I'm inside a gray box!} command:


and the ordered/unordered lists, with bold numbers/stars inside a gray circle:

  \node[shape=circle,draw,inner sep=2pt,fill=lightgray,lightgray!50!white] (char) {\color{gray}#1};}}

Lab guide book layout design - details
This is how ordered/unordered lists, gray boxes and page numbers look like.


With references I'm not referring to the bibliography (not only), but to LaTeX references, which basically are:

  • References to a page, equation, image, table, etc.: for example, if we want to reference an image in our document, we do Fig. \ref{fig:label}, and then we add \label{fig:label} to the image we want to reference. You don't have to use fig:, but it helps keep things organized. If you had tables, for example, you would use tab:, etc. You can reference anything.
  • References to a bibliography item: Those are done with \cite{aut:YY}, where aut are three letters to refer to the author name and YY is the year of publication. Works pretty much as normal references, but instead of \label you use \bibitem[AutYY]{aut:YY} in a separate file, which I called cosmicrad.bbl. This file contains a list of all your bibliography items. After that command, you write the information of your item.
  • The lists (table of contents, list of figures, etc.): Those are very easily inserted by placing \tableofcontents, \listoffigures, etc. where you want the list to appear. Then you just have to use \chapter{Chapter title}, \section{Section title}, \includegraphics{path to the figure}, etc to tell LaTeX that those have to be listed in their respective lists.

This is just basic LaTeX, you can learn more in the LaTeX Wikibooks.

The cool stuff


Regarding the first type of references, I'm using the caption package, which is a cool way to modify LaTeX default captions. You can include some options when you load it:


You can use a lot of modifiers but for this guide I used hang, which indents the caption text, and small, which renders the caption font in a small size. You can even add more settings in your preamble, for example, I set a sans-serif font for the caption, set a bold weight for the label and a light one for the text, and the total width as 80% of the text width.

% **************************************************
% Captions
% **************************************************

Lab guide book layout design - tables and plots
This is how captions look like.

For the second type of references (citing), I chose the alpha style, that is, author/year, which also happens to make the items look like the captions we just set. I did that with \bibliographystyle{alpha} at the end of the main file.

Finally, for the table of contents: if you want to have a table of contents not only in your document but in your PDF reader also, you can use the awesome package hyperref. You should load it after all your other packages. It's a good idea to load the url package too, which makes clicable URLs inside your document.


Now let's define a special blue color for our links:


With \hypersetup you can configure hyperref's options. Since the code is commented, I think it's best to show it as is:

% **************************************************
% PDF output
% **************************************************
\hypersetup {
% --- Configuration options -------------------------------------------------------------------------------------------------------
  breaklinks  = true,		% allow links to break over lines by making links over multiple lines into PDF links to the same target.
% --- Extension options -----------------------------------------------------------------------------------------------------------
  linktoc     = page,		% section, slide, page, none, or all be link on TOC/LOF/LOT. Also linktocpage = true.
  colorlinks  = true,		% false: boxed links; true: colored links (boxed links are not printed). Only named colors work.
  linkcolor   = red,		% color of internal links
  urlcolor    = blue,		% color of external links
  citecolor   = blue,		% color of links to bibliography
% --- PDF-specific display options ------------------------------------------------------------------------------------------------
  linkbordercolor = {1 0 0},			% The color of the box around normal links
  urlbordercolor  = {0.13 0.50 0.73},	% The color of the box around links to URLs
  citebordercolor = {0.13 0.50 0.73},	% The color of the box around citations
% --- PDF display and information options -----------------------------------------------------------------------------------------
  pdftitle     = {Cosmic Radiation},					% title
  pdfsubject   = {Advanced Experimental Techniques},	% subject of the document
  pdfstartview = {FitV},								% fits the height of the page to the window

I am doing several things here:

  • When I open the PDF in a viewer, there will be an index to the left that will allow me to jump to the sections by clicking them.
  • The page numbers in the table of contents of the document are clicable.
  • Different colors for internal (to parts of the document) and external links.
  • I can add meta data to the generated PDF document, like the title, author, etc.

Code syntax highlighting

There's a package for everything in LaTeX. You can use the listings package to highlight your code. First you define your colors:

% **************************************************
% Colors
% **************************************************
\definecolor{green} {HTML}{9ACD32}

Then you configure the listings package settings:

% Listings
  language        = C++,
  breaklines      = true,
  tabsize         = 4,
  frame           = single,
  numbers         = left,
  numberstyle     = \color{gray}\sffamily,
  basicstyle      = \color{violet}\scriptsize\ttfamily,
  keywordstyle    = \color{green!90!black}\bfseries,
  identifierstyle = \color{black},
  commentstyle    = \color{gray}\itshape,
  stringstyle     = \color{blue},
  showstringspaces= false,
  rulecolor       = \color{lightgray},
  backgroundcolor = \color{lightgray!50!white}

Then you can embed your code, even add more specific configurations locally:

cout("Hello world!");

Or you can include a file:


Or even just some lines in that file:

  firstnumber = 49,
  firstline   = 49,
  lastline    = 49

This would show just the line number 49 of the file.

Lab guide book layout design - syntax highlighting
This is listings in action.


For tables I used the package ctable. You can make totally awesome tables with it and it has a lot of useful options. I also used multirow which creates tabular cells spanning multiple rows, and dcolumn, to align on the decimal point of numbers in tabular columns.

The even cooler stuff

In this section, I'll cover the look and feel of the different types of chapters throughout the guide, page numbers, etc.

Cooler also means way more advanced. First, you have to load these three life savers:


The first one lets you configure the way your headings and footers are going to look like. That includes, for example, the format of your page numbers. If you haven't, check this screenshots, where you can see the page number format I ended up using. Any command in the preamble that starts with \fancy is doing configurations in the header and footer of your pages.

The second one lets you personalize the titles of your chapters and sections, which look pretty dull in LaTeX defaults. The third one is a super awesome package that you should check more deeply, because it lets you draw stuff in LaTeX or work with graphics in a very simple way, and it has a lot of commands and possibilities. The manual is a thousand pages long.

With the help of these two packages, I made those white chapter numbers over a yellow background. Then, for anything that is not a "real" chapter title, like the title of the "Contents" page or the bibliography, etc. I use a different style. This also applies to the page numbering which is roman at the beginning, and Arabic later.

Lab guide book layout design - inner pages
LaTeX helping you make cooler chapter titles.

The cover

The package eso-pic lets you make a cover that is worth your content. The command \AddToShipoutPicture lets you use an image as background of a page. Then you can configure that image more precisely in the preamble. For example, for a cover that fits the entire page:


Then you just have to tell eso-pic to use it where you want it:


And that's pretty much all. This is my first book-layout design with LaTeX, I'm sure there are better ways to achieve this, but this is what I found and learned!

What do you think? What would you change? Would you do it in a different way and how? Have you ever designed a book layout? Have you ever used LaTeX?