0

I have a long document, consisting of 40 chapters. I want to index two different things, so I use imakeidx to have multiple indexes.

When the indexes are displayed, I would like them to show, for each entry, the chapter number AND the page number of the reference.

With some research, I managed to find the following solution (see MWE below). I modified one of the indexing command to have it store chapter number alongside page number.

It does produce what I wanted, visually speaking, except that the links are broken. The numbers are clickable, but they all send me back to the first page of the document, no matter what they are referencing.

Does anyone know of a solution to have the chapters displayed like they are on the image and still have working hyperlinks ?

Ideally, I would prefer to keep working with imakeidx, but I guess I could switch to another indexing package, if it supports multiple indexes.

\documentclass{report}

\usepackage{imakeidx} 

\makeatletter
\renewcommand{\imki@wrindexentrysplit}[3]{%
  \expandafter\protected@write\csname#1@idxfile\endcsname{}%
   {\string\indexentry{#2}{\thechapter-#3}}%
}
\let\imki@wrindexentry\imki@wrindexentrysplit
\makeatother

\usepackage{hyperref} 

\makeindex

\begin{document}

\chapter{This is a chapter}
Hello\index{Hello}
\newpage
There's some text on this page too!
\chapter{This is another chapter}
Who\index{Who} are\index{are} you\index{you} ?

\printindex
\end{document}

The resulting index

2
  • 1
    try with \AddToHook{shipout/background}{\put(0,0){\MakeLinkTarget*{page.\thechapter-\thepage}}}. Apr 1 at 21:33
  • @UlrikeFischer That's great, it does exactly what I was looking for! Maybe you could post it as an answer, so I could accept it ? Apr 2 at 8:25

1 Answer 1

1

After your change the ind-file contains commands like \hyperpage{2-3}, and hyperref tries to add a link to page 2-3 which doesn't exist. Your log then contains warnings like this:

pdfTeX warning (dest): name{page.2-3} has been referenced but does not exist, 
replaced by a fixed one

The "fixed one" is Doc-Start and so all links go to the start of the document.

To fix that the easiest is to add additional, suitable page anchors with

\AddToHook{shipout/background}{\put(0,0){\MakeLinkTarget*{page.\thechapter-\thepage}}}

You must log in to answer this question.

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