# 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. –  Suresh Aug 15 '10 at 5:33
Of course this would not be suitable for a math book. But it may be suitable for a novel. –  Andrea Aug 17 '10 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. –  Charles Stewart Sep 6 '10 at 14:26
Also, epub3 supports MathML now (at least a subset): idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-mathml –  Christoph Jan 19 '12 at 11:03
more recent discussion: tex.stackexchange.com/q/95600/6853 –  Ben Crowell Jan 31 '13 at 18:00

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 (Pandoc site not accessible right now, try Google's cache)
2. My answer recommending Pandoc to SO question, User manual for Java software: In-application help + PDF, which also discusses some other useful technologies for dealing with Epub conversion.
-
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... –  drozzy Jan 26 '12 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. –  Charles Stewart Feb 9 '12 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. –  naxa Jun 13 '13 at 10:55

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. –  Andrea Aug 11 '10 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. –  Andrea Aug 11 '10 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.) –  frabjous Aug 11 '10 at 20:29

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. –  gozzilli Jan 22 '13 at 9:04
+1 never checked that calibre comes with an ebook-converter as stand-alone tool before! –  naxa Jun 13 '13 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 –  Filipe Correia May 6 '14 at 13:56

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. –  Joseph Wright Oct 17 '10 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? –  Salim Fadhley Apr 4 '11 at 23:11

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.

-
\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. –  moose Nov 8 '13 at 23:25