# Designing a book's layout with LaTeX

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.

### Fonts

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:

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


\usepackage{Alegreya}
\usepackage{AlegreyaSans}
\usepackage{DejaVuSansMono}
\usepackage{mathpazo}
\usepackage{fontspec}


Then I set them as defaults:


% **************************************************
% Fonts
% **************************************************
\defaultfontfeatures{Ligatures=TeX}
\setmainfont[Numbers=Lining]{Alegreya}
\setsansfont[Numbers=Lining]{Alegreya Sans}
\setmonofont{DejaVuSansMono}
\setmathrm{Alegreya}
\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.



This is how ordered/unordered lists, gray boxes and page numbers look like.

### References

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

### References

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:


\usepackage[hang,small]{caption}


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
% **************************************************
\DeclareCaptionFont{sf}{\AlegreyaSansLight}
\DeclareCaptionFont{bf}{\AlegreyaSansMedium}
\captionsetup{width=0.8\textwidth}
\captionsetup{labelfont=bf,textfont=sf}


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.


\usepackage{url}
\usepackage[bookmarks]{hyperref}


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


\definecolor{blue}{RGB}{34,128,188}


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.
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.
• 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{blue}{RGB}{34,128,188}
\definecolor{lightblue}{RGB}{199,234,253}
\definecolor{lightgray}{RGB}{230,230,230}
\definecolor{yellow}{HTML}{F3C50F}
\definecolor{green} {HTML}{9ACD32}
\definecolor{violet}{HTML}{990055}


Then you configure the listings package settings:


% Listings
\lstset{
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},
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:


\begin{lstlisting}[language=cpp]
cout("Hello world!");
\end{lstlisting}


Or you can include a file:


\lstinputlisting{../mcEff/mcScin.c}


Or even just some lines in that file:


\lstinputlisting[
firstnumber = 49,
firstline   = 49,
lastline    = 49
]{../mcEff/mcScin.c}


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

This is listings in action.

### Tables

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:


\usepackage{fancyhdr}
\usepackage[explicit]{titlesec}
\usepackage{tikz}


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.

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:


\newcommand\BackgroundPic{%
\put(-126,0){
\parbox[b][210mm]{297mm}{%
\vfill
\centering
\includegraphics[width=210mm,keepaspectratio]{img/cover.jpg}%
\vfill
}%
}%
}%


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