2

I am trying to set up a book template for tex4ebook using memoir and Texlive.

The MWE that has everything I want is:

\documentclass[ebook,12pt,oneside,openany]{memoir}

\begin{filecontents}{test.bib}
@book{Hiemenz,
author={Hiemenz},
title={Polymer Chemistry}
}
\end{filecontents}

\usepackage{lipsum}

\usepackage[backend=biber,hyperref=auto,backref=true]{biblatex}
\bibliography{test}

\usepackage{imakeidx}
\makeindex[intoc=true,columns=1]

\usepackage[hyperindex=true]{hyperref}

\title{Book}
\author{Author}
\date{}

\begin{document}

\maketitle

\frontmatter

\tableofcontents \newpage

\mainmatter

\chapter{C1}

aword\index{aword}

\lipsum \newpage

Here comes a quotation \cite{Hiemenz}.

\chapter{C2}

bword\index{bword}

\lipsum \newpage

\backmatter

\phantomsection
\addcontentsline{toc}{chapter}{Bibliography}
\printbibliography \newpage

\phantomsection
\printindex

\end{document}

Using pdflatex produces a correct result.

pdflatex output

Using tex4book causes some problems.

The build.mk4 file is:

 Make:add("biber","biber ${input}")

 Make:htlatex {}
 Make:biber {}
 Make:htlatex {}
 Make:htlatex {}
 Make:htlatex {}

The cfg file is:

% 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}
\CutAt{section}
%
% enforce newpage
%
% 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

This doesn't produce a correct ebook. The Index entry is missing in the table of contents and the index pages are not hyperlinks.

tex4ebook output

Is there any way to get the tex4ebook version to work correctly?

Thank you.

Edit 1

I forgot to put in \mainmatter before the first chapter. It's now included. I've left the rest of the input file the same to allow a comparison with @michal.h21 excellent solution.

Edit 2 - Extra questions

Question 1:

I want to include a cover (call it cover.jpg) and have it show up correctly on the Kindle version with a link in the table of contents so that the toc in the ebook picture looks like:

Cover

Beginning [Comment: Where should this link to in sample.tex?]

Rest of table of contents as in original question

I've looked at here and here but I'm not sure what's the best way to do this and avoid the double cover issue discussed on these pages.

Question 2:

The two web pages above also discuss various ways to tidy the html code generated in the build process. What is the recommended way to do this?

In summary, what additional changes are necessary to the revised sample.tex and build.mk4 from @michal.h21 and my sample.cfg to make all this work? [imakeidx.4ht from @michal.h21 remains as is.]

I hope that these additional changes will provide a complete template for Kindle ebooks that other users can easily adapt for their books.

Thank you.

4

The problem with index is that there are no pages in the HTML, so it doesn't make sense to use them. Instead, we can number each \index use and insert link destination in that place. This way, we can go directly to the place where the index entry has been used.

We will use xindy, because it is more flexible than makeindex and it supports additional languages. We will also use the fact that you use imakeidx package, because it enables us to modify the information which should be included in the index easily.

First of all, you need to modify your source file, because you don't want automatic index creation (maybe you want, but it doesn't make sense to call indexing command in every LaTeX run), and we need to include the index in the TOC:

\documentclass[ebook,12pt,oneside,openany]{memoir}

\begin{filecontents}{test.bib}
@book{Hiemenz,
author={Hiemenz},
title={Polymer Chemistry}
}
\end{filecontents}

\usepackage{lipsum}

\usepackage[backend=biber,hyperref=auto,backref=true]{biblatex}
\bibliography{test}

\usepackage{imakeidx}
\makeindex[intoc=true,columns=1]

\usepackage[hyperindex=true]{hyperref}

\title{Book}
\author{Author}
\date{}

\begin{document}
\ifdefined\HCode
  \phantomsection
  \addcontentsline{toc}{chapter}{Cover}
  \coverimage{cover.jpg}
\fi

\maketitle

\frontmatter

\tableofcontents \newpage

\mainmatter

\chapter{C1}

aword\index{aword}

\lipsum \newpage

Here comes a quotation \cite{Hiemenz}.

\chapter{C2}

bword\index{bword}
\lipsum \newpage

\backmatter

\phantomsection
\addcontentsline{toc}{chapter}{Bibliography}
\printbibliography \newpage

\phantomsection
\printindex

\end{document}

Then config file for imakeidx, imakeidx.4ht:

\RequirePackage[]{etoolbox}
\RequirePackage{gettitlestring}
\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
\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%
}


\GetTitleStringSetup{expand}
\GetTitleStringDisableCommands{%
  \renewcommand\LaTeX{LaTeX}
}

\newcount\idxkwdcnt
\LinkCommand\IdxKWLink{span,href,id}
\newcommand\idxkeyword[1]{%
  \global\advance\idxkwdcnt by 1\relax%
  % use tags only one time
  \GetTitleString{#1}
  \ifcsdef{idxkw\GetTitleStringResult}{}%
  %{\Tag{idxkw#1}{idxkw\the\idxkwdcnt}}%
  {\expandafter\Tag\expandafter{idxkw\GetTitleStringResult}{idxkw\the\idxkwdcnt}}%
  \csgdef{idxkw\GetTitleStringResult}{x}
  \a:IdxKW{}{idxkw\the\idxkwdcnt}#1\b:IdxKW
}


\NewConfigure{IdxKW}{2}
\Configure{IdxKW}{\IdxKWLink}{\EndIdxKWLink}


\newcommand\idxlocator[1]{\Link{idx-anch#1}{}#1\EndLink}%

\begin{filecontents*}{\jobname.xdy}
(define-attributes (("default" "definition" "textbf")))
( define-location-class "sec-num" :var
                        ("arabic-numbers"
             :sep "." "arabic-numbers"
             :sep "." "arabic-numbers"
             :sep "." "arabic-numbers"
             :sep "." "arabic-numbers"
))
(require "texindy.xdy")
(markup-keyword :open "\idxkeyword{" :close "}" )
(markup-locref :open "\idxlocator{" :close "}")
(markup-locref :open "\textbf{\idxlocator{" :close "}}"  :attr "textbf")
(markup-locref :open "\textbf{\idxlocator{" :close "}}"  :attr "definition")
\end{filecontents*}

It contains configuration for index destinations and xindy helper module.

As last thing, we must add xindy support to the .mk4 file:

Make:add("biber","biber ${input}")
Make:add("xindy", function(par)
  -- par.encoding  = par.encoding or "utf8"
  -- par.language = par.language or "english"
  par.idxfile = par.idxfile or par.input .. ".idx"
  local modules = par.modules or {}
  local t = {}
  for k,v in ipairs(modules) do
    t[#t+1] = "-M ".. v
  end
  par.moduleopt = table.concat(t, " ")
  local xindy_call = "xindy -L ${language} -C ${encoding} ${moduleopt} ${idxfile}" % par
  print(xindy_call)
  return os.execute("xindy -L ${language} -C ${encoding} ${moduleopt} ${idxfile}" % par)
end, {modules = {"texindy"}, language = "english", encoding = "utf8"})

if mode=="draft" then
  Make:htlatex {}
else
 Make:htlatex {}
 Make:biber {}
 -- xindymodule = Make.params["input"]
 Make:xindy {modules = {"sample"}}
 Make:htlatex {}
 Make:htlatex {}
 Make:htlatex {}
end

The Make:xindy command can accept some additional arguments, namely language, encoding, idxfile and modules table. We can use the default values, we only need to pass the module generated by imakeidx.4ht, using

 Make:xindy {modules = {"sample"}}

The module is named as \jobname.xdy, we should post it without the extension. My file is named sample.tex, thus the module name is sample.

This is the result:

enter image description here


Edit:

You can add the cover image to the document using \coverimage command. It assumes that it is in png format, so you need to add

 \Configure{CoverMimeType}{image/jpeg}

to the .cfg file if you want to use jpg. You can use the trick with \phantomsection and \addcontentsline to put it to the TOC. The \coverimage is defined by tex4ebook, so you must use it only when you compile the document with tex4ebook, for example:

\ifdefined\HCode
  \phantomsection
  \addcontentsline{toc}{chapter}{Cover}
  \coverimage{cover.jpg}
\fi
  • @Michael.h21 Thank you very much for a complete solution to my problem. I have only one minor question: How do I get rid of the "A" and "B" headings in the index so that it just has aword, bword etc like the pdf? Finally, I hope that your solution is useful to other users of tex4ebook. With this in mind, may I please suggest that your excellent answers to tex4book question be stored on the tex4ebook site so that users can find them quickly? Perhaps in a faq section? Thanks again. – user41974 Jan 12 '17 at 3:52
  • @Michael.h21 I found a solution for the A and B headers at tex.stackexchange.com/questions/264072/sorting-author-index-made-using-biblatex-removing-letter-group-headings. I haven't resolved my extra questions yet. Thanks. – user41974 Jan 16 '17 at 3:52
  • @user41974 I've added the solution for the cover image, it is included in the document and in the toc, but only in the HTML mode, hence \ifdefined\HCode. Regarding your last question, you can execute tidy command on all your pages with -t command line option, this should suffice. You can also use make4ht filters, but they usual deal with unusual cases. – michal.h21 Jan 16 '17 at 14:18
  • @Michael.h21 Thanks very much for your additional clarification. I think I have everything straight now. – user41974 Jan 17 '17 at 5:44
  • There's also students.engr.scu.edu/~sschaeck/latexforebooks. It has a downloadable zip with ready project structure. – Jim Schubert May 21 '17 at 12:27

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.