3

This is a follow-up of my previous question here. I'm using texlive 2020 and using the files provided by @Michal.h21.

I have modified the input file sample.tex.

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

\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

\chapter{C2}

bword\index{bword}

\index{Muller@M\"{u}ller}

\lipsum \newpage

zword\index{zword}

\backmatter

\printindex

\end{document}

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

ebook.cfg:

% 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

build.mk4

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

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}%

\renewenvironment{theindex}%
{\imki@maybeaddtotoc
  % \imki@indexlevel{\indexname}\imki@indexheaders
      \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-trace :on)
;; ignore group headers
;;markup-letter-group :open "% ")
;;
(markup-index :open  "\begin{theindex}~n
 \providecommand*\lettergroupDefault[1]{}
 \providecommand*\lettergroup[1]{%
 \par}"
          :close "~n~n\end{theindex}~n"
          :tree)
;;
(markup-keyword :open "\idxkeyword{" :close "}" )
(markup-locref :open "\idxlocator{" :close "}")
(markup-locref :open "\textrm{\idxlocator{" :close "}}"  :attr "textrm")
(markup-locref :open "\textrm{\idxlocator{" :close "}}"  :attr "definition")
;;
;; change style of index entries
;;(markup-locclass-list :open "\dotfill " :sep ", ")
(markup-locclass-list :open "\ \ \ ")
(markup-locref-list :sep ", ")
\end{filecontents*}

The index item for Muller is not generated correctly - see picture below.

Index from tex4ebook

The tex4ebook run gives errors.

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=latex)
 \write18 enabled.
