4

I'm having trouble replicating the work of others in getting a LaTeX document properly rendering in the epub format. Specifically I'm having trouble with paragraph spacing, centering, and newpage/clearpage.

.tex file

\documentclass{book}
\usepackage{lipsum}
\begin{document}
\parindent=7pt
\parskip=7pt
\title{\Huge\textbf{Latin Gibberish\normalsize}}
\author{Ralph}
\date{1440}
\maketitle
\begin{center}This stuff should be centered\end{center}
\lipsum[1]

\lipsum[1]
\newpage
\lipsum[1]
\clearpage
\end{document}

.cfg file

% save the clearpage before it is redefined by tex4ht
\let\oldclrearpage\clearpage
% define macro for newpage insertion
\def\mypagebreak{\Configure{newpage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="newpage"></div>}}}
\Preamble{xhtml}
% define it for \newpage
\mypagebreak
\Css{.newpage{page-break-before:always;}}
% modify \Configure{BODY} so our confiurations work on all extracted pages
\catcode`\:=11
\append:def\a:BODY{\def\clearpage{\bgroup\mypagebreak\oldclrearpage\egroup}}
\append:def\b:BODY{\gdef\clearpage{\relax}}
\catcode`\:=12
\begin{document}
\EndPreamble

And here's the end of what I get when I try tex4ebook -c config.cfg test.tex:

zip error: Nothing to do! (try: zip -qXr9D test.epub . -i OEBPS) Pack outputdir 3072 Copy generated epub Copy: cp "test-epub/test.epub" "test.epub"

2

The issue is caused by this line in the .cfg file:

 \append:def\b:BODY{\gdef\clearpage{\relax}}

It redefines \clearpage at the end of HTML page (tex4ebook write separate pages for chapters, sections, etc.), because spurious <div class="newpage"></div> is placed after </html> otherwise. That results in invalid HTML file.

The problem is that tex4ebook calls special routines to write metadata files in \AtEndDocument LaTeX hook. It seems that this configuration disables execution of that hook and the metadata files aren't written in turn.

The better configuration would be to redefine \clearpage back to it's original meaning and configure newpage to produce nothing. The corrected .cfg file:

% save the clearpage before it is redefined by tex4ht
\let\oldclrearpage\clearpage
% define macro for newpage insertion
\def\mypagebreak{\Configure{newpage}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="newpage"></div>}}}
\Preamble{xhtml}
% define it for \newpage
\mypagebreak
\Css{.newpage{page-break-before:always;}}
% modify \Configure{BODY} so our confiurations work on all extracted pages
\Configure{@BODY}{\def\clearpage{\bgroup\mypagebreak\oldclrearpage\egroup}}
\Configure{@/BODY}{\global\let\clearpage\oldclrearpage\Configure{newpage}{}}
\begin{document}
\EndPreamble

I've found that \Configure{@BODY} and \Configure{@/BODY} can be used to insert material at the beginning and at the end of the page, so we could simplify the configuration a little bit.

The generated HTML file:

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"  
> 
<head> <title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" /> 
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)" /> 
<!-- charset=utf-8,epub,html,xhtml --> 
<meta name="src" content="test.tex" /> 
<link rel="stylesheet" type="text/css" href="test.css" /> 
</head><body 
>
<!--l. 4--><p class="noindent" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum
ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu
neque. Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet
tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra
ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu,
accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
</p>
   <div class="newpage"></div>

<!--l. 6--><p class="indent" >   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris.
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula
augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet
tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra
ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu,
accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
</p>
   <div class="newpage"></div>

<!--l. 8--><p class="indent" >   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris.
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula
augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem.
Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet
tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra
ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu,
accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
</p>
   <div class="newpage"></div>
</body></html>

The updated sample as displayed in Calibre:

enter image description here

  • Thanks! Now how do I get paragraph breaks and centering working? – Frotz Jan 5 '17 at 12:37
  • @Frotz could you update your question with some example of what do you have in mind? Paragraph breaks should be handled with <p> tags, with centering, it is a little bit more complicated, you can't use \centering switch, but center environment instead – michal.h21 Jan 5 '17 at 13:41
  • I've updated the .tex with \title, \author, \date, a center environment, and a paragraph break. The title gets centered correctly, but not the author or date. – Frotz Jan 5 '17 at 14:30
  • @Frotz I've got centered contents of \maketitle and center environments in Calibre, see the image in the updated answer. What reader do you use? It is possible that it doesn't support CSS (I know that fbreader didn't support CSS in the past) – michal.h21 Jan 5 '17 at 15:42
  • Ya. I was using fbreader (installed with Debian 8's repos). Calibre is showing things correctly now. Thanks! – Frotz Jan 6 '17 at 1:06

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.