2

I'm currently using packages hyperref and imakeidx to create my book's index. This index is big enough that it spans multiple pages. It contains some entries such as

% In the index under "B"...

\index{bar!foo|see {foobar}}%

% Several pages later in the index, under "F"...

\index{foobar}%

Since I'm using package hyperref, index-entries that point to a specific page in the mainmatter are rendered as hyperlinks in the final PDF. I wonder if there is a way to hyperlink the see entries as well. That is, I'd like to render "bar, see foobar" as a hyperlink that points to the page of the index where "foobar" is listed.

Is this possible? and if so, what's the best way to do it?

2

A preliminary version, working for top-level index items only, not working for entries like bar!foo itself if |see {foo} is meant.

There is a hypertarget defined and retrieved with \seelink.

The multiple usage of \index{foobar} will prevent same target names.

\documentclass{article}

\usepackage{blindtext}
\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{letltxmacro}
\usepackage{hyperref}


\ExplSyntaxOn
\AtBeginDocument{%

  \LetLtxMacro\indexorig\index

  \newcommand{\hyperindextarget}[1]{%
    \hypertarget{#1}{}%
  }

  \RenewDocumentCommand{\index}{om}{%
    \IfValueTF{#1}{%
      \addindextarget[#1]{#2}%
    }{%
      \addindextarget{#2}%
    }%
  }
}

\newcommand{\seelink}[2]{%
  \seename\ \hyperlink{#1}{#1}%
}


\seq_new:N \g_quuxxplusone_target_seq

\NewDocumentCommand{\addindextarget}{om}{%
  \IfValueTF{#1}{%
    \seq_if_in:NnTF \g_quuxxplusone_target_seq {#2} {%
      \indexorig[#1]{#2}%
    }{% False branch
      \seq_gput_right:Nn \g_quuxxplusone_target_seq {#2}
      \indexorig[#1]{#2|hyperindextarget{#2}}%
    }%
  }{% No optional argument
    \seq_if_in:NnTF \g_quuxxplusone_target_seq {#2} {%
      \indexorig{#2}%
    }{% False branch
      \seq_gput_right:Nn \g_quuxxplusone_target_seq {#2}
      \indexorig{#2|hyperindextarget{#2}}%
    }%
  }%    
}

\ExplSyntaxOff

\makeindex


\begin{document}



Foo
\clearpage
\blindtext[5]
% In the index under "B"...

\index{bar}
\index{bar!foo|seelink {foobar}}%

% Several pages later in the index, under "F"...

\blindtext[15]

\index{foobar}%

\blindtext[15]

\index{foobar}

\index{zzz|seelink{bar}}

\printindex

\end{document}
  • I'll try to improve this as soon as possible – user31729 Oct 17 '17 at 20:41
  • Hi Christian. My vote is always +1. – Sebastiano Oct 17 '17 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.