entering extended mode
[ERROR]   htlatex: Compilation errors in the htlatex run
[ERROR]   htlatex: Filename Line    Message
[ERROR]   htlatex: ./ebook.cfg  40   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  40   LaTeX Error: Missing \begin{document} in `ebook.cfg'.
[ERROR]   htlatex: ./ebook.cfg  40   Extra \endcsname.
[ERROR]   htlatex: ./ebook.cfg  40   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  40   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  40   Extra \endcsname.
[ERROR]   htlatex: ./sample.ind [15 17   Missing \endcsname inserted.
[ERROR]   htlatex: ./sample.ind [15 17   Missing \endcsname inserted.
[ERROR]   htlatex: ./sample.ind [15 17   Extra \endcsname.
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=latex)
 \write18 enabled.
entering extended mode
[ERROR]   htlatex: Compilation errors in the htlatex run
[ERROR]   htlatex: Filename Line    Message
[ERROR]   htlatex: ./ebook.cfg  43   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  43   LaTeX Error: Missing \begin{document} in `ebook.cfg'.
[ERROR]   htlatex: ./ebook.cfg  43   Extra \endcsname.
[ERROR]   htlatex: ./ebook.cfg  43   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  43   Missing \endcsname inserted.
[ERROR]   htlatex: ./ebook.cfg  43   Extra \endcsname.
[ERROR]   htlatex: ./sample.ind [15 17   Missing \endcsname inserted.
[ERROR]   htlatex: ./sample.ind [15 17   Missing \endcsname inserted.
[ERROR]   htlatex: ./sample.ind [15 17   Extra \endcsname.
--- warning --- Ignoring \special{t4ht.html}

The index from a pdflatex run is correct.

The tex4ebook run used to work correctly a few years ago but it doesn't now.

Is there a way to fix this thank you?

Update using build.mk4 from @Michal.h21.

I get index errors and no index is generated.

Errors in tex4ebook run:

This is LuaTeX, Version 1.12.0 (TeX Live 2020/W32TeX)
 restricted system commands enabled.
[ERROR]   htlatex: Compilation errors in the htlatex run
[ERROR]   htlatex: Filename Line    Message
[ERROR]   htlatex: ./sample.ind [15 5    Extra }, or forgotten $.
[ERROR]   htlatex: ./sample.ind [15 9    Missing $ inserted.
[ERROR]   htlatex: ./sample.ind [15 13   Missing } inserted.

sample.idx

\beforeentry{samplech1.html}{dx3-2001}{}
\indexentry{aword}{3}
\beforeentry{samplech2.html}{dx4-3001}{}
\indexentry{bword}{9}
\beforeentry{samplech2.html}{dx4-3002}{}
\indexentry{Muller@M\"{u}ller}{9}
\beforeentry{samplech2.html}{dx4-3003}{}
\indexentry{zword}{13}

sample.ind

\begin{theindex}
\indexspace
\textbf{A}\label{L-xindex-A}
\nopagebreak[4]
  \item aword, {\Link[]{dx3-2001}{}1\EndLink{}}
\indexspace
\textbf{B}\label{L-xindex-B}
\nopagebreak[4]
  \item bword, {\Link[]{dx4-3001}{}2\EndLink{}}
\indexspace
\textbf{M}\label{L-xindex-M}
\nopagebreak[4]
  \item M\({u}ller, {\Link[]{dx4-3002}{}3\EndLink{}}
\indexspace
\textbf{Z}\label{L-xindex-Z}
\nopagebreak[4]
  \item zword, {\Link[]{dx4-3003}{}4\EndLink{}}
\end{theindex}

Update 2:

There are two issues still for the ebook.

The index doesn't show on the table of contents and the links in the index don't go anywhere - see below.

ebook

The toc in the pdf file is correct and the links work- see below.

pdf

Is there any way to fix this thank you.

Update 3:

The index now shows in the toc using the new imakeidx.4ht.

I added an extra line to the build file to get the links to work. It's now

if mode=="draft" then
 Make:htlatex {}
else
  Make:htlatex {}
  Make:xindy {modules={"utf8-lang"}}
  Make:biber {}
  Make:htlatex {}
  Make:xindy {modules={"utf8-lang"}}
  Make:htlatex {}
end

Is this the best or most efficient way?

I also changed the xindy module to utf8-lang as above and added a new line

S\'{e}an\index{Sean@S\'{e}an}

in the input file and it worked as well. Is this the best way to deal with character accents from

https://en.wikibooks.org/wiki/LaTeX/Special_Characters#Escaped_codes

and not just german ones?

There is still one minor issue with the index. There is a missing space between "aword" and the "B" header:

new index

Thank you.

user41974
  • 841

1 Answers1

1

I've changed the indexing support in make4ht and tex4ebook, so you don't need to use imakeindex.4ht anymore. You can delete it. Instead, built-in TeX4ht indexing support can be used.

The build.mk4 file can look like this:

if mode=="draft" then
  Make:htlatex {}
else
  Make:htlatex {}
  Make:xindy {modules={"duden-utf8"}}
  Make:biber {}
  Make:htlatex {}
  Make:htlatex {}
end

The Make:xindy command is built-in make4ht command. There are also Make:xindex and Make:makeindex commands. See the make4ht documentation for more details.

This is the resulting index in the Epub file, compiled using

tex4ebook -l -c epub.cfg -e build.mk4 filename.tex

enter image description here

Every term has now distinct number, so index contains all used terms. This is a difference to the previous version, where terms were sorted under section numbers.

Edit:

For the intoc option imakeidx support, try the following version of imakeidx.4ht:

% imakeidx.4ht (2020-05-18-14:06), generated from tex4ht-4ht.tex
% Copyright 2019 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 2020-05-18-14:06}


\def\:temp#1#2#3{\html:addr%
\hbox{\Link-{}{dx\last:haddr
}\EndLink}%
\expandafter\protected@write\csname#1@idxfile\endcsname{}%
{\string\beforeentry{\RefFileNumber\FileNumber}{dx\last:haddr
}{\a:makeindex}}%
\o:imki@wrindexentrysplit:{#1}{#2}{#3}%
}
\HLet\imki@wrindexentrysplit\:temp

\ifx \a:makeindex\:UnDef
\NewConfigure{makeindex}{}\Configure{makeindex}{}
\fi
\Configure{@begin}{theindex}{\ind:defs\imki@maybeaddtotoc}



\Hinput{imakeidx}
\endinput

I've updated TeX4ht sources, so it should work everywhere soon.

michal.h21
  • 50,697
  • Thanks for your help. Unfortunately it doesn't work for me and gives an error that it can't find binhex. Must be something wrong with my installation. – user41974 May 13 '20 at 00:30
  • 1
    @user41974 ah, binhex.tex should be part of your installation. it is in texmf-dist/tex/generic/kastrup/binhex.tex in my install. it is needed for accented characters with LuaTeX or XeTeX. – michal.h21 May 13 '20 at 12:05
  • Sorry my mistake, I thought binhex was a program not a package. I installed it and still get errors as shown in updated question. Thanks. – user41974 May 14 '20 at 00:19
  • 1
    @user41974 ah, I've replaced \index{Muller@M\"{u}ller} with \index{Müller} and it worked. This seems like a bug in Xindex, as it introduces wrong characters in the generated .ind file: \item M\({u}ller, {7}. It works correctly with Xindy. But I would use accented characters anyway and then Xindex works well. – michal.h21 May 14 '20 at 08:38
  • Thanks for the update. It still doesn't work for me and I think it's the ü that causes problems in the index file where Müller is corrupted as M�er and it gives an invalid utf-8 sequence error in the run using Make:xindy {modules="duden-utf8"} (your version of this line in build.mk4 gives an error). It's a pity that the original \index{Muller@M"{u}ller} doesn't work anymore because all the character are standard ASCII and portable. – user41974 May 14 '20 at 10:32
  • 1
    @user41974 and do you use LuaTeX (with make4ht -l option)? Accented version should definitely work. If accent doesn't work, you can use the ASCII version with Xindy – michal.h21 May 14 '20 at 11:55
  • I'm not calling make4ht but using the tex4ebook command you gave. Can you please put all your files in your answer for using the ASCII version with Xindy and I will verify that they are what I have. Thank you for your patience and help. – user41974 May 14 '20 at 12:17
  • 1
    @user41974 sure, please see the edit – michal.h21 May 14 '20 at 12:49
  • Is the build.mk4 complete. The line Make:xindy {modules={"duden-utf8} causes an error for me. Error is: [WARNING] mkutils: [string "if mode=="draft" then..."]:5: unfinished string near '"duden-utf8}'. Are you still using the tex4ebook command or a make4ht command. Thanks. – user41974 May 14 '20 at 12:54
  • 1
    @user41974 sorry that was syntax error in the build file. It should be fixed now, it works with tex4ebook and make4ht as well. – michal.h21 May 14 '20 at 15:27
  • The error is gone with the corrected xindy line but the index is not generated. Did you make changes to the Make:add("xindy", function(par) part of the build.mk4. Thanks. – user41974 May 15 '20 at 01:44
  • 1
    @user41974 sure, if you have Make:add("xindy"...) in your build then please remove it. There is built-in support in make4ht now. – michal.h21 May 15 '20 at 07:58
  • It's now working. Thank you so much for all your help. One remaining issue. If I change the source file makeindex line to \makeindex[intoc=true,title=My Index,columns=1,noautomatic] the index doesn't appear in the table of contents in the ebook. It's ok for the pdf file. Thanks. – user41974 May 15 '20 at 09:03
  • I've updated the question to show the remaining issues I didn't realize that the links don't work when I made the previous comment. Thank you. – user41974 May 17 '20 at 01:52
  • 1
    @user41974 links should work, but they may need multiple compilations. the intoc option should work with the updated answer. – michal.h21 May 18 '20 at 12:11
  • I think everything is now working with you new imakeidx.ht4 and an extra xindy line in build.mk4. I updated the question to show what I did and asked some final questions on the correctness and efficiency of my changes. This is also a minor spacing issue in the index. Thank you so much for your work on this. – user41974 May 19 '20 at 01:21
  • 1
    @user41974 you definitely don't need two calls to Xindy. But you are right that it is better to use it after second call to Htlatex, as the internal links are resolved at this stage. so you can remove the first call to Xindy. Regarding spacing issue, I cannot reproduce that. It may be some issue with the Epub viewer. I use Calibre on my desktop and it shows correct spacing. – michal.h21 May 19 '20 at 08:58
  • Thanks for the update. If I remove the first xindy line in the build file, the index disappears from the toc. I also use the epub viewer from calibre so the spacing issue in the index is strange. Is my use of the utf8-lang option ok for the accented characters? Thanks again. – user41974 May 19 '20 at 10:45