# Use LaTeX to produce ePub

I'm asking here a question which was asked on the Italian TeX group forum.

Is there a way to produce ePub output automatically from a LaTeX source?

I should remark that ePub is just HTML + a subset of CSS + some XML for metadata, packed in a ZIP file. So one can settle with the existing tools for HTML conversion. Still something better could be done for ePub output.

In the first place, ePub accepts only a subset of CSS, then it would be nice to produce the file with the metadata from the LaTeX source too. Finally, I'm not sure how it works, but ePub files allow for automatic hyphenation of words. By this I mean that hyphenation can be specified in the ePub file itself, so that a reader will not need to know hyphenation rules. Note that since ePub files are resizable, all possible points of hyphenation must be specified.

• How would epub handle math ? I'm thinking that an epub format would be a nice touch for my papers when viewed on a mobile browser, but I don't know if the math would get rendered right. Aug 15, 2010 at 5:33
• Of course this would not be suitable for a math book. But it may be suitable for a novel. Aug 17, 2010 at 10:29
• @Suresh: EPUB 2.0.1 doesn't have any special support for representing mathematics: you are supposed to represent it using SVG images. The EPUB 2.1 working group is considering including Mathml to support maths. I'm not sure how well ebook readers handle Mathml. Sep 6, 2010 at 14:26
• Also, epub3 supports MathML now (at least a subset): idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-mathml Jan 19, 2012 at 11:03
• more recent discussion: tex.stackexchange.com/q/95600/6853
– user6853
Jan 31, 2013 at 18:00

## 12 Answers

Pandoc supports converting Latex to Epub. I don't think there's any "black box" solution producing high-quality output, but the HTML that Pandoc generates is easy to work with.

Resources:

1. John MacFarlane's Creating an ebook with pandoc

2. My answer recommending Pandoc (now deleted, so included inline below) to SO question, User manual for Java software: In-application help + PDF, which also discusses some other useful technologies for dealing with Epub conversion.

It's not clear what your problem with HTML is: I recommend looking at the HTML-based Epub format.

There are Latex to Epub converters, such as Pandoc, which generate the separate TOC and metadata files, possibly making the representation more satisfactory from your point of view.

Epublib has a bundled Java viewer (source) that you could tailor to your needs.

Note that you can generate SVG from PDF, which may offer you some flexibility.

• Pandoc doesn't really convert Latex to epub - it leaves a lot of things like figure labels and references out. Might be more trouble "debugging" what pandoc generates, than it's worth, especially if you are converting a 200 page book. I'm tackling this problem right now... Jan 26, 2012 at 0:32
• @drozzy: In the absence of a good "black-box" solution and you care about quality output, you are going to have to go through the resulting document line-by-line anyway. I'd usually value cleanliness of output over completeness of mapping, based on experience. Using Pandoc's -R option should keep all the needed labels in the output. Feb 9, 2012 at 12:12
• +1 for answer + for idiom you call "black box" solution. never heard this name/term for it but describes really well, alternatively to "automated", "programmatic" (+ yet to write a solution), or similar. Jun 13, 2013 at 10:55

I use the following very simple script:

latexml --dest=$1.xml$1.tex
latexmlpost -dest=$1.html$1.xml
ebook-convert $1.html$1.epub --language en --no-default-epub-cover


It makes use of latexml and ebook-converter (the command line tool that comes with calibre). This works very well with lots of formulas. Once mathML is encoded in newer epub versions things will even get prettier by using latexmlpost -dest=$1.xhtml$1.xml instead of the second line of my script.

• This is not perfect but it's the best I've seen so far. Jan 22, 2013 at 9:04
• +1 never checked that calibre comes with an ebook-converter as stand-alone tool before! Jun 13, 2013 at 11:05
• These binaries are not necessarily on the path. To add them run: sudo ln -s /opt/local/libexec/perl5.12/sitebin/latexml /usr/bin/latexml, sudo ln -s /opt/local/libexec/perl5.12/sitebin/latexmlpost /usr/bin/latexmlpost, and sudo ln -s /Applications/calibre.app/Contents/MacOS/ebook-convert /usr/bin/ebook-convert May 6, 2014 at 13:56
• On Mac, install with brew cask install calibre to have the aliases created as part of the setup.
– xgMz
Feb 18, 2018 at 18:25
• Unfortunately, the output of this is much worse than the output of pandoc. The inter-word spaces are gone, the bibliography is not printed, and math conversion chokes on super/sub scripts. Feb 1, 2021 at 18:57

I’ve never tried to automate this, but I would think that a script that generates HTML from the TeX source, and then passes that HTML to Calibre’s ebook-convert command line program would be sufficient. You could use TeX4t or similar for the first step.

