## How to Create a Kindle Ebook with LaTeX

Book publishing is easier than ever, and my favorite language to use to create books is LaTeX. The Amazon ebook upload platform (https://kdp.amazon.com) allows users to upload a variety of formats, but not for LaTeX or its product, a PDF. Why is that? The reason is that Kindle ebook viewers are not simply PDF viewers. Kindle takes the text and reformats it in a way so that it is easily readable and provides the customer a repeatable experience reading a variety of books. PDFs, on the other hand, are fixed: no matter what viewer you use, page 37 of a particular book always looks the same.

How then can we use LaTeX to get an Kindle ebook published? Instead of PDFs, the workflow will rely on HTMLs which are converted to MOBI files. The MOBI files can be imported by Kindle. The big advantage of this workflow is that you can use the HTML files to convert into other formats, like posting on your blog (which typically requires the removal of all the LaTeX code and a reformatting).

But no worries, we will go through the workflow step by step, I first wanted to give you the big picture.

So, how do we create the HTML file? I am a fan of “one-click” solutions. Whatever we do, LaTeX should generate the final HTML file and we should not have to make any manual changes. This requires some work, but in the end, it is worth it!

For my setup, I am using Overleaf. It offers an online editor, a PDF preview, an automatic Git backup, and a LaTeX compiler on one single website. If you are using a different LaTeX website or your own local installation, a different configuration might be necessary.

## Converting LaTeX to HTML

To convert LaTeX to HTML, we need a special compiler, TEX4HT. Unfortunately, TEX4HT only works with pdfLaTeX, not with XeLaTeX or LuaLaTeX. So, as an initial preparation of your existing LaTeX code, you might have to make it compatible to pdfLaTeX. If it is already compatible or if you are already working with pdfLaTeX, you can move on to the next paragraph. If not, you will need to: switch to pdfLaTeX in the project settings; add the ifxetex package; and surround XeLaTeX-specific code with a “\ifxetex … \fi” construction. Having this compatibility allows you to generate PDFs with XeLaTeX, and also produce HTMLs with pdfLaTeX when you switch the compiler settings.

TEX4HT itself needs no installation, as it is already part of the Overleaf setup. All you need to do is to include it in your workflow. In Overleaf, this is done by adding a file named “latexmkrc” in the main directory of your project and adding a configuration file (you can find an example here).

First, let’s create the latexmkrc file in the main directory of your project:

$pdflatex = “htlatex %S \”my.cfg,MyFonts,NoFonts\” \”\” \”\” -shell-escape > output.txt; pdflatex -synctex=1 %O %S”; All this does is override the way Overleaf names the compiler$pdflatex, calling it htlatex in addition to pdflatex, and writing the output of the compilation of htlatex to a new file called output.txt. It also references my.cfg, where much of the HTML configuration resides. TODO LINKs

When all compiles, the HTML file will not show up within Overleaf. Instead, you have to actually download the output files (use the drop-down menu at the bottom left in the project window). There, you should check the output.txt for errors. If there are no errors, there should be the HTML file in the same directory, ready for use! You can unzip the file, open it in a browser, and there is your ebook! On to step two!

## Converting HTML to MOBI

The main tool we will be using (besides LaTeX) is the Kindle Previewer. To preview how your HTML will look on various Kindle devices, download the Kindle Previewer here. Besides previewing, it also converts the HTML to MOBI, which can be uploaded to the KDP website or to a Kindle. Generally, for a professional ebook release, it is recommended to get a  set of Kindle devices for testing. With the Kindle uploader tool, you can easily send your MOBI files to a specific device.

Now, open the HTML file with the Kindle previewer. It might give you helpful compiler warnings for you to fix, as well as a conversion to MOBI. Once all is in order, you can browse through your ebook with different Kindle device simulators and, finally, upload the MOBI file to KDP.

But wait! It does not look right!

Now we have to go a little deeper. First, you may wish to review the design documentation by Amazon.

During the conversion, we lost page breaks, maybe some lines, spacing formatting, indentation, etc. The bad news is that there is no 1:1 conversion possible. The good news is that we can include our own .css file to correct a few issues and redefine some of the environments in LaTeX itself to make things right. I will talk about this in a later article. Here, I can talk only about a few instances I have encountered. For others, that is up to you, especially if you used some custom formatting in your LaTeX document.

For example, the title sizes might need some fixing:


h2 {
font-size: 1.5em;
margin-top: 0.83em;
margin-bottom: 0.83em;
font-weight: bold;
}

h3 {
font-size: 1.17em;
margin-top: 1em;
margin-bottom: 1em;
font-weight: bold;
}

h4 {
margin-top: 1.33em;
margin-bottom: 1.33em;
font-weight: bold;
}

h5 {
font-size: 0.83em;
margin-top: 1.67em;
margin-bottom: 1.67em;
font-weight: bold;
}



And kind of a hack, but if you dislike paragraph indents, this is the way to go for Kindle:


p {
margin-top: 1em;
margin-bottom: 1em;
text-indent: 0.01em;
}

In LaTeX itself, you might also want to redefine some commands. The following will move your footnotes to the end of the ebook (if you don’t, they will be put into external html files which are not included in the ebook), add a new command “myrule” for horizontal lines, a new command “emdash” for em-dashes, fix semi-colons, and rewrite newpage to tell Kindle to insert a page break.


\ifxetex

\else
\usepackage{endnotes}
\let\footcite\citep
\ifx\HCode\undefined
\def\myrule{\hrule}
\newcommand{\emdash}[1][]{\hspace{0pt}---\hspace{0pt}}%

\else
\def\myrule{\HCode{<hr style="clear: both" />}}
\def\semicolon{\detokenize{;}}
\def\emdash{\HCode{&\#8212;}}%
\renewcommand\newpage[1][]{\HCode{<mbp:pagebreak />}}
\fi
\let\footnote=\endnote
\fi



That is all for now, I will add a second part with more details (like the table of contents, additional design options, and the index) later. But with some luck, I hope you get your ebook working with these instructions! Feel free to ask questions in the comment section.

2017-03-12T22:46:50+00:00