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.

  • 12
    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
  • 3
    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
  • 3
    @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
  • 4
    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
  • 3
    more recent discussion: tex.stackexchange.com/q/95600/6853 – Ben Crowell Jan 31 '13 at 18:00

12 Answers 12

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 to SO question, User manual for Java software: In-application help + PDF, which also discusses some other useful technologies for dealing with Epub conversion.
  • 7
    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. – n611x007 Jun 13 '13 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. – gozzilli Jan 22 '13 at 9:04
  • 3
    +1 never checked that calibre comes with an ebook-converter as stand-alone tool before! – n611x007 Jun 13 '13 at 11:05
  • 2
    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
  • On Mac, install with brew cask install calibre to have the aliases created as part of the setup. – xgMz Feb 18 at 18:25

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
  • 1
    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

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
  • 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. – Echeban Dec 13 '15 at 18:21
  • @Echeban - Do you have this on a gist / github anywhere? – Andy Clifton Feb 11 at 17:45
  • Here: github.com/Echeban/LaTeX2ePub – Echeban Aug 27 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.

  • 1
    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? – Charles Stewart May 9 '11 at 12:38
  • 1
    @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. – Aditya May 10 '11 at 18:30
  • 2
    Context MkIV can generate epub. – Martin Schröder Oct 4 '11 at 22:18

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
  • 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. – Martin Thoma Nov 8 '13 at 23:25

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}

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/ – Echeban Dec 13 '15 at 18:22
  • customizations available here: github.com/Echeban/LaTeX2ePub – Echeban Aug 27 at 15:10

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

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" – Nichlas H. Dec 18 '17 at 12:04

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.