# Customizing tex4ht to generate html to convert to epub with Calibre

I'm trying to prepare html for conversion to an epub and have encountered a couple of problems that I would appreciate help with. Hope mixing a couple of related questions in a single post is OK.

The easiest is the quotation environment doesn't indent both margins (and not enough on the left). I've written a .cfg file and think I have that one solved. Here's what I'm using:

\Preamble{html}
\begin{document}
\Css { .quotation { margin-bottom:0.25em; margin-top:0.25em; margin-left:4em; margin-right:4em } }
\EndPreamble


The next is figures and tables. tex4ht inserts an hr tag before and after the table. This is not part of the css, it's a separate tag generated by tex4ht. I haven't found where one customizes this. I can delete these in emacs from the html, but that doesn't seem the right way to do this. Here's a fairly minimal example showing the rules (and the quotation indent as well).

\documentclass{book}

\begin{document}

\chapter{Simple Example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec
odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla
quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent
mauris. Fusce nec tellus sed augue semper porta. Mauris
massa. Vestibulum lacinia arcu eget nulla.

\begin{quotation}
Default book style icreases the left and right margins for quotations
by a noticeable amount. The css generated by tex4ht only increases the
left margin by one em. The attached .cfg increases this to 4 em.

\end{quotation}

Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus
vitae pharetra auctor, sem massa mattis sem, at interdum magna augue
eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et
ultrices posuere cubilia Curae; Morbi lacinia molestie dui.
amet mauris.

\begin{table}
\begin{center}
\hbox{
\begin{tabular}{|l|c|c|}\hline
entry1&entry2&entry3\\
entry4&entry5&entry6\\ \hline
\end{tabular}}
\end{center}
\caption{a caption}
\end{table}

Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer
lacinia sollicitudin massa. Cras metus. Sed aliquet risus a
tortor.

\end{document}


[Edit 2] I'm pretty sure the hr tag is being generated for all floats. By trial and error I found my way to the html4.4ht file, which contains this code:

\Configure{float}
{\ifOption{refcaption}{}{\csname par\endcsname\ShowPar \leavevmode}}
{\:xhtml{\IgnorePar\EndP}\HCode{<hr class="float"\xml:empty><div
class="float"\Hnewline>}}
{\ifvmode \IgnorePar \fi\EndP
\HCode{</div><hr
class="endfloat" />}\csname par\endcsname\ShowPar}


On the assumption that the \HCode macro defines the html output, I removed the hr tag:

\Configure{float}
{\ifOption{refcaption}{}{\csname par\endcsname\ShowPar \leavevmode}}
{\:xhtml{\IgnorePar\EndP}\HCode{<div
class="float"\Hnewline>}}
{\ifvmode \IgnorePar \fi\EndP
\HCode{</div>}\csname par\endcsname\ShowPar}


When I run htlatex with this config file, I get an error on a \begin{center} that is within a begin{table}. Without this change to the config file it runs fine, it has something to do with the edit, but since I don't really understand what's going on here, I'm utterly baffled as to how to fix it.

[End Edit 2]

[Edit 3]

The problem is not the change to the \Configure statement, but the presence of the \Configure statement in the .cfg file. Even if I copy the \Configure statement verbatim, I still get an error when \begin{center} is encountered. The error is "Missing \$ inserted"

[End Edit 3]

The captions for figures and tables are not on the float page, but follow immediately on the next page after conversion to epub. Is this an html problem that can be fixed in tex4ht, or is it a problem in how Calibre is interpreting the html?

(updating to the current version of Calibre fixed this)

Should I be producing xhtml instead of html?

I'd be grateful for a pointer to good documentation about customizing tex4ht.

• could you please provide minimal example covering problems you describe? and also the config file? for conversion to epub, you may try github.com/michal-h21/tex4ebook it generates epub directly, without need to use calibre – michal.h21 Jun 30 '14 at 6:36

You've identified code for float configuration correctly. It doesn't work in the config file because of \:xhtml command. Macros with : in name work only in .4ht files, because they have different \catcode here. If you want to use it outside 4ht files, you need to set \catcode of : character to category 11, so it can be used in command names.

\Preamble{html}
\def\makecolonletter{%
\edef\makecolonother{\catcode\string\string : \the\catcode\string\:\relax}
\catcode\:=11\relax%
}
\def\makecolonother{}

\makecolonletter
\Configure{float}
{\ifOption{refcaption}{}{\csname par\endcsname\ShowPar \leavevmode}}
{\:xhtml{\IgnorePar\EndP}\HCode{<div
class="float"\Hnewline>}}
{\ifvmode \IgnorePar \fi\EndP
\HCode{</div>}\csname par\endcsname\ShowPar}
\makecolonother
\begin{document}
\Css { .quotation { margin-bottom:0.25em; margin-top:0.25em; margin-left:4em; margin-right:4em } }
\EndPreamble


I've defined commands \makecolonletter and \makecolonother which take care of setting catcodes. Now you can use \makecolonletter before \Configure commands where commands with colon in name are used, don't forget to use \makecolonother to restore \catcode of colon back.

It seems that \:xhtml expands it's argument only when xhtml mode is on, otherwise, argument is discarded.

As you want to produce epub, you should definitely use xhtml mode, because it is the format epub` is based on.