Do any ePub viewers support hyphenation, even when specified? Most ePub software is based on Adobe Digital Editions, and last I heard, it chokes on this. I doubt iBooks is any better.

• I'm using Calibre or the Firefox add-on Epub viewer, and both support hyphenation. I don't own an ebook reader, so I can't tell about the support for them. Aug 11, 2010 at 17:20
• Also, I think that doing LaTeX->HTML->Epub would lose both the metadata and the hyphenation information, which were the reason of my question. Aug 11, 2010 at 17:21
• I don't know of any way to do hyphenation—I really think it’s a bad idea to try given the current state of ebook readers (as noted in the thread I linked to, ADE, which is what is used on the Nook and Sony Reader, will break the line but not insert a visible hyphen, which is very ugly. As for the other point, calibre will preserve the metadata, so long as either it makes it into the HTML, or you write your script to include it. (See the manual page for ebook-convert I linked above; it has flags for the metadata.) Aug 11, 2010 at 20:29

tex4ebook can be used to produce Epub, Epub 3 and Mobi from LaTeX sources directly. It uses tex4ht as the underlying conversion engine. It supports full LaTeX, including most packages and custom macros, it saves basic document metadata like title, author and the document language. It can be configured to include custom CSS styles and fonts in the ebook, so it is possible to change it's the appearance quite a lot.

To convert document to Epub 3, the following command may be used:

tex4ebook -f epub3 filename.tex


As Epub 3 readers should support MathML, you may request it as well, adding mathml option to the command:

tex4ebook -f epub3 filename.tex mathml

• Thanks for this. I used the verse package to format poetry and pandoc created a mess. This worked great. Feb 5, 2020 at 14:15
• @TerriSimon that is good to hear, thanks :) Feb 5, 2020 at 14:36
• This is the best solution I found so far, thanks. I'm having just problem with the figures sizes (too small), maybe because I use scale instead of defining the width (but I had this problem using the alternative methods too). Aug 17, 2020 at 23:55
• @TheDoctor it may help if you generate bounding boxes for your figures using ebb -x *.jpg (and similar for PNG or PDF figures). I am not sure it will help though. You may try to post a new question here so I can try to find a fix for your particular use case. Aug 18, 2020 at 11:05
• this should be the main answer Apr 19, 2021 at 20:43

Here's link that mentions using LaTeXML to convert LaTeX to ePub: http://uk.tug.org/2010/10/16/uk-tug-discussion-meeting-and-agm/ (see third-last paragraph or so)

• Indeed, Andrew Ford gave a talk on exactly this topic yesterday. He seemed to have had good success with LaTeXML, although he is still very much learning the necessary points. Oct 17, 2010 at 6:16

Is it possible that http://plastex.sourceforge.net/ might be somehow related to the solution requested by the original poster?

(I have only just found Plastex today, and so have had no time to research it very deeply, but I will return and update my post if I find it to be the solution to this question.)

I have found Calibre and Sigil to be excellent tools for working with epub format ebooks, but being able to create epub directly from a LaTeX source document would surely be ideal in my opinion. :)

• I found PlasTex very easy to use - you need to simplify your LaTeX script slightly but I had my stuff re-compiling perfectly. The only problem is that it looks like a man-page. What I really want to do is turn it into something that looks like a Novel. Any suggestions? Apr 4, 2011 at 23:11
• I followed up on your suggestion and was successful. Manpage problem solved creating a new theme and few more tricks. I have 2 MOBI on Amazon. Seehttp://www.amazon.com/LaTeX-ePub-MOBI-iBook-Conversion-ebook/dp/B0190O0OIY/ ...thanks for pointing me in this direction. Dec 13, 2015 at 18:21
• @Echeban - Do you have this on a gist / github anywhere? Feb 11, 2018 at 17:45
• Aug 27, 2018 at 15:09

ConTeXt allows for export to xml (and xhtml). With a small wrapper script, it should be possible to convert the output to epub. For example, consider a simple TeX file that inputs another file and has some math in it:

\setupbackend[export=yes]

\starttext
\section {The first section}

\input tufte

\startformula
f(x) = \frac 1n \log \Pr(y^n > x)
\stopformula

\stoptext


Processing it through ConTeXt creates a file \jobname.export as follows

<?xml version='1.0' standalone='yes' ?>

<!-- input filename   : test              -->
<!-- processing date  : Fri May  6 23:20:25 2011 -->
<!-- context version  : 2011.05.06 16:52  -->
<!-- exporter version : 0.20              -->

<document language="en" file="test" date="Fri May  6 23:20:25 2011" context="2011.05.06 16:52" version="0.20" xmlns:m="http://www.w3.org/1998/Math/MathML">
<section detail='section' location='aut:1'>
<sectionnumber>1</sectionnumber>
<sectiontitle>The first section</sectiontitle>
<sectioncontent>
We thrive in information--thick worlds because of our marvelous and everyday capacity to select, edit, single out, structure, highlight, group, pair, merge, harmonize, synthesize, focus, organize, condense, reduce, boil down, choose, categorize, catalog, classify, list, abstract, scan, look into, idealize, isolate, discriminate, distinguish, screen, pigeonhole, pick over, sort, integrate, blend, inspect, filter, lump, skip, smooth, chunk, average, approximate, cluster, aggregate, outline, summarize, itemize, review, dip into, flip through, browse, glance into, leaf through, skim, refine, enumerate, glean, synopsize, winnow the wheat from the chaff and separate the sheep from the goats.

<formula>

<formulacontent>
<m:math>
<m:mrow>
<m:mi>%Gð%@ </m:mi>
<m:mo>(</m:mo>
<m:mi>%Gð¥%@</m:mi>
<m:mo>)</m:mo>
<m:mo>=</m:mo>
<m:mrow>
<m:mfrac>
<m:mrow>
<m:mn>1</m:mn>
</m:mrow>

<m:mrow>
<m:mi>%Gð%@</m:mi>
</m:mrow>
</m:mfrac>
</m:mrow>
<m:mrow>
<m:mi>l</m:mi>
<m:mi>o</m:mi>
<m:mi>g</m:mi>
</m:mrow>
<m:mrow>
<m:mi>P</m:mi>
<m:mi>r</m:mi>
</m:mrow>
<m:mo>(</m:mo>
<m:msup>
<m:mi>%Gð¦%@</m:mi>
<m:mi>%Gð%@</m:mi>
</m:msup>
<m:mo>&gt;</m:mo>
<m:mi>%Gð¥%@</m:mi>
<m:mo>)</m:mo>
</m:mrow>
</m:math>
</formulacontent>
</formula>
</sectioncontent>
</section>
</document>


(Note: The unicode math characters get garbled during copy paste). This is a translation of the output of the TeX file: the contents of the \inputed file are exported and the TeX math is converted into MathML. This feature is still experimental.

• In a Usenet thread about this feature, comments.gmane.org/gmane.comp.tex.context/68217, Hans talks of there being an article on this feature: do you have access to that? May 9, 2011 at 12:38
• @Charles: That thread is from the ConTeXt mailing list (not usenet). I could not get the Eurotex article. Later in that thread, Hans has said that the latest version allows for an epub export as well but I have not tested that yet. May 10, 2011 at 18:30
• Context MkIV can generate epub. Oct 4, 2011 at 22:18

For kindles you can upload a pdf of your latex file. I added the following to my latex file, and when I uploaded it I found I had a single page per screen at a readable size. There was no need to scroll down the page. However, I used it for a document that had exclusively text. How math would turn out, I can't say.

\usepackage[papersize={4.5in, 6.2in}, width=4.13in, height=5.83in, vcenter, centering]{geometry}

• This doesn't work very well: 1. It only supports document classes book and article. 2. It has serious problems with self-defined environment 3. At least for me formulas were not displayed at all. Nov 8, 2013 at 23:25

Have a look at http://www.sp4comm.org/multipub/:

The multipub package, together with external software and some custom scripts, allows you to generate multiple publication formats from a single LaTeX manuscript; ... targeted formats are a printer-friendly PDF, a PDF version for small-screen eReaders such as the Kindle, an HTML version and an EPUB3 version. ... Multi-target compilation is achieved using a variety of applications, including LaTeXML, a style file and some custom scripts

I use Plastex with theme:minimal, then Sigil to assemble the ePub.

Plastex is open source (Python) so in theory once we understand the code, we could customize it to refine the conversion from LaTeX to HTML. But he raw conversion you get from theme:minimal is not bad at all, including math (which the earlier answer "just upload pdf to Amazon" will definitely not do), plus Bibliography, footnotes, tables (a bit ugly but fixable), \includegraphics very nice, etc.

• Now fully documented herehttp://www.amazon.com/LaTeX-ePub-MOBI-iBook-Conversion-ebook/dp/B0190O0OIY/ Dec 13, 2015 at 18:22
• customizations available here: github.com/Echeban/LaTeX2ePub Aug 27, 2018 at 15:10

Have a look at https://www.softcover.io :

Write your book using Softcover-flavored Markdown or PolyTeX (a subset of the powerful LaTeX typesetting language), and publish it as HTML, EPUB, MOBI, and PDF.

• I was curious and looked into their guide to PlyTeX: "To learn PolyTEX, I recommend running a local server and trying out LATEX commands to see if they work" Dec 18, 2017 at 12:04