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.).

System Architecture

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.


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?


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.


This section describes the errors I encountered, from memory.


Could not parse the .tex file.


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:

LaTeX2HTML Generated Image

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:

$ file -bi 22.tex 
text/x-tex; charset=utf-8


Requires the .cls file be converted to .hva. The .cls file has a dependency on memoir and enumitem, which aren't currently implemented.


Generates a number of warnings and some errors, such as:

ERROR: Error while expanding "csname" in /usr/local/texlive/2013/texmf-
   dist/tex/latex/ccicons/ccicons.sty on line 31 (sequence item 4: expected
   string or Unicode, @currname found)

Although it can initially find the .cls file, it cannot open it:

Traceback (most recent call last):
  File "/usr/local/bin/plastex", line 108, in <module>
  File "/usr/local/bin/plastex", line 54, in main
  File "/usr/local/lib/python2.7/dist-packages/plasTeX/TeX.py", line 387, in parse
    for item in tokens:

Alternative to LaTeX

I am leaning towards a non-LaTeX solution. Since XML and XSLT are already part of the architecture, creating a book using DocBook might be an easier path at this point. There are a number of supporting tools, and DocBook readily converts to formats such as EPUB2, EPUB3, HTML, and more.

  • If every converter failed, you will probably need to add some configurations for your cls file. You will surely need some configurations if you use some custom commands and if you want to output semantically logical html and not just something what looks more or less similarly to the pdf output. You will probably get best support for tex4ht, but you should provide some code examples, so we can see the errors.
    – michal.h21
    Commented Aug 12, 2013 at 18:15

1 Answer 1


I am able to compile the example including the indexes using tex4ht, resp. make4ht (see installation instructions). The result isn't much pretty, some css and configurations are needed and also images are missing: example-book.html

But it is something you can build on.

I had to make some modifications to the example-book.tex:

\title{Dan's Delicious Desserts}
\author{Dave Jarvis}

This code uses imakeidx instead of memoir for indexing. I have some configuration for imakeidx, imakeidx.4ht which I use to process the index with xindy and tex4ht to get correct links from the index to terms location:



and xindy module to process generated with this method, imakeidx.xdy:

(define-location-class "t4ht-loc" 
    ("arabic-numbers" :sep "/")
    :hierdepth 1
(define-attributes (("definition" "default")))

(merge-to "definition" "default" :drop)

(markup-locref :open "\hello{" :close "}" :class "t4ht-loc")

(markup-locref :open "\textbf{\hello{" :close "}}" :class "t4ht-loc" :attr "textbf")
(markup-locref :open "\textbf{\hello{" :close "}}" :class "t4ht-loc" :attr "definition")

Now we must instruct make4ht to run xindy, with custom make script, example-book.mk4:

Make:add("texindy", "texindy -M imakeidx -L ${lang} ${name}.idx", {lang="english"})
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:


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 Commented Aug 26, 2013 at 16:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .