# How to reference the Index?

How can I reference the index created by the package makeidx?

Minimal working example:

\documentclass{scrbook}

\usepackage{hyperref} % creates links on refs
\usepackage{makeidx}
\makeindex

\begin{document}
% add something to the index...
The foobar\index{foobar} is quite impressive!

% some reference to the index...

% print index...
\printindex
\end{document}


The obvious \label{index} does neither work before nor after \printindex.

Edit (thanks to daleif): An important fact I forgot to tell: I want the index to be linked at \ref{index}. That's why I cannot just say See index.

-
As long as it is listed in the TOC, I don't think a direct link to it is necessary. See the index ought to be reference enough. – daleif Jun 2 '14 at 9:02
Thanks - good point! I edited my question regarding this... – matheburg Jun 2 '14 at 9:15

There is a hook \setindexpreamble in the scr classes to add material at the beginning of the index. You can use this to set your label. Using just \ref will give a blank link, you need either a \pageref or a \nameref.

\documentclass{scrbook}

\usepackage{makeidx}
\usepackage{hyperref} % creates links on refs
\makeindex

\setindexpreamble{\label{index}}

\begin{document}
\label{start}
% add something to the index...
The foobar\index{foobar} is quite impressive!

% some reference to the index...

% print index...
\printindex
\end{document}


In standard classes such as article and book you can use the etoolbox package to patch theindex environment to provide a suitable anchor as follows:

\patchcmd{\theindex}{\thispagestyle{plain}}
{\thispagestyle{plain}\phantomsection\label{index}}{}{}


You can usefully refer this either via \pageref, or via \hyperref[index]{text description} and in particular \hyperref[index]{\indexname}

\documentclass{book}

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

\patchcmd{\theindex}{\thispagestyle{plain}}
{\thispagestyle{plain}\phantomsection\label{index}}{}{}

\begin{document}
\label{start}
% add something to the index...
The foobar\index{foobar} is quite impressive!

% some reference to the index...

% print index...
\printindex
\end{document}

-
Is there an equivalent to \setindexpreamble for the book document class (as opposed to scrbook)? – Jeff Irwin Jun 13 at 17:39
@JeffIrwin Not by default, but you can patch theindex. I have now extended my answer with such a description. – Andrew Swann Jun 13 at 19:02

You can do it with imakeidx:

\documentclass{scrbook}

\usepackage{imakeidx}
\usepackage{hyperref} % creates links on refs

\makeindex[title={Index\label{index}}]

\begin{document}
% add something to the index...
The foobar\index{foobar} is quite impressive!

% some reference to the index...

% print index...
\printindex

\end{document}

-

A more 'basic' version, using a fake label that is implanted by \@currentlabel and \label.

\documentclass{scrbook}

\usepackage{makeidx}

\usepackage{hyperref} % creates links on refs

\makeatletter
\AtBeginDocument{%
\newcommand{\@@fakelabelname}{}%
\newcommand{\definefakelabel}[1]{%
\gdef\@@fakelabelname{#1}% Make the label name globally available
}
\newcommand{\implantfakelabel}{%
\expandafter\def\csname p@\@@fakelabelname\endcsname{}% Define the dummy 'label' content%
\protected@edef\@currentlabel{\csname p@\@@fakelabelname\endcsname\indexname}% Fake \refstepcounter
\phantomsection
\label{\@@fakelabelname}%
}

\let\latex@@theindex\theindex  % store the original index version
\let\latex@@theindexend\endtheindex % store the original \endtheindex
\renewenvironment{theindex}[1][index]{%
\latex@@theindex% Call the old index start up
\implantfakelabel% place the fake label here
}{%
\latex@@theindexend% End code
}
}
\makeatother

\makeindex

\begin{document}
% add something to the index...

\chapter{Foo}
The foobar\index{foobar} is quite impressive!

% some reference to the index...