Indexing showing "enumerate" counter instead of page

The default index created by makeidx shows the list of the indexed entries and the pages of the corresponding occurrences.

I need to index many words that appear many times in just a few pages (inside a long enumerate environment), so it doesn't make sense to show the pages where they occur. Instead I'd like to show, for each indexed key, its occurrences inside the enumerate environment (i.e. the values of the enumerate counter corresponding to items in which the key appears).

Is it possible to do that?

Here is an MWE:

\documentclass{article}
\usepackage{makeidx}

\makeindex

\begin{document}

\begin{enumerate}
\item The One Ring can be destroyed only in Mordor.
\index{One Ring}
\index{Mordor}

\item Sauron's fortress is Barad-d\^{u}r, which is located in Mordor.
\index{Sauron}
\index{Mordor}

\item The One Ring belongs to Sauron.
\index{One Ring}
\index{Sauron}
\end{enumerate}

\printindex

\end{document}


This is what is shown in the pdf:

1. The One Ring can be destroyed only in Mordor.
2. Sauron’s fortress is Barad-dur, which is located in Mordor.
3. The One Ring belongs to Sauron.

Index

Mordor, 1

One Ring, 1

Sauron, 1

But I'd like the index to be:

Mordor, 1, 2

One Ring, 1, 3

Sauron, 2, 3

• It is certainly possible, although I'm not sure if it is advisable. In any case, please provide a MWE so we can play with it and suggest what you can do. Jan 5 '14 at 13:03

Solution Idea

We note that the entries for index are made by the \index command. Further investigation shows that actual low level writings to the .idx file are done by the \@wrindex command in latex.ltx. This \@wrindex uses \thepage to write the page numbers which are eventually used in the index entries.

So, we hack the \@wrindex command and use \theenumi in place of \thepage.

The Solution

\documentclass{article}

\usepackage{makeidx}
\makeatletter
\def\@wrindex#1{%
\protected@write\@indexfile{}%
{\string\indexentry{#1}{\theenumi}}
\endgroup
\@esphack}
\makeindex

\begin{document}

\begin{enumerate}
\item The One Ring can be destroyed only in Mordor.
\index{One Ring}
\index{Mordor}

\item Sauron's fortress is Barad-d\^{u}r, which is located in Mordor.
\index{Sauron}
\index{Mordor}

\item The One Ring belongs to Sauron.
\index{One Ring}
\index{Sauron}
\end{enumerate}

\printindex

\end{document}


Output

Limitation

Only the first level of enumeration (\enumi) has been used. Should you want otherwise, please see this question to improve the solution to use other (current sublevel) levels of enumeration.

One possibility is to use glossaries. You need at least version 4.02 for the following example to work:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[index,counter=enumi,nomain]{glossaries}

\makeglossaries

\newterm{duck}
\newterm{arara}
\newterm{cat}
\newterm{zebra}
\newterm{aardvark}
\newterm{goose}

\begin{document}

\begin{enumerate}
\item A \gls{duck} and a \gls{zebra} went to tea with the \gls{arara}.

\item A \gls{zebra}.

\item More \glspl{zebra}.