[Tex/LaTex] Linking indexed term to index entry

hyperrefindexinglinks

I am trying to create links from \index commands to corresponding index entries. When hyperref package is loaded, page numbers in index are, by default, linked to the page on which \index command for that entry appears. But it does not work another way around (forward-linking).

When checking correctness of index, it would be useful if I could click on indexed word in PDF (#2 in ati macro in MWE below) and it would take me to the place in the index in which corresponding entry appears. Actual index has many pages, so it is not sufficient to just determine page number where index starts, because given entry can actually appear few pages later.

My first idea was to add label to index entry and then create hyperlink from indexed word. Attempt in doing so is reflected in commented atiwl macro below. Unfortunately, this didn't compile. (Even if it did, there would be a problem with adding unique labels to each entry). Second idea to test if using label could work was to add one label directly to .ind file, like this:

\item Index entry 1, \hyperpage{1}\label{linkedentry}

Altought it compiled, resulting link did not work in PDF.

MWE:

\documentclass{book}
\usepackage{blindtext}
\usepackage{makeidx}
\makeindex
\def \ati#1#2{#2\index{#1}} % "Add to index" macro
% \def \atiwl#1#2{\hyperlink{linkedentry}{#2}\index{#1\label{linkedentry}}} % "Add to index with label" macro
\usepackage{hyperref}
\begin{document}
\blindtext

Some text and \ati{Index entry 1}{indexed term}.

\blindtext

Some text and \ati{Index entry 2}{another indexed term}.

% \blindtext

% Some text and \atiwl{Entry with label}{some indexed text}.

\printindex

\end{document}

I am not sure if this is relevant to the solution, but I use texindy as index processor. And actually, I use splitidx package and accompanying splitindex command line tool, because I need many indexes. In MWE, though, I used makeidx to make it more universal for other people with the same problem.

Is there any way to create link from #2 in ati macro to corresponding index entry?

Best Answer

This is perhaps what is requested: a \ati - macro which allows forward-backward linking with an automatically generated label depending on a special counter.

As long this counter is only changed by \ati and not manipulated otherwise, the label is unique.

Use the starred command to prevent hyperlinking to the index and use the optional argument (in conjunction with imakeidx) for the special features of \index - macro from imakeidx.

\documentclass{book}
\usepackage{blindtext}
\usepackage{imakeidx}
\usepackage{xparse}
\newcounter{indcntr}
\makeindex


\NewDocumentCommand{\ati}{somm}{%
  \IfBooleanTF{#1}{%
    \IfValueTF{#2}{%
      #4\index[#2]{#3}%
    }{%
      #4\index{#3}%
    }%
  }{%
    \stepcounter{indcntr}%
    \protect\hyperlink{ind::\number\value{indcntr}}{#4}%
    \IfValueTF{#2}{%
      \index[#2]{\protect\hypertarget{ind::\number\value{indcntr}}{#3}}%
    }{%
      \index{\protect\hypertarget{ind::\number\value{indcntr}}{#3}}%
    }%
  }%
}

\usepackage{hyperref}
\begin{document}
\blindtext

Some text and \ati{Index entry 1}{indexed term}

\blindtext

Some text and \ati*{Index entry 2}{another indexed term}.

\blindtext[4]

 Some text and \ati{Entry with label}{some indexed text}.

\printindex

\end{document}
Related Question