269

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.

Mohamed
  • 143
Andrea
  • 4,127
  • 16
    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 05: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
  • 5
    @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 06 '10 at 14:26
  • 1
    my personal preference for multi output, single source is: xml-> epub, then xml->conTeXt->pdf – Mica Oct 15 '10 at 21:28
  • Suresh, this might be of interest: http://www.dessci.com/en/reference/ebooks/EPUBMath_spec.htm Although this is an old document, I like its spirit. Offering several choices the reader can pick from is certainly the way to go. Hopefully, something similar is/will be implement. I plan on scripting something together that manages LaTeX -> EPUB all the way, but I can not give an ETA. – Raphael Jan 28 '11 at 18:33
  • EPUB 3 has been around for a couple of months. Any news? – Raphael Jan 04 '12 at 19:36
  • 7
    Also, epub3 supports MathML now (at least a subset): http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-mathml – Christoph Jan 19 '12 at 11:03
  • 4
    more recent discussion: http://tex.stackexchange.com/q/95600/6853 –  Jan 31 '13 at 18:00
  • 2
    Here is link to an instruction on how this can be achieved with tex4ebook https://d800fotos.wordpress.com/2015/01/19/create-e-books-from-latex-tex-files-ebook-aus-latex-tex-dateien-erstellen/ –  Jan 20 '15 at 16:26
  • @Volker Thank you very much for the detailed instructions! – Keks Dose Jan 20 '15 at 20:56

12 Answers12

79

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.

Flimzy
  • 107
Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
  • 16
    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... – Andriy Drozdyuk Jan 26 '12 at 00:32
  • 1
    @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 09 '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
71

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.

psygo
  • 438
g4v4g4i
  • 719
  • This is not perfect but it's the best I've seen so far. – gozzilli Jan 22 '13 at 09:04
  • 3
    +1 never checked that calibre comes with an ebook-converter as stand-alone tool before! – n611x007 Jun 13 '13 at 11:05
  • 3
    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 06 '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 '18 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. – Alex Petrosyan Feb 01 '21 at 18:57
  • Worked well for me. Thanks!. Note there maybe an OSX dependency as it assumes Calibre is installed and this the path to ebook-convert (on a Mac) from Filipe Correia should not be overlooked (i.e. /Applications/calibre.app/Contents/MacOS/ebook-convert /usr/bin/ebook-convert) – GregD Aug 11 '23 at 23:16
  • See also: https://manual.calibre-ebook.com/generated/en/ebook-convert.html – GregD Aug 11 '23 at 23:18
34

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.

frabjous
  • 41,473
  • 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
  • Note the path to the man page for ebook-covert noted above has changed to: http://manual.calibre-ebook.com/generated/en/ebook-convert.html – GregD Aug 11 '23 at 23:19
24

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
michal.h21
  • 50,697
  • Thanks for this. I used the verse package to format poetry and pandoc created a mess. This worked great. – Terri Simon Feb 05 '20 at 14:15
  • @TerriSimon that is good to hear, thanks :) – michal.h21 Feb 05 '20 at 14:36
  • 1
    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). – The Doctor Aug 17 '20 at 23:55
  • 1
    @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. – michal.h21 Aug 18 '20 at 11:05
  • this should be the main answer – alexandre1985 Apr 19 '21 at 20:43
17

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 06:16
7

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.

Aditya
  • 62,301
  • 1
    In a Usenet thread about this feature, http://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 09 '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
7

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 04 '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 '18 at 17:45
  • Here: https://github.com/Echeban/LaTeX2ePub – Echeban Aug 27 '18 at 15:09
4

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}
Adam Liter
  • 12,567
3
  • 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 08 '13 at 23:25
3

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

asmaier
  • 2,209
2

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.

Echeban
  • 107
1

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.

asmaier
  • 2,209
  • 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