# Background

The diagram below shows an architecture for generating books using a dynamic preamble. The top part of the image shows the database relations between a book theme and its preamble. The top set of numbered items represent:

1. A LaTeX snippet, later collated into a preamble, stored in a table.
2. Various snippets controlling aspects of the final document (e.g., colours, fonts, layout).
3. A theme comprised of a default set of snippets (e.g., A, B, & C below).

When the user selects a theme, a database query dynamically creates a preamble. The user can select a theme then tweak it by overriding the default snippets, as illustrated by the browser wire-frame (i.e., palette, font, photo, etc.).

# Output Format

The user creates a book as shown in the diagram. The system performs the following actions when the user clicks the "download" button:

1. Generates an XML document to gather the information for the book.
2. Dynamically creates the LaTeX preamble based on theme and tweaks.
3. Transforms the XML into a .tex file, injecting the LaTeX preamble.
4. Calls pdflatex to create a .pdf, complete with multiple indexes, table of contents, etc.
5. Returns the PDF to the user.

Although the LaTeX preamble is almost all database-driven, parts are coded in XSLT, such as the \documentclass declaration. This is trivial to parameterize.

# Question

I would like to revise the architecture to allow users to create version of their documents for different devices and file formats (e.g., ePub, Mobi, Kindle, PDF). There is no math, but any images must be fixed on the page (i.e., they cannot float; here, definitely).

Given the constraints of such an architecture, what approach should I take to convert the LaTeX documents for various eReaders such that the output produced is as high a quality as possible, and as true to the PDF layout as possible?

# Efforts

I have tried to convert the LaTeX, as it stands, directly to XHTML. This failed in spectacular ways using Hevea, latex2html, pandoc, and mk4ht (htlatex), mostly due to the dependency on the .cls file.

My thoughts are that I'm going to have to do the following, at least:

• Allow the user to choose ePub format (prior to "clicks" in the diagram).
• Update the XSLT To remove the reference to the .cls file (for ePub format).

After that...?

# Source Code

The .cls source code is part of an open source project. There is an example .tex file in the repository, along with example themes. Note that the images in the example .tex file are not in the repository, so generating a PDF will require minor modifications.

Also, there is an installation script (install.sh) that drops dependent artefacts into specific directories. It probably won't work for other LaTeX installations without modification.

# Errors

This section describes the errors I encountered, from memory.

## pandoc

Could not parse the .tex file.

## latex2html

Could not find the .cls file, despite it being located in TEXMFHOME. The pdflatex program can generate the PDF without any issues. Generates many PNG images that are difficult to read, even though the content is mostly text:

## mk4ht & htlatex

Was able to generate HTML documents, but there appear to be some UTF8 encoding issues (the output is gibberish). There are also image scaling issues (i.e., the images didn't shrink). Not sure why the result is gibberish, as it is UTF8-encoded:

Make:htlatex {}
Make:texindy {name="flatingred"}
Make:texindy {name="catingred"}
Make:htlatex {}


Now we are able to process the file with command:

make4ht -u example-book


Last problem is huge amount of empty images, which are generated by tex4ht. I have found that this is caused with wallpaper package. So you must edit the file recipe-book.cls to comment out wallpaper.sty when tex4ht is running:

\RequirePackage{endnotes}
\RequirePackage{afterpage}
\ifdefined\HCode
\else
\RequirePackage{wallpaper}
\fi
\RequirePackage{pdflscape}
\RequirePackage{wrapfig}
\RequirePackage{filecontents}
\RequirePackage{microtype}
\RequirePackage{bookmark}
\RequirePackage{relsize}


You will probably need to add some configurations for emulating missing functionality, but the example compiles without errors for me.

• After running into error upon error trying to convert the LaTeX document to EPUB, I switched the architecture to ConTeXt. wiki.contextgarden.net/Epub Aug 26 '13 at 16:29