15

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

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.

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:

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

hevea

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

plastex

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>
    main(sys.argv)
  File "/usr/local/bin/plastex", line 54, in main
    tex.parse()
  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 Aug 12 '13 at 18:15
3

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:

\usepackage[noautomatic]{imakeidx}
\makeindex[name=flatingred]
\makeindex[name=catingred]
\title{Dan's Delicious Desserts}
\date{2013-06-09}
\author{Dave Jarvis}
\frontcoverphoto[]{}
\begin{document}
  \let\index\Index

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:

\usepackage[]{etoolbox}
\newcounter{indexcnt}
\let\@Index\@index
\patchcmd{\@Index}{\@wrindex}{\@Wrindex}{}{}
\let\@Wrindex\@wrindex
\patchcmd{\@Wrindex}{\thepage}{\theindexcnt/}{}{}
\let\xIndex\index
\patchcmd{\xIndex}{\@index}{\@Index}{}{}
\patchcmd{\xIndex}{\@index}{\@Index}{}{}

\pretocmd{\xIndex}{\@stepindexcnt\@indexanchor}
%\patchcmd{\index}{\@index}{\@Index}{}{}
%\patchcmd{\xIndex}{\@index}{\@Index}{}{}
%\let\protected@iwrite\protected@write
%\let\@index\@Index
%\patchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
\patchcmd{\@Wrindex}{\protected@write}{\protected@iwrite}{}{}
\let\Index\xIndex
%\show\Index
\newcommand\@indexanchor{%
\edef\idx@anch{idx-anch\theindexcnt}%
\Link[]{}{\idx@anch}\EndLink%
%\Ref{(idx-link-\idx@anch)}{\FileName/\idx@anch}
}
\newcommand\@stepindexcnt{\stepcounter{indexcnt}}
\newcommand\hello[1]{\@hello#1}
\def\@hello#1/{%
\Link{idx-anch#1}{}#1\EndLink%
}

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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.