# Make tex4ebook support memoir's \book

Running tex4ebook on the following example

\documentclass{memoir}
\begin{document}

Some dedication text

\book{Hello}

\part{World}

\chapter{How are you}

\end{document}


produces an .epub where the \book is formatted as a likeSection, but without any prior page breaks nor any corresponding bookmarks.

What configurations are needed for tex4ebook (or tex4ht) to support memoir's \book construct, or would this require more extensive changes upstream?

There is no support for the \book command in tex4ht, so what you see is just the formatting based on the fonts used by this command. Try the following configuration code that redefines it as a sectioning command recognized by tex4ht:

\Preamble{xhtml}
\NewSection\book{\thebook}
\Configure{book}{\ifvmode\IgnorePar\fi\EndP\HCode{<h2 class="bookHead"><span class="booknumber">}\bookname\refstepcounter{book}\space\thebook\HtmlParOff\HCode{</span><span class="booktitle">}}{\HCode{</span></h2>}\HtmlParOn}{}{}
\Css{.booknumber{display:block;}}
\Configure{toToc}{book}{part}
\CutAt{book}
\begin{document}
\EndPreamble


The \NewSection command redefines \book as sectioning command recognized by tex4ht. The second argument should contain the counter command used to print the section number.

It is then necessary to configure the HTML code using \Configure{book}. This configuration is declared by \NewSection. It is a bit complicated, so I will try to describe it in more detail:

 {\ifvmode\IgnorePar\fi\EndP\HCode{<h2 class="bookHead"><span class="booknumber">}\bookname\refstepcounter{book}\space\thebook\HtmlParOff\HCode{</span><span class="booktitle">}}


The \ifvmode\IgnorePar\fi\EndP closes the current paragraph, this code is necessary for all block level elements in tex4ht. The \HCode{<h2 class="bookHead"><span class="booknumber">} starts the HTML code, the span.booknumber element is used to style the book number, as it should stay on a separate line than the book title. \bookname is Memoir's command that contains the Book string. We need to manually increment the book counter using \refstepcounter command. \HtmlParOff disables HTML paragraphs, because some spurious paragraphs were produced in my tests. The \HCode{</span><span class="booktitle">} closes the book number and opens span.booktitle for the book title.

{\HCode{</span></h2>}\HtmlParOn}


This code just closes all opened HTML elements and enables paragraphs.

\Css{.booknumber{display:block;}}


This command style the book number, it is just printed on a separate line.

 \Configure{toToc}{book}{part}


Requires \book to be included in TOC, on a same level as \part. I am not sure if it is possible to put it on a higher level without need to redefine lot of stuff.

 \CutAt{book}


This will open a new HTML page for each book.

This is the result in ebook-viewer:

• Thanks for the solution and the very clear explanation! This certainly works for me, and happy enough to have the \book and \part bookmarks on the same level. – LianTze Lim May 31 at 13:02