Wave Walker DSP

DSP Algorithms for RF Systems

Buy the Book!

Foundations of Digital Signal Processing: Complex Numbers is available Amazon now! Great for young engineers looking for a simple explanation of complex numbers.

Writing an eBook for Amazon Kindle with LaTeX on Linux
April 27, 2022

Table of Contents

Introduction

The following post is a map to avoid the traps and pitfalls of writing an eBook in LaTeX. I’ve compiled my experience into the highlights and I describe what software packages I use, how I write the LaTeX, how to convert it into the EPUB format and where to upload to Amazon. Good luck!

More posts on LaTeX:

Understanding eBooks

An eBook is not like a paper book.

An eBook is a fully digital product that needs to conform to multiple devices such as phones, tablets and eBook readers such as the Amazon Kindle. An eBook does not have page dimensions or fixed font sizes. An eBook should be viewable in both color and black and white.

Much of the formatting will be handled on the eBook reader due to the limitations of the device. Forced page breaks may not be possible. Math symbols may display in some circumstances and be broken in others.

Developing for an eBook is much like developing a piece of software: write, compile, test and repeat until done. Compiling the LaTeX into an EPUB and being able to display on Amazon is difficult! Error messages can be opaque or non-existant. Start small by working with a couple of test pages. Try to create an equation and see if that works. If so, try an image. And so on.

Create an eBook

The eBook you want to publish is going to need content. Don’t focus on the format of the document, but rather what do you want the eBook to be about? What information do you to convey? What story do you want to tell?

The eBook I wrote is an expanded and improved version of some of the blog posts I wrote in the past. I copy and pasted the contents into a LaTeX file since I use WP Quick LaTeX to display the mathematics in my blog.

Finding the Tools

Since I have content already in LaTeX I went looking for a way to convert the LaTeX into an eBook format. Amazon seems to prefer the EPUB format, which is where my search started.

I came across Dan Grec’s post on self-publishing an eBook with LaTeX and pandoc but had problems getting pandoc to convert any of the equations into LaTeX.

I later came across tex4ebook which is provided in the texlive-extra-utils package on Ubuntu 21.10. The benefit of tex4ebook over pandoc was the ability to property display equations in LaTeX.

With some help from the README, LaTeX Stack Exchange, FBReader and a huge amount of trial and error I was able to get an EPUB compiled from LaTeX and uploaded to Amazon’s Kindle Direct Publishing website.

Top Level LaTeX File

Fancy formatting is lost when compiling into an EPUB so my top level LaTeX file is simple:

\documentclass[11pt,twoside,openright]{book}

\usepackage{mathtools}
\usepackage{amssymb}

\input{References}

\title{Foundations of Digital Signal Processing: Complex Numbers}
\author{Matt Carrick, PhD}

\begin{document}

\input{FrontMatter/FrontMatter}
\input{ComplexNumbers/ComplexNumbers}
\input{Derivations/Derivations}
\input{BackMatter/ReferencesHeader}

\end{document}

Chapters and Sections

FrontMatter, ComplexNumbers and Dervivations are each a chapter with multiple sections. Here’s an example of the FrontMatter.tex file:

\input{FrontMatter/Copyright}
\input{FrontMatter/Preface}
\input{FrontMatter/AboutTheAuthor}
\input{FrontMatter/Dedication}

\tableofcontents

The Copyright section is as follows:

\chapter*{Copyright}
\COLORBANNER % an image I use, remove this line

Copyright~\copyright~2022 Matt Carrick. All rights reserved. No portion of this book may be reproduced in any form without permission from the publisher, except as permitted by U.S. copyright law.

For permissions contact: matt@wavewalkerdsp.com

The beginning of an example section is given below:

\section{Euler’s Formula}

A complex number $c$ can be interpreted as a triangle in the complex plane

References

I was unable to get the nice IEEE style references working and instead used footnotes instead. The References.tex file contains all of the references in the following format:

\newcommand{\CarrickDissertation}{M. Carrick, Cyclostationary Methods for Communication and Signal Detection Under Interference. September 24, 2018, https://vtechworks.lib.vt.edu/handle/10919/85126}

A reference is then cited by:

\footnote{\CarrickDissertation}

Warning! A \newcommand cannot have a number it it’s name! So the reference \YourNamePaper2022 needs to be named \YourPaperTwentyTwentyTwo, for example.

Images

Images need to be within the local directory the LaTeX is being compiled in, or a subdirectory of it. Images outside the path of the local directory will be lost when uploading to Amazon and not displayed in the eBook. Here’ s an example of the local path used for an image:

\begin{figure}[h]
\centering
\includegraphics[width=1.25\linewidth]{images/CartesianForm/CartesianForm_complexVector.png}
\caption{The complex vector $2+3j$ in the complex plane.}
\label{fig:CartesianForm_complexVector}
\end{figure}

Also notice that the width is defined as a function of \linewidth. I was never able determine how to center an image, so if you know please leave a comment below!

I also found that \begin{figure} … \end{figure} puts a black bar across the top and bottom of the figure and allows you to use a caption. If you want to use an image without the divider and caption just use \includegraphic.

Equations

I ran into problems with how forced spacing using the tilde ~ in LaTeX equations was displayed. Unfortunately it appears the conversion into EPUB format translates all ~ into x in math mode. For example, ~\&~ was displayed as x&x.

The font sizes for the equations also varied, many time being too small and too hard to read. I forced larger font sizes using \huge in many of the equations.

I also had issues with some greek symbols, most commonly theta, not being displayed inline with other text. I often had to place the theta on new line by itself for it to be able to render. I also removed theta from the section headers.

Installing Dependencies

I installed LaTeX on Ubuntu 21.10 using:

$ sudo apt-get install texlive-full

I also installed FBReader which will be used to preview the eBook before uploading to Amazon:

$ sudo apt-get install fbreader

Compile LaTeX into EPUB

I created a compilation script for turning the LaTeX into an EPUB file:

#!/bin/bash

# define variable name to avoid typos
FILENAME=FoundationsOfDigitalSignalProcessingComplexNumbers

# remove all temp files
rm -rf *.png *.aux *.log *.bbl *.blg *.out *.toc *.xml *.html *.opf *.4ct *.4tc *.bcf *.css *.idv *.lg *.ncx *.xref *.tmp *-epub *.dvi *.epub

# tex4ebook comes with tex-live install
tex4ebook ${FILENAME}.tex

# remove all temp .png files
rm *png

# open ebook with fbreader
fbreader ${FILENAME}.epub &

Save the text into the file createEPUB.sh. Use the following command to give it execute permissions:

$ chmod +x createEPUB.sh

Then run the script to compile and open the eBook:

$ ./createEPUB.sh

If all goes well, you will get an eBook to display!

An example of FBReader opening an EPUB created from LaTeX
An example of FBReader opening an EPUB created from LaTeX

Uploading to Amazon

Go to Amazon’s KDP website and create an account. Go to your Bookshelf and click + Kindle eBook:

Create a new eBook from the Amazon KDP Bookshelf
Create a new eBook from the Amazon KDP Bookshelf

Fill out all of the details about the eBook on the first page. On the second page, upload the EPUB file and then preview it:

Upload the EPUB eBook manuscript to Amazon's KDP
Upload the EPUB eBook manuscript to Amazon's KDP
Click to preview the eBook format on Amazon KDP
Click to preview the eBook format on Amazon KDP

The previewer may take a minute or two to finish, depending on the length and complexity of the eBook:

The eBook can be viewed as a tablet, cell phone or Kindle devices.
The eBook can be viewed as a tablet, cell phone or Kindle devices.

Conclusion

Getting the formatting correct on the Amazon KDP previewer is a process. It took some time for me to understand how the LaTeX formatting is translated through the EPUB format and into Amazon’s website. Note that FBReader and Amazon KDP may render the eBook differently. Be sure to check out the formatting for larger and smaller text sizes as well as different devices.

Once you’re comfortable with the look, set your price and start selling!

More posts on LaTeX:

Leave a Reply