Skip to main content
All CollectionsAuthor Help
Methods for Writing and Displaying Math in Ebooks and HTML
Methods for Writing and Displaying Math in Ebooks and HTML

How does Leanpub produce math in ebooks generated from plain text book manuscripts?

Leanpub Support avatar
Written by Leanpub Support
Updated over a week ago
Methods for Writing and Displaying Math in Ebooks and HTML

In the ebook files produced by our book generators, Leanpub renders math as a PNG with a white (not transparent) background.

This may seem thoughtless and lazy, but it's actually the result of a lot of thought and experimentation!

In this brief article we're going to set out our thoughts on the matter.


Math is commonly typed in LaTeX, AsciiMath, Mathematica, and many other languages.

On Leanpub, LaTeX math and AsciiMath are supported for the production of ebooks in the PDF, EPUB, and web formats. (We also produce a Print-Ready PDF to use for the production of print books through print-on-demand services.)

When an author's plain text Leanpub manuscript is converted into a PDF file, the manuscript is parsed using Leanpub's Haskell-based custom-built Pandoc fork for Markua 0.30.

LaTeX is produced by this process, which is rendered as a PDF using xelatex.

When the manuscript is converted to an EPUB file, a similar process is used to generate HTML, which is output as EPUB.

Leanpub's Markua-processed EPUB books still support LaTeX math, despite HTML not having the ability to render LaTeX.

So, how can Leanpub display math in EPUB books?

MathML

Rendering math on the web is difficult. One universal HTML-supported standard is the XML-extended MathML.

MathML can be converted from LaTeX math. Firefox fully supports MathML, Safari questionably supports it, and Chrome has zero support. Chrome developers decided continuing support for MathML was unreasonable due to security concerns and a lack of consistent support. This means MathML won't be rendered in Chrome, and there is no other way of directly embedding math in pure HTML.

MathJax

MathJax is "a JavaScript display engine for mathematics that works in all browsers." By inserting a script source to MathJax at the top of the HTML of a page, MathML can be displayed in HTML directly or via SVGs in any browser. A similar tool called KaTeX is used for similar purposes.

While these tools solve the issues they're made for, they have their downsides.

Leanpub isn't willing to require readers to wait a few seconds to load a math-heavy page. Older and slower devices should be able to read Leanpub's books without waiting with every page flip.

As well, relying on MathJax for Leanpub's EPUB reader is making the false assumption that all other EPUB readers support MathJax. So, MathJax isn't for Leanpub.

SVGs

LaTeX is mainly output as two different possible formats: DVI or PDF.

There are many tools which convert DVI and/or PDF to PNG, JPEG, SVG, etc. Some tools even hide the middleman and directly convert LaTeX to other formats (tex2png, tex2svg, and many others).

The positive attributes of an SVG are their vector image properties such as scalability, the ability to dynamically change color with the css style fill: currentColor; to support dark mode, and the ability to inherit height/width attributes.

However, for our specific purposes, the downsides outweigh their benefits:

  • Slow book generation time (about 130 seconds to generate a book with 100 SVGs vs 16 seconds without SVGs)

  • Inkscape often produces an incorrect SVG output:

Correct SVG rendering

Incorrect SVG rendering

  • Inkscape's PDF to SVG generation crashed periodically, adding extra generation time

  • Complex SVG formulas can increase page loading time

So, Leanpub reluctantly decided against SVGs.

PNGs

One extremely reliable universal file format is a PNG.

LaTeX math that is converted from PDF to PNG, and displayed with black text on a white background, will always be legible.

To be clear, PNGs aren't always the prettiest option. Inline math as a PNG is slightly elevated above the text and looks out of place, and PNGs with a white background in dark mode stand out a bit too much.

However, given our decisions here, at least we can guarantee any reader on any device could buy a Leanpub-generated ebook with math, and still be able to read and understand all of its equations.

Thanks for reading!


If you have any feedback or questions about this article, please email the Leanpub team about it at hello@leanpub.com!

If you have any questions or thoughts on writing and self-publishing with Leanpub, please join our global community of authors in our Authors Forum here!

Are you interested in self-publishing, and creating your first Leanpub book? Here are some quick tutorials for our most popular writing modes: http://help.leanpub.com/en/articles/3088382-quick-walkthroughs-for-getting-started-on-a-leanpub-book

Are you looking for great deals on Leanpub ebooks, ebook bundles, and courses? Sign up for our Weekly and Monthly newsletter sales here!

Subscribe to our YouTube channel here: https://www.youtube.com/leanpub

Please subscribe to our YouTube channel here: https://www.youtube.com/leanpub

You can also follow Leanpub in lots of other places!

Did this answer your question?