### Publishing mathematics in ebooks – part 1

This is the first part of what I hope will be a series of posts on my explorations of how to author maths-heavy writing in ebook format.

I have for quite some time now been annoyed with PDFs on mobile phones and tablets. Although there are some fine PDF viewers avaible, it usually still takes a lot of annoying scrolling to read a scientific paper on my phone or tablet. On the other hand, I have recently read a few novels as ebooks on my phone and my tablet and this has been an entirely different, enjoyable experience. The main difference is that the text in ebooks is re-flowable so as to make it easily adaptable to the screen size and preferred font size. This makes ebooks seem like a promising choice as an alternative to PDF for distributing scientific papers in more screen-friendly format. There is just one hurdle: mathematics

As an engineer, my papers invariably contain a lot of mathematics and I really want it to display beautifully, as I am used to from working with LaTeX and its IMO unbeatable rendering of very complicated mathematics. Let us take a simple example:

$X( \omega ) = \int_{-\infty}^{\infty} x( t ) e^{( -j \omega t )} dt$

becomes:

$X( \omega ) = \int_{-\infty}^{\infty} x( t ) e^{( -j \omega t )} dt$

Voila! (In this case, WordPress has rendered the LaTeX code into an image for us.)

In an ebook I would expect to be able to display mathematics just as beautifully. It turns out this may actually not be that far a stretch of the imagination. ePub1 (3.0) supports MathML2 which can be used to display mathematics in web pages as well. Using MathML, we can get the above formula rendered as:

X(ω)=∫−∞∞x(t)e(−jωt)dt

It seems I cannot display the above formula correctly here on WordPress since WordPress seems to convert the originally inserted MathML to a rough approximation using unicode characters. This looks much better in a stand-alone HTML document rendered in Firefox.

This leads me to my next problem, then: how well can I expect my mathematics to be displayed in different browsers/e-readers? This is a question I will get back to in another blog post.

But back to the subject here – How do I get from the LaTeX math (which I am used to writing) to the MathML code inside an ePub ebook? Fortunately, this is simple thanks to Pandoc, a wonderful text-conversion tool that does a whole lot more than just this. I can take my LaTeX document containing formulas:
 \documentclass{article} \begin{document} \title{This is a test ebook containing maths} Here is some text. Now let us try inserting maths: $X( \omega ) = \int_{-\infty}^\infty x(t) e^{(-j \omega t)} dt$ And some text again... \end{document} 
I save this document as test.tex and then run pandoc --mathml -o test.epub test.tex. This produces the ebook test.epub which I can then open in an e-reader. I have tried this in Calibre for Linux and the formula displays very nicely:

This looks promising so far. Now I need to experiment with more formulas and testing the result in different e-readers. As mentioned, I am not sure I can expect this to work in all e-readers, but there are other things we can do in the cases where the above does not work. I will get back to that in a coming post.

Please comment if you have experience with this or tips on how I could do this better.

References: