[Tex/LaTex] Remove page number from index entries

indexingpage-numbering

My indexed terms are hyperlinks (to external files — see question listed below), so the page numbers are really not useful information. Hece I would like to be able to remove the page number (and the comma preceding the page number).

The MWEs below produce the image on the left, but I would like it to produce the image on the right:

enter image description here
enter image description here

Would be nice if one solution worked with all the packages, but package specific solutions are ok too. I am using imakeidx so that would be most helpful to me.

References:

Code: imakeidx

\documentclass{article}
\usepackage{imakeidx}
\usepackage{hyperref}

\makeindex[columns=1]

\begin{document}
  Test\index{foo}
  \index{bar}
  \printindex
\end{document}

Code: makeidx

\documentclass{article}
\usepackage{makeidx}
\usepackage{hyperref}
\makeindex

\begin{document}
  Test\index{foo}
  \index{bar}
  \printindex
\end{document}

Best Answer

UPDATED ANSWER

I answer this question for two index processors: makeindex and texindy. Both examples use the package imakeidx to simplify the compilation.


texindy

With texindy you have to compile with the option shell-escape.

First of all I set the option texindy of the package imakeidx to specify the index processor.

\makeindex[program=texindy,options=-M mystyle.xdy]

The options specify what will be passed as an argument to texindy. The option -M mystyle.xdy means that texindy use the style filemystyle.xdy.

the algorithm

Before I explain the code I want to explain the algorithm which I use.

  1. The comma between the index entry and the page number is a simple predefined operator of the texindy style. So I can change it by setting them inside the style file.
  2. Every page number of the index entry can be surrounded and modified with a command. To suppress the output of the page number, I can use the LaTeX kernel command \@gobble which is simply defined as

    \def\@gobble#1{}
    

These modification must be executed after the sorting algorithm of the index processor. So you can't remove the page of \indexentry.

Now the implementation.

The index processor texindy (and also makeindex) can't handle LaTeX commands with an @ symbol, because @ is a special character for the index processor. So I defined a command equal to \@gobble

\let\mygobble\@gobble

The usage will be explained later. Next I have to manipulate the command \index so that every page number of the the command index will be modified. Therefore you can use the expansion |. For example to print a bold page number you use \index{foo|textbf}. The textbf is a predefined attribute of the index processor. Instead of textbf I use the attribute gobble. The command \index provided by imakeidx has an optional argument so I recommend the macro \LetLtxMacro provided by package letltxmacro instead of \let

\LetLtxMacro\OldIndex\index
\renewcommand{\index}[1]{\OldIndex{#1|gobble}} 

Through this redefinition every index command gets the attribute gobble. This attribute is defined in the style file for the texindy run:

(define-attributes (("gobble" "default")))
(markup-locref  :open "\mygobble{" :close "}" :attr "gobble")
(markup-locclass-list :open "" :sep "")

In the first line I define a new attribute. In the second line I say what the attribute should do. Here the command \mygobble is used. The third line defines the separator to be empty "".

Side note: Comments in a texindy style file are introduced with a ;.

Altogether we get the following example:

%% !TEX program  = pdflatex --shell-escape
\documentclass{article}
\usepackage{xcolor}
\usepackage{imakeidx}
\makeindex[program=texindy,options=-M mystyle.xdy]
\usepackage{letltxmacro}
\usepackage{filecontents}
\makeatletter
\let\mygobble\@gobble
\LetLtxMacro\OldIndex\index
\renewcommand{\index}[1]{\OldIndex{#1|gobble}} 
\makeatother
\begin{filecontents*}{mystyle.xdy}
;;; xindy style file
(define-attributes (("gobble" "default")))
(markup-locref  :open "\mygobble{" :close "}" :attr "gobble")
(markup-locclass-list :open "" :sep "")
\end{filecontents*}
\begin{document}
Test\index{foo}
\index{bar}
\printindex
\end{document}

The result is:


makeindex

In relation to makeindex here are the options for the package imakeidx

\makeindex[program=makeindex,options=-s mystyle.ist]

The option options must be set to -s mystyle.ist because in this way you pass a style file to the index processor makeindex.

makeindex works with the same algorithm. So the lines are almost identical

\let\mygobble\@gobble
\LetLtxMacro\OldIndex\index
\renewcommand{\index}[1]{\OldIndex{#1|mygobble}} 

Unlike texindy, makeindex doesn't work with attributes. The command mygobble without backslash will be passed as an argument of \index to the processor. The processor will write \mygobble{\thepage}.

The separator must be redefined inside a style file.

quote '+'
delim_0 " "
delim_1 " "
delim_2 " "
delim_n " "

delim_0 etc. are the separator of the different levels.

Side note: Comments in a makeindex style file are introduced with a the same as in LaTeX the %.

So we get the following example with makeindex:

\documentclass{article}
\usepackage{xcolor}
\usepackage{imakeidx}
\makeindex[program=makeindex,options=-s mystyle.ist]
\usepackage{letltxmacro}
\usepackage{filecontents}
\makeatletter
\let\mygobble\@gobble
\LetLtxMacro\OldIndex\index
\renewcommand{\index}[1]{\OldIndex{#1|mygobble}} 
\makeatother
\begin{filecontents*}{mystyle.ist}
quote '+'
delim_0 " "
delim_1 " "
delim_2 " "
delim_n " "
\end{filecontents*}
\begin{document}
Test\index{foo}
\index{bar}
\printindex
\end{document}

hyperref support

Remember to load imakeidx before hyperref.

In combination with hyperref the approach with makeindex doesn't need any special handling. If you use texindy in combination with hyperref you have to change the style file *.xdy in the following way:

(markup-locclass-list :open "" :sep "")
(markup-range :sep "")

In the document body you must change the printing of the index as follows to gobble the page numbers:

\begingroup
\def\hyperpage#1{}
\printindex
\endgroup

Here the complete MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{imakeidx}
\makeindex[program=texindy,options=-M mystyle.xdy]
\usepackage{letltxmacro}
\usepackage{hyperref}
\usepackage{filecontents}
\begin{filecontents*}{mystyle.xdy}
;;; xindy style file
(markup-locclass-list :open "" :sep "")
(markup-range :sep "")
\end{filecontents*}
\begin{document}
Test\index{foo}
\index{bar}

\begingroup
\def\hyperpage#1{}
\printindex
\endgroup
\end{document}
Related Question