Writing a maths book in LaTeX
Categories: recreational maths tools
I recently decided to write a book, based in part on articles I have published here.
Writing a maths book presents particular challenges. It is likely to contains a lot of images, and even more formulas.
My book will probably contain hundreds of formulas. It is important to find a way of handling them that not only creates a result that looked good for the reader but also that allows me to work efficiently without being driven crazy by the writing process.
I ultimately chose to use LaTeX, but not before considering two alternatives - MS Word, or Markdown. I have used both these tools for other books, but they were software engineering books rather than maths books.
In this article, I will explain why I made the switch. But the main thing I want to do is demonstrate that LaTeX is actually quite easy to use. I will do that by presenting a minimal, very short book in LaTeX. It includes everything you need to know to create a decent-looking maths book that you wouldn't be ashamed to publish.
Of course, I am not saying this article contains absolutely everything you need to know to write a maths book in LaTeX. For example, you might want to make stylistic changes to give your book a bit of character - those are things you can experiment with as you go along.
But you will most be able to write most of your book, and it will look pretty good, using just the techniques described here. After that, adding a few bells and whistles as you need them will not be too daunting.
Why not MS Word?
I have used MS Word for over 30 years, so when I decided to write my first programming book it was the obvious choice. That book was mainly text, with a few listings (which were just more text, but in a monospaced font), and the odd diagram. Word was a very good fit for this task. I wrote the book in Word and then exported it as a PDF, no problem. To be honest, I had absolutely no idea if the book would even sell a single copy, so I wanted to keep things simple.
Since then, I have moved over to Markdown for everything. However, I did take another look at Word for creating my maths book, for a couple of reason. First, with a book that contains a lot of images, it is useful if you can "float" the images - that is, position them on the page where they fit nicely, even if that is not directly below the place they are referenced in the text. Word can do that. And, secondly, Word can handle complex formulas. At least in theory.
In practice, these Word features are fine for small documents, but when you have dozens of images and hundreds of formulas, the mouse-based interface gets tedious quite quickly. Dragging an image to a different page and then aligning it using a dialog box is quite time-consuming compared to just cutting and pasting text (in Markdown or LaTeX).
Creating formulas using a visual interface is also a bit tedious after a while. Word can accept LaTeX formulas, but I found the interface a bit quirky. After writing a couple of chapters, I decided it wasn't the tool for the job.
Why not Markdown?
I currently write all my articles and books in Markdown. If you are not familiar, Markdown is a plain text format that uses special characters to mark headings, bullet points, formatted text and so on. Here is a simple example:
# Introduction
This is the first chapter.
# Adding features
Here is a bulleted list:
- Point 1
- Point 2
Here is some *italic text* and some **bold text**.
There are also simple ways to create tables, code listings, hyperlinks, and embed images, among other features. The most common use of Markdown is for web content. It is easier to write Markdown than raw HTML, and then Markdown can be converted to HTML simply and predictably. You always get exactly what you would expect.
It is possible to create a book using Markdown, in fact I have done it several times. You can use a special Markdown processor (such as Pandoc) to convert Markdown to PDF. Pandoc can do things like automatic numbering of chapters. It can even do more advanced things like automatically numbering figures and allowing you to reference them from the text.
The problem is that Markdown wasn't designed to do these things, so the advanced rely on hacks. Essentially, you need to embed bits of LaTeX syntax in your Markdown document that are then used by Pandoc to create the document. Rather than doing that, for a complex book, it seemed better to just use LaTeX.
What about LaTeX?
LaTeX was designed for writing maths and scientific books, papers, and articles in general. It is the obvious choice. But doesn't it have a massive learning curve?
Well, it is a big, mature format with lots of features. But if you are familiar with Markdown you can pick it up pretty quickly. And the results are great, straight out of the box. For the remainder of this article, we will look at a simple LaTeX document.
Want to give it a try? The easiest way to get started is probably the Overleaf website (not an affiliate link). It is free for basic use and provides an editor where you can type in some LaTeX, hit the compile button and see the result. It is a great place to start.
When you are ready to tackle a bigger project the options are:
- Stick with the Overleaf free tier, you can do quite a lot with it.
- If a is too limiting, you can move to the paid tier (in my opinion it is slightly expensive for what it does, but if you just have a short document to write it might be the best option).
- Choose one of the many open-source software options. I am using TeXstudio which is free and open source. The GUI looks slightly dated, but it works just fine. There are lots of other alternatives too.
LaTeX example
Here is the LaTeX equivalent of the previous Markdown sample:
\chapter{Introduction}
This is the first chapter.
\section{Adding features}
Here is a bulleted list:
\begin{itemize}
\item Point 1.
\item Point 2.
\end{itemize}
Here is some \textit{italic text} and some \textbf{bold text}.
LaTeX is a bit wordier, for example, it uses \chapter
rather than a simple hash character for first-level headings. But for this simple example, there is a one-to-one correspondence between Markdown and LaTeX implementations. It is no more complicated than Markdown.
LaTeX files are usually stored with a .tex
extension, so this one might be called chapter-1.tex
.
A full LaTeX document
LaTeX does require one extra thing. It needs a few extra lines at the start to define the type of document you wish to create. Since we are creating a PDF rather than an HTML, we need to define things such as the page size, the font we wish to use, etc.
It is common to use a separate LaTeX file to define the document, and then put each chapter in a separate LaTeX file that gets included in the main document. That is entirely optional, but it makes things easier to manage if you have a large document such as a book. Here is a basic document file:
\documentclass[a4paper, 12pt, oneside]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\frontmatter
\tableofcontents
\include{preface}
\mainmatter
\include{chapter-1}
\include{chapter-2}
\backmatter
\include{glossary}
\end{document}
The first line declares the document class, including the page size, font size and type of document. We will make a book with one-sided pages - this is suitable for PDF ebooks. We will use the default font, a serif font not dissimilar to Times Roman.
We then import a few packages using the \include
statement. We set the input encoding to utf8, which is normally the best option (most modern text editors default to this encoding). The graphicx
package is necessary when we add images to our book. The amsmath
and amssymb
packages provide support for maths formulas.
That's it! Our maths book is configured in just five lines. There are lots more options, but we have a perfectly good starting point for a basic maths book.
The \begin{document}
and \end{document}
block defines our document structure. We divide our document into three sections:
- Frontmatter contains things like the preface, foreword, and table of contents.
- Mainmatter contains the book chapters.
- Backmatter contains things like the glossary, index, and references.
The main difference is that the chapters in the mainmatter are automatically numbered - for example, chapter-1
which has the title Introduction will automatically be shown a *Chapter 1 Introduction", whereas the front- and backmatter chapters are left unnumbered. In addition, the frontmatter pages are numbered with Roman numerals, as is common in many books.
\tableofcontents
adds a table of contents, generated automatically from the book's contents.
The include statements pull in the book content. For example \include{chapter-1}
pulls in chapter-1.tex
. By default, LaTeX expects the included files to be in the same folder as the main file, although you can add a path to the include statement if the files are somewhere else.
That is our basic book definition. We just need to write all the chapters (adding extra include statements if required for extra chapters) and compile them into a PDF. If you are using Overleaf you just need to hit the compile button, and you will see a preview on the right-hand side of the screen.
Adding images
How do we add images? It is quite easy and similar to Markdown
The simplest way to add an image is
\includegraphics{image.png}
However, you will normally want to control the size of the image. There are various ways of doing that, but this is quite a good method:
\includegraphics[width=0.5\textwidth]{image.png}
This will scale the image so that it has a width of half the printed part of the page. The image height will be scaled by the same amount so the aspect ratio remains the same. You can vary the width for different images, of course.
This might be adequate for a web page, but typically in a book, you will want to give the image a caption and a figure number so you can reference it in the text. You can do that like this:
\begin{figure}[h]
\centering
\includegraphics[width=0.5\textwidth]{image.png}
\caption{The first image}
\label{fig:first-image}
\end{figure}
The first image is called Figure \ref{fig:first-image}
Here we have created a figure that includes the image. We have added a caption The first image. We have also added a label fig:first-image. LaTeX will automatically give the figure a number, for example, if this was the first image in chapter 3 it would be numbered 3.1.
The figure will appear with a caption Figure 3.1: The first image. If we need to reference the figure from our text we can use \ref{fig:first-image}
, which will be replaced with 3.1. And, of course, as we add figures the numbering will automatically be adjusted.
We have also added an optional \centering
which centres the image horizontally.
Finally, notice the [h]
parameter of the \begin{figure}
block. This tells LaTeX to attempt to put the image on the page where the figure statement is. However, LaTeX will adjust the position depending on the page break. It will generally do a decent job of placing the image sensibly.
Although this might seem like quite a lot of text to place one image, bear im mind that it is almost identical for every image. All you need to do is copy and paste, then change the filename, caption and label text (and possibly the width if needed).
Adding math formulas
LaTeX has a rich notation for maths formulas. It can create pretty much any formula you can imagine.
If you have ever added formulas to a web page using MathJax, you will have used LaTeX Maths. We will look at a couple of examples here, but there are numerous examples online for more complex formulas, including a few sites where you can build formulas graphically and then obtain the LaTeX version.
There are two maths modes in LaTeX:
Inline maths mode is used to display a maths formula as part of the body text. x squared, $x^2$, is an example of this.
Display mode shows formulas on their own line. Here is the quadratic formula:
\[x = \frac {-b \pm \sqrt{b^2 -4ac}} {2a}\]
Inline maths can be used in any text. For example, we could use inline maths in a figure caption:
\caption{Graph of $e^x$}
Summary
If you are starting any kind of maths writing project, from a paper to a complete book, LaTeX deserves a look. It is not really any more difficult than Markdown, and a whole lot less frustrating than trying to work with Word.
There is far, far more to LaTeX than I have included here. There are entire books written about LaTeX. But this article is probably 90% of what you will need to create a nice-looking maths book in LaTeX. You might want to add tweaks to make it your own, but you can do that as you go along. Starting on the right course is the most important thing.
Join the GraphicMaths Newletter
Sign up using this form to receive an email when new content is added:
Popular tags
adder adjacency matrix alu and gate angle area argand diagram binary maths cartesian equation chain rule chord circle cofactor combinations complex modulus complex polygon complex power complex root cosh cosine cosine rule cpu cube decagon demorgans law derivative determinant diagonal directrix dodecagon eigenvalue eigenvector ellipse equilateral triangle euler eulers formula exponent exponential exterior angle first principles flip-flop focus gabriels horn gradient graph hendecagon heptagon hexagon horizontal hyperbola hyperbolic function hyperbolic functions infinity integration by parts integration by substitution interior angle inverse hyperbolic function inverse matrix irrational irregular polygon isosceles trapezium isosceles triangle kite koch curve l system line integral locus maclaurin series major axis matrix matrix algebra mean minor axis nand gate newton raphson method nonagon nor gate normal normal distribution not gate octagon or gate parabola parallelogram parametric equation pentagon perimeter permutations polar coordinates polynomial power probability probability distribution product rule proof pythagoras proof quadrilateral radians radius rectangle regular polygon rhombus root sech set set-reset flip-flop sine sine rule sinh sloping lines solving equations solving triangles square standard curves standard deviation star polygon statistics straight line graphs surface of revolution symmetry tangent tanh transformation transformations trapezium triangle turtle graphics variance vertical volume volume of revolution xnor gate xor gate