Wave Walker DSP

DSP Algorithms for RF Systems

Trending

Buy the Book!

DSP for Beginners: Simple Explanations for Complex Numbers! The second edition includes a new chapter on complex sinusoids.

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 found 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:

3 Responses

  1. In the section of the post about images there’s a mention of centering. Did you try wrapping the image with
    \begin{center}
    \begin{figure}

    \end{figure}
    \end{center}
    ?

    1. Great question. I have tried that but the difference is that the formatting is all controlled by the eBook viewer. The eBook viewer will decide things like font size, when a new page starts, image size and many others. Formatting for eBooks requires a different mindset than traditional type-setting with LaTeX.

Leave a Reply

God, the Lord, is my strength; He makes my feet like the deer's; He makes me tread on my high places. Habakkuk 3:19
For everything there is a season, and a time for every matter under heaven. A time to cast away stones, and a time to gather stones together. A time to embrace, and a time to refrain from embracing. Ecclesiastes 3:1,5
The earth was without form and void, and darkness was over the face of the deep. And the Spirit of God was hovering over the face of the waters. Genesis 1:2
Behold, I am toward God as you are; I too was pinched off from a piece of clay. Job 33:6
Enter His gates with thanksgiving, and His courts with praise! Give thanks to Him; bless His name! Psalm 100:4
Lift up your hands to the holy place and bless the Lord! Psalm 134:2
Blessed is the man who trusts in the Lord, whose trust is the Lord. He is like a tree planted by water, that sends out its roots by the stream, and does not fear when heat comes, for its leaves remain green, and is not anxious in the year of drought, for it does not cease to bear fruit. Jeremiah 17:7-8
He said to him, “You shall love the Lord your God with all your heart and with all your soul and with all your mind. This is the great and first commandment. And a second is like it: You shall love your neighbor as yourself. On these two commandments depend all the Law and the Prophets.” Matthew 22:37-39
Then He said to me, “Prophesy over these bones, and say to them, O dry bones, hear the word of the Lord. Thus says the Lord God to these bones: Behold, I will cause breath to enter you, and you shall live." Ezekiel 37:4-5
Riches do not profit in the day of wrath, but righteousness delivers from death. Proverbs 11:4
The angel of the Lord appeared to him in a flame of fire out of the midst of a bush. He looked, and behold, the bush was burning, yet it was not consumed. And Moses said, “I will turn aside to see this great sight, why the bush is not burned.” When the Lord saw that he turned aside to see, God called to him out of the bush, “Moses, Moses!” And he said, “Here I am.” Exodus 3:2-3
Daniel answered and said: “Blessed be the name of God forever and ever, to whom belong wisdom and might. He changes times and seasons; He removes kings and sets up kings; He gives wisdom to the wise and knowledge to those who have understanding." Daniel 2:20-21
Previous slide
Next slide

This website participates in the Amazon Associates program. As an Amazon Associate I earn from qualifying purchases.

© 2021-2024 Wave Walker DSP