1

The MWE below creates and index with hyperlinks on pages in the generated PDF. The index also appears in the TOC. However, the epub generated with tex4ebook looks great and the index appears. However, the index is not listed in my TOC and its pages are not hyperlinked.

\documentclass{memoir}
\let\ordinal\relax

\usepackage{makeidx}
\usepackage{hyperref}
\makeindex

\begin{document}
\tableofcontents*
\chapter{The First Chapter}
Here is some text.\index{text}
\chapter{The Second Chapter}
Here is some text.\index{text}


\backmatter
\printindex

\end{document}

Here's the PDF: enter image description here

Here's the EPUB: enter image description here

1 Answer 1

2

TeX4ht doesn't support Memoir's indexing system yet, here is a fix for that, memoir.4ht:

% memoir.4ht (2021-01-29-14:14), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2021 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2021-01-29-14:14}

\input book.4ht
\input verse.4ht
\input booktabs.4ht
\let\columnlines\empty

\let\rm\empty
\let\sf\empty
\let\tt\empty
\let\bf\empty
\let\it\empty

\renewcommand*{\@memoldfonterr}[3]{}
\renewcommand*{\@memoldfontwarn}[3]{}


\def\@chapter[#1]#2{%
   \gHAdvance\:mpNum by 1
\HAssign\minipageNum=\:mpNum \relax
%
   {\SkipRefstepAnchor \let\addcontentsline\:gobbleIII\no@chapter[#1]{}%
    \global\let\f@rtoc\f@rtoc
    \ifx\f@rtoc\empty\gdef\f@rtoc{#2}\fi
    \global\let\f@rhdr\f@rhdr  }%
   \HtmlEnv   \Toc:Title{\f@rtoc}\:chapter{#2}}
\def\:tempc{\@ifstar\o:tableofcontents:\o:tableofcontents:}
\HLet\tableofcontents\:tempc
\Def:Section\book{\thebook}{#1}
\NewConfigure{abstracttitle}{2}
\newenvironment{abstracttitle4ht}{\a:abstracttitle}{\b:abstracttitle\par\noindent}
% get rid of all extra <span> elements introduced by smaller font size
% \renewcommand{\abstracttextfont}{\normalfont}
% \renewcommand{\abstractnamefont}{\normalfont}
% use our dummy environment to insert tags around the abstract title
\renewcommand{\absnamepos}{abstracttitle4ht}

\NewConfigure{poemline}{2}
\NewConfigure{legend}{2}
\renewcommand\legend[1]{\a:legend #1\b:legend}
\@ifpackageloaded{hyperref}{%
\ifHy@hyperfootnotes
\Hy@hyperfootnotesfalse
\newcounter{Hfootnote}
\fi
}{}

\ifx \memgobble\:UnDef
   \pend:defI\getthelinenumber{%
   \let\sv:thepoemline\thepoemline
   \pend:def\thepoemline{\a:poemline}%
   \append:def\thepoemline{\b:poemline}}
\append:defI\getthelinenumber{\let\thepoemline\sv:thepoemline}

\else
   \pend:defII\getthelinenumber{%
   \let\sv:thepoemline\thepoemline
   \pend:def\thepoemline{\a:poemline}%
   \append:def\thepoemline{\b:poemline}}
\append:defII\getthelinenumber{\let\thepoemline\sv:thepoemline}

   \let\no@Msect\M@sect
\NewConfigure{@sec @ssect}[1]{%
   \def\rdef:sec##1{#1\csname no@##1\endcsname}}
\:CheckOption{sections-}     \if:Option
   \Configure{@sec @ssect}{}
\else
   \Configure{@sec @ssect}{%
      \let\sv:Sc\:Sc \let\sv:sect\@sect \let\sv:ssect\@ssect
\let\sv:Msect\M@sect
\let\:Sc\:gobble
      \def\M@sect##1##2##3##4##5##6[##7][##8]##9{%
   \ifx \o:@seccntformat:\:UnDef
  \let\o:@seccntformat:\@seccntformat
\fi
\let\@seccntformat\:gobble
%
   \let\M@sect\no@Msect   \xdef\c:secnumdepth{##2}%
   {\SkipRefstepAnchor \let\addcontentsline\:gobbleIII \let\mark\:gobble
    \no@Msect{##1}{##2}{##3}{##4}{##5}{##6}[{##7}][{##8}]{}}%
   \let\:Sc\sv:Sc \let\@sect\sv:sect \let\@ssect\sv:ssect
\let\M@sect\sv:Msect
%
   \let\@seccntformat=\o:@seccntformat:
%
   \HtmlEnv    \Toc:Title{##8}\csname no:#1\endcsname{##9}}%
%
      \def\@sect##1##2##3##4##5##6[##7]##8{%
   \ifx \o:@seccntformat:\:UnDef
  \let\o:@seccntformat:\@seccntformat
\fi
\let\@seccntformat\:gobble
%
   \let\@sect\no@sect   \xdef\c:secnumdepth{##2}%
   {\SkipRefstepAnchor \let\addcontentsline\:gobbleIII \let\mark\:gobble
    \no@sect{##1}{##2}{##3}{##4}{##5}{##6}[{##7}]{}}%
   \let\:Sc\sv:Sc \let\@sect\sv:sect \let\@ssect\sv:ssect
\let\M@sect\sv:Msect
%
   \let\@seccntformat=\o:@seccntformat:
%
   \HtmlEnv    \Toc:Title{##7}\csname no:#1\endcsname{##8}}%
%
      \def\@ssect##1##2##3##4##5{%
   \gdef\ssect:ttl{##5}%
%
   \let\@ssect\no@ssect
   {\def\addcontentsline####1####2####3{}%
    \no@ssect{##1}{##2}{##3}{##4}{}}%
   \let\:Sc\sv:Sc \let\@sect\sv:sect \let\@ssect\sv:ssect
\let\M@sect\sv:Msect
%
   \HtmlEnv   \csname :like#1\endcsname{##5}}%
\IgnorePar}
\fi

\fi

\def\:tempa#1|#2|#3\\{%
\html:addr\hbox{\Link-{}{dx\last:haddr}\EndLink}%
\expandafter\protected@write\csname \jobname @idxfile\endcsname{}{\string\beforeentry{\RefFileNumber\FileNumber}{dx\last:haddr}{}}%
\expandafter\protected@write\csname \jobname @idxfile\endcsname{}{\string\indexentry[\@idxfile]{#1}{\thepage}}%
\endgroup%
\@esphack%
}
\HLet\@@wrindexhyp\:tempa
\HLet\@@wrspindexhyp\:tempa

% we use make4ht's splitindex functionality, which produces .ind files in the form of \jobname-idxname.ind
\renewcommand{\printindex}[1][\jobname]{\@input@{\jobname-#1.ind}}

\Hinput{memoir}
\endinput

The important part is this:

\def\:tempa#1|#2|#3\\{%
\html:addr\hbox{\Link-{}{dx\last:haddr}\EndLink}%
\expandafter\protected@write\csname \jobname @idxfile\endcsname{}{\string\beforeentry{\RefFileNumber\FileNumber}{dx\last:haddr}{}}%
\expandafter\protected@write\csname \jobname @idxfile\endcsname{}{\string\indexentry[\@idxfile]{#1}{\thepage}}%
\endgroup%
\@esphack%
}
\HLet\@@wrindexhyp\:tempa
\HLet\@@wrspindexhyp\:tempa

% we use make4ht's splitindex functionality, which produces .ind files in the form of \jobname-idxname.ind
\renewcommand{\printindex}[1][\jobname]{\@input@{\jobname-#1.ind}}

It redefines the command used for writing index info to the auxiliary file, inserts hyperlink destination, which is necessary to be able to link back to the text from the index, and writes index entry in the format needed by make4ht.

The indexing is not supported out of the box, you need to explicitly call indexing commands in a build file. For example, you can save the following code as build.lua:

Make:htlatex {} 
Make:makeindex {} 
Make:htlatex{}

The last thing is to include the index in TOC. You can try this config file:

\Preamble{xhtml}
\begin{document}
\RenewDocumentCommand\tableofcontents{s}{\TableOfContents[part,likepart,chapter,likechapter,section,likesection,subsection,likesubsection]}
\EndPreamble

It redefines \tableofcontents command to print also starred version of sectioning commands (thanks to likechapter etc.).

Compile using:

$ tex4ebook -c config.cfg -e build.lua sample.tex

This is the resulting TOC:

enter image description here

And the index:

enter image description here

Links should point to the correct point in the text.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .