# How the EPUB2 and MOBI formats can be used for typeset Math.

According to This preceding posting, I was experiencing some frustration over trying to typeset Math, for publication in EPUB2 format. EPUB3 format with MathML support was a viable alternative, though potentially hard on any readers I might have.

Well a situation exists in which either EPUB2 or MOBI can be used to publish typeset Math: Each lossless image can claim the entire width of a column of text, and each image can represent an entire equation. That way, the content of the document can alternate vertically between Text and Typeset Math.

In fact, if an author was to choose to do this, he or she could also use the Linux-based solutions ‘LyX’ , ‘ImageMagick’ , and ‘tex4ebook’ .

(Edited 1/9/2019, 15h35 … )

(As of 1/8/2019, 22h10 : )

One way to proceed would be, to create a single document using LyX, that has been formatted to whatever the maximum size of the card is, that is to hold each equation, and in which each page holds one equation. This document can be exported to a PDF-File.

(Updated 1/9/2019, 15h35 : )

For this part of the exercise it might be good, to set the Page Style to ‘Empty’ within LyX, just to suppress failed attempts to print page numbers, as well as to use ‘ps2pdf’ to generate the actual PDF-File, just to force that document to accept arbitrary sizes.

And then, the following shell script can be run on it:

#!/bin/bash

# pdf2figs.sh
# By Dirk Mittler

CONVERT='/usr/bin/convert'

if [ -z "$1" ] ; then echo "Usage: ./pdf2figs.sh [color]" echo "Input: basename.pdf" echo "Output: basename-####.png" exit 0 fi PATTERN=$'[ \'"\t\n\r]'
if [[ $1 =~$PATTERN ]] ; then
echo "Error: Parameter must not contain spaces."
exit 1
fi

if [ -n "$2" ] && [[$2 =~ $PATTERN ]] ; then echo "Error: Parameter must not contain spaces." exit 1 fi if ! [ -a "${1}.pdf" ] || [ -d "${1}.pdf" ] ; then echo "Error: Cannot locate File${1}.pdf"
exit 1
fi

$CONVERT -density 300${1}.pdf -define png:compression-level=9 \
+profile '*' -define png:format=png32 -fuzz 10 \
-crop 95% -trim +repage -transparent white \
+level-colors ${2:-black},${2:-black} ${1}-%04d.png for IMAGEX in${1}-????.png
do
if [ $(convert$IMAGEX -define histogram:unique-colors=true -format %c histogram:info:- | wc -l) -eq 1 ]
then
rm -f \$IMAGEX
fi
done



Then, whatever software one prefers to create an EPUB or a MOBI -File can be used to write text, and to insert these images. That software could be LyX again, this time together with ‘tex4ebook’ .

The problem which I was reporting in my previous posting was merely, that such realizations don’t enable my linear processing of existing LaTeX Files, to obtain the desired result.

(Update 1/9/2019, 14h25 : )

I had an earlier recommendation above, in how to craft that ImageMagick command-line, which suffered from 4 separate bugs:

1. The density with which the Native PDF Files produced by LyX were to be scanned, was not set, and was therefore extremely low. It is currently set to 300DPI, which could be correct for viewing on some modern smart-phones,
2. The images output were not anti-aliased,
3. The notation of using ‘-alpha off -fill black -alpha on’ did not succeed at filling the actual colour of the glyph,
4. Auto-Cropping was not working at first.

Error (3) proved the hardest for me to correct, and the reason it was happening was, the fact that the ‘-fill’ command requires that the image be input – not output – in RGB format, in order to work. Simply setting the output format to ‘PNG32′ doesn’t solve this problem because the input started out as grey-scale. The actual notation of ‘-alpha off … -alpha on’ works just fine.

What I finally had to do, among other things, was to use the ‘+level-colors’ command-line option, which creates a gradient between the two colours specified, and which uses the brightness of the input pixel, to select a colour along this gradient. To set both endpoints of this gradient to the same colour might seem wasteful, but at least doing it this way provides functionality, when the input isn’t in the RGB pixel-format.

Alas, only recent, up-to-date ImageMagick versions accept ‘+level-colors … ‘ .

Problem (4) above seemed to have a simple cause: The ‘-trim’ command only works, in whichever directions the page received a non-trivial ‘-crop’ parameter, which can be anything we like, as long as we don’t crop away any part of the glyph.

Dirk

## One thought on “How the EPUB2 and MOBI formats can be used for typeset Math.”

1. Andrius says:

Very clever article. I must agree with you. I was working with a v-tex and they seem just as good as you.