TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...
    Please follow the \ref{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.

share|improve this question
1  
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
1  
Thanks - good point! I edited my question regarding this... – matheburg Jun 2 '14 at 9:15
up vote 8 down vote accepted

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.

Sample output

\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...
    Please see the \nameref{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...
    Please see the \hyperref[index]{\indexname}.

    % print index...
    \printindex
\end{document}
share|improve this answer
    
Is there an equivalent to \setindexpreamble for the book document class (as opposed to scrbook)? – Jeff Irwin Jun 13 at 17:39
1  
@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...
Please follow the \hyperref[index]{index}.

% print index...
\printindex

\end{document}
share|improve this answer

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...
    Please follow the \ref{indexfoo}. % Refer to the label

\chapter{Foobar}

    % print index...
\definefakelabel{indexfoo}% how is the label named?
\printindex 
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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