Writing and publishing an ebook with LaTeX, pandoc, and calibre05 Oct 2015 | by Scott Nesbitt
For the longest time, I’ve been threatening to write and publish an ebook using LaTeX. While I have prepared the PDF versions of some of my ebooks using LyX that’s not quite the same as going full out with LaTeX.
Recently, though, I started gathering some of the essays that I wrote for my bi-weekly newsletter into a book. Using LaTeX, I thought, would be the perfect way to write and publish that book. It was, as far as generating the PDF version. What about EPUB and .mobi? Using pandoc (a conversion tool) and calibre (an ebook management tool), doing that was fairly easy. There was a bit of work involved beyond writing the book, but nothing too techie aside from using the command line.
Here’s what I did.
Getting started with LaTeX
I’m not a huge fan of the stock LaTeX book class. It’s serviceable, but not much else. Instead, I turned to the scrbook class, which is part of the koma-script bundle. koma-script produces a book that’s better looking than the one the standard book class generates. My opinion, I know, and it may differ from yours. That’s the way it is.
I also used the following packages:
- pdfpages (to include the cover I had created, which is a PDF file)
- eurosym (because one of the essays needed to include the symbol for the Euro)
From there, I copied and pasted the text of each essay into my file and added LaTeX markup to format the essays. I also broke the book up into three sections. Once that was done, I generated a PDF. It actually looked quite good, though I needed to tweak a few bits of markup here and there.
Going to EPUB with pandoc
The LaTeX file was done and ready to go. Next up, the EPUB version. Why an EPUB version? PDF is find for viewing on desktop or laptop computers. It’s not so great on smartphones, tablets, or dedicated ereaders. I always like to give people a choice.
The only tool that quickly and accurately converts LaTeX to EPUB is pandoc. I cracked open a terminal window, navigated to the directory holding the LaTeX file, and ran the following command:
pandoc -t epub file-name.tex -o file-name epub
After a couple of seconds, I had an EPUB file. But I needed to do some work on it. Like what?
- Add the cover as an image file (in this case, a JPEG)
- Remove the table of contents — pandoc converted the LaTeX file’s table of contents, but also created the EPUB’s navigation
- Insert the pages that denoted the start of a new section
- Swap the CSS file that pandoc generated with the one I use for my ebooks
All that took about 15 minutes. Once I did that, it was time to create the .mobi file.
Turning to calibre
Why .mobi? It’s the format used with the Amazon Kindle ereader. Yes, I know that the Kindle is a closed device and that .mobi is a closed format but I also know that a lot of people (including more that a couple of FOSS advocates) use the Kindle. Making a book available as a .mobi file can potentially get that book in front of more eyes.
To create the .mobi, I turned to calibre. calibre isn’t just a great tool for managing your ebooks. It’s also excellent for converting them to other formats. First, I imported the EPUB I created into calibre by clicking Add Books > Add books from a single directory. Then, I clicked on the EPUB in the list and clicked Convert books > Convert individually. That opened the Convert window.
In that window, I:
- Selected MOBI from the Output format list
- Clicked Look & Feel and selected Smarten punctuation to create typographical quotes and dashes
- Clicked Page Setup and selected Kindle from the Output Profile list
There are a number of other options in that window, but I didn’t explore them. Maybe in the future.
I clicked OK and after a 10 second wait I had a .mobi file. It didn’t turn out too badly at all, either.
What about the book? I’ll be putting it on sale sometime in early 2016.Thoughts? Let's start a conversation on Twitter.