# Some further issues with index generation and tex4ebook

This is a follow-up of my previous question here.

It uses the files provided by @Michal.h21. I have modified the input file as below:

\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,noautomatic]

\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
\printbibliography \newpage

\phantomsection
\indexprologue{Concepts}
\printindex

\end{document}


The configuration and build files are the same as the previous question and again provided by @Michal.h21.

The main issues are:

1. The \indexprologue command is correctly handled in the pdf:

However, it is ignored in the tex4ebook run.

1. I think there is inconsistent behavior in the intoc option and the \addcontentsline{toc}{chapter}{Index} option. The intoc option is ignored in the tex4ebook run.

2. The index link in the TOC points to a blank page rather than to where the index header is.

It is correct in the pdf file.

1. The use of an automatic option in the makeindex command doesn't produce hyperlinks:

Using the noautomatic option works. This is not a big issue, but it is different to how a normal latex run behaves and it seems strange.

Have others experienced these issues, and are there any suggested fixes or ways to get consistent results?

I suspect this will fall on @Michal.h21 and so I will thank him in advance.

The previous version of configuration for imakeidx package didn't take into account some features of this package, it used the default tex4ht configuration to handle the \printindex command. We can use the modified version of imakeidx definition to get support for some of these features.

This is the new version of imakeidx.4ht file:

\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}%
}
\newcommand\@stepindexcnt{\stepcounter{indexcnt}}
\newcommand\hello[1]{\@hello#1}
\def\@hello#1/{%
}

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

\newcount\idxkwdcnt
\newcommand\idxkeyword[1]{%
% 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}

\renewenvironment{theindex}%
\imki@idxprologue
\global\let\imki@idxprologue\relax
\parindent\z@
\parskip\z@ \@plus .3\p@\relax
\raggedright
\let\item\@idxitem
\imki@othercode}
{%
}

\let\indexspace\relax

\ConfigureEnv{theindex}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="theindex">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}

\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*}


The important change is this:

\renewenvironment{theindex}%
\imki@idxprologue
\global\let\imki@idxprologue\relax
\parindent\z@
\parskip\z@ \@plus .3\p@\relax
\raggedright
\let\item\@idxitem
\imki@othercode}
{%
}

\let\indexspace\relax

\ConfigureEnv{theindex}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="theindex">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}


The \reneenwironment{theindex} will take into account the \indexprologue and TOC support, \ConfigureEnv{theindex} will add <div class="theindex"> element around the index contents.

With this change, you don't need to use \addcontentsline{toc}{chapter}{Index} in your document anymore, the link from TOC links correctly to the index.

Regarding your last question, the automatic option of imakeidx doesn't work because you need to use different options when you compile with pdflatex and with tex4ht. This is why xindy is called from the build file. The other reason is that LaTeX is called multiple times in each tex4ebook run and it doesn't make sense to compile the index in each of these calls.