Writing Your Mathematics Book with LaTeX: A Comprehensive Guide
Written on
Chapter 1: Introduction to LaTeX
I recently embarked on the journey of authoring a book, partly inspired by articles I've shared previously.
Writing a mathematics book comes with its unique set of challenges. It will likely include a multitude of diagrams and an even greater number of equations. My project is expected to feature hundreds of formulas, necessitating a method that not only produces visually appealing results for the reader but also allows for an efficient writing process without causing frustration.
After deliberation, I opted to utilize LaTeX, having briefly considered two alternatives: MS Word and Markdown. While I've employed both of these tools for previous writing projects—specifically software engineering texts—they seemed less suitable for mathematics.
In this article, I will outline my reasons for selecting LaTeX. My primary goal is to showcase that LaTeX is indeed quite user-friendly. To illustrate this, I will present a concise, fundamental book structure in LaTeX. This guide will encompass everything necessary to craft a visually appealing mathematics book that you wouldn't hesitate to publish.
Of course, I do not claim this article contains all the knowledge you need to write a mathematics book in LaTeX. You might wish to make stylistic adjustments to infuse some personality into your work—these are areas you can explore as you progress. However, you should be able to draft the majority of your book using the techniques discussed here, with additional enhancements becoming manageable as needed.
Why I Chose LaTeX Over MS Word
Having used MS Word for over three decades, it was the obvious choice when I first contemplated writing a programming book. That initial book was largely text-based, featuring a few code snippets and some diagrams. Word served me well for that project—I wrote the manuscript in Word and successfully exported it as a PDF. To be honest, I had no expectations regarding sales, so I aimed for simplicity.
Since then, I've transitioned entirely to Markdown for all my writing. However, I revisited Word for my mathematics book for a couple of reasons. First, given the volume of images, it’s advantageous to "float" them—placing them in a visually appealing manner on the page, even if they don't appear directly beneath their corresponding references in the text. Word can facilitate this. Second, it has the capacity to manage complex equations. At least, that’s the theory.
In practice, these features work well for shorter documents, but the mouse-driven interface becomes cumbersome when dealing with numerous images and equations. Adjusting the placement of an image or aligning it via a dialog box can be laborious compared to simply copying and pasting text in either Markdown or LaTeX.
Creating equations through a visual interface also becomes tedious over time. While Word can accept LaTeX equations, I found its interface somewhat cumbersome. After drafting a couple of chapters, I concluded it was not the right tool for my needs.
Why Not Markdown?
Currently, I write all my articles and books in Markdown. For those unfamiliar, Markdown is a plain text format that employs specific characters to denote headings, bullet points, formatted text, and more. Here’s a brief 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.
Markdown also provides straightforward methods for creating tables, code snippets, hyperlinks, and embedding images, among other features. The most common application of Markdown is for web content, as it is simpler to write than raw HTML, and conversion to HTML is both easy and reliable.
It is indeed possible to write a book using Markdown; I have done so multiple times. You can utilize a specialized Markdown processor, such as Pandoc, to convert Markdown to PDF. Pandoc can handle tasks like automatic chapter numbering and more advanced features like figure numbering and referencing.
However, Markdown wasn't specifically designed for this purpose, so advanced functions often require workarounds. Essentially, you need to embed sections of LaTeX syntax into your Markdown document for Pandoc to process it effectively. Given the complexity of my book, I found it more sensible to use LaTeX directly.
Why LaTeX?
LaTeX was specifically crafted for writing mathematical and scientific texts, making it the ideal choice. But doesn’t it have a steep learning curve?
While it is a comprehensive and mature system with extensive features, those already familiar with Markdown can adapt to it relatively quickly. The results are impressive right from the start. In the following sections, we will explore a simple LaTeX document.
For those eager to dive in, the Overleaf platform is likely the best starting point (not an affiliate link). It offers free basic usage and features an editor where you can input LaTeX, click the compile button, and view the output. It's an excellent way to begin.
When you're ready for larger projects, you have a few options:
- Continue with the Overleaf free tier, which offers substantial capabilities.
- Upgrade to the paid tier if you find the free version limiting (though I consider it slightly pricey for what it provides, it may be worthwhile for brief documents).
- Choose from various open-source software alternatives. I personally use TeXstudio, which is both free and open-source. Its interface may seem somewhat outdated, but it functions perfectly well. There are many other alternatives available too.
LaTeX Example
Here’s the LaTeX counterpart to the earlier Markdown example:
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 somewhat more verbose; for instance, it employs chapter rather than a simple hash symbol for first-level headings. However, in this straightforward example, there exists a one-to-one correlation between Markdown and LaTeX implementations, making it no more complex than Markdown.
LaTeX files typically carry a .tex extension, so this one could be named chapter-1.tex.
Creating a Full LaTeX Document
LaTeX necessitates a few additional lines at the beginning to specify the type of document being created. Since we aim to produce a PDF rather than HTML, we need to indicate parameters such as page size and font type, among others.
It’s common to use a separate LaTeX file for the document's structure, with each chapter housed in its own file. This approach is optional but can simplify management for larger documents like books. Here’s a basic document configuration:
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, specifying the page size, font size, and document type. In this case, we are making a book with single-sided pages, suitable for PDF ebooks. The default font, a serif type similar to Times Roman, will be used.
Next, we import several packages using the include command. We set the input encoding to UTF-8, which is generally the best option, as most modern text editors default to this encoding. The graphicx package is essential for including images in our book, while the amsmath and amssymb packages support mathematical formulas.
That’s it! Our mathematics book is configured in just five lines. While there are many more options, this gives us a solid foundation for a basic mathematics book.
The begin{document} and end{document} commands establish our document’s structure. We categorize the document into three sections:
- The frontmatter contains elements like the preface, foreword, and table of contents.
- The mainmatter includes the chapters of the book.
- The backmatter contains elements such as the glossary, index, and references.
The main distinction is that chapters in the mainmatter are automatically numbered— for instance, chapter-1 titled "Introduction" will be displayed as "Chapter 1: Introduction," while sections in the front- and backmatter remain unnumbered. Additionally, frontmatter pages are numbered with Roman numerals, a common practice in many books.
The tableofcontents command generates a table of contents automatically from the book's sections.
The include statements pull the book's content. For example, include{chapter-1} incorporates chapter-1.tex. By default, LaTeX expects the included files to reside in the same folder as the main file, but you can specify a path if the files are located elsewhere.
This defines our basic book structure. All that remains is to write each chapter (adding extra include statements as necessary for additional chapters) and compile them into a PDF. If you’re using Overleaf, simply hit the compile button to view a preview on the right side of the screen.
Adding Images
How do we incorporate images? It’s quite straightforward and resembles Markdown.
The simplest way to include an image is:
includegraphics{image.png}
However, you’ll likely want to adjust the image size. There are various methods to achieve this, but a common approach is:
includegraphics[width=0.5textwidth]{image.png}
This scales the image to a width of half the printed page width, maintaining the aspect ratio. You can modify the width for different images as needed.
While this might suffice for a web page, in a book, you’ll typically want to provide a caption and figure number for referencing in the text. You can do this as follows:
begin{figure}[h]
centering
includegraphics[width=0.5textwidth]{image.png}
caption{The first image}
label{fig:first-image}
end{figure}
Here, we create a figure that incorporates the image, adds a caption, and assigns a label. LaTeX will automatically number the figure; for instance, if this is the first image in chapter 3, it will be numbered 3.1. The figure will be displayed with the caption "Figure 3.1: The first image." If we reference the figure in our text, using ref{fig:first-image} will display the corresponding number (3.1). Additionally, the optional centering command centers the image horizontally.
Lastly, notice the [h] parameter in the begin{figure} block. This instructs LaTeX to attempt to position the image on the page where the figure statement appears, although it may adjust the placement depending on page breaks. Generally, it does a good job of positioning images sensibly.
While this may seem like a lot of text just to place one image, remember that the process is nearly identical for every image. Simply copy and paste the structure, updating the filename, caption, label text, and possibly the width as needed.
Inserting Mathematical Formulas
LaTeX boasts a rich syntax for mathematical formulas, capable of rendering virtually any equation you can conceive.
If you've ever added equations to a webpage using MathJax, you've utilized LaTeX math syntax. We'll explore a couple of examples here, though numerous online resources exist for more intricate formulas, including sites where you can graphically construct equations and obtain the LaTeX version.
LaTeX offers two modes for mathematics:
Inline math mode is used for displaying a formula within the body text. For example, x squared can be represented as $x^2$.
Display mode showcases formulas on their own line. Here’s the quadratic formula:
[x = frac {-b pm sqrt{b^2 -4ac}} {2a}]
Inline math can be incorporated into any text. For instance, we could utilize inline math within a figure caption.
Summary
If you're embarking on any mathematics writing endeavor, whether a paper or a complete book, LaTeX is worth considering. It’s not significantly more challenging than Markdown, and it proves far less frustrating than working with Word.
There is a wealth of information on LaTeX beyond what I've covered here. Entire books have been dedicated to the subject. Nevertheless, this article likely encapsulates about 90% of what you'll need to produce an attractive mathematics book in LaTeX. You can always introduce personal touches as you progress, but commencing your project on the right foot is crucial.
If you found this article helpful, consider subscribing to my free newsletter.
An introductory video on using LaTeX to create beautiful mathematical equations, perfect for beginners.
Chapter 2: LaTeX Basics for Book and Report Writing
In this chapter, we will delve into the foundational aspects of LaTeX, focusing on its application in writing books and reports.
A tutorial video that covers the essential features of LaTeX for book and report writing, equipping you with the necessary skills.