Using counters within \index{...} gives solutions to the problem of using counters within the \index command when using \usepackage{makeidx}, as in

    \index{index entry \arabic{counter}}

In this case, \arabic{counter} is written verbatim

    "index entry \arabic{counter}"

to the .idx file rather than being first expanded to something like

    "index entry 1".

Solutions given include defining a new macro \Index or use an \mbox{}. These solutions work great when using the makeidx package.

However, neither solution works when using the index package rather than the makeidx package. Here is a simple example that was adapted from the above link:

%\usepackage{makeidx}% using this, "\arabic{section}" expands to 1, 2, 3,...
\usepackage{index}% using this, "\arabic{section}" does not expand
\section{First  Section} \Index{first  \arabic{section}}
\section{Second Section} \Index{second \arabic{section}}
\section{Third  Section} \Index{third  \arabic{section}}
\section{Fourth Section} \mbox{\index{fourth \arabic{section}}}
\section{Fifth  Section} \mbox{\index{fifth  \arabic{section}}}
\section{Sixth  Section} \mbox{\index{sixth  \arabic{section}}}

When using \usepackage{makeidx}, the .idx file contains what one might want (expanded counter values):

    \indexentry{first 1}{1}
    \indexentry{second 2}{1}
    \indexentry{third 3}{1}
    \indexentry{fourth 4}{1}
    \indexentry{fifth 5}{1}
    \indexentry{sixth 6}{1}

However, when using \usepackage{index}, the .idx file contains what one may not want (verbatim strings/unexpanded counter values):

    \indexentry {first \arabic {section}}{1}
    \indexentry {second \arabic {section}}{1}
    \indexentry {third \arabic {section}}{1}
    \indexentry {fourth \arabic {section}}{1}
    \indexentry {fifth \arabic {section}}{1}
    \indexentry {sixth \arabic {section}}{1}

I want to use the index package for multi-index support. Does anyone have a solution that can be used with the index package?

Many many thanks in advance,

  • Welcome to TeX.SX! – Bobyandbob Nov 21 '17 at 4:21
  • 2
    Use the imakeidx package in order to get multiple indices together with expansion – user31729 Nov 21 '17 at 6:51
  • Thank you @Bobyandbob for the welcome message and to @Christian for the package recommendation. Thank you so much. The imakeidx package works great. I tested it both with the above code and with a "real" project. I did have some issues with it involving apparently failing to successfully automatically call makeindex.exe and crashing when \index{...} included \ifthenelse. But these are separate issues and if they are significant enough, I will post them separately. Actually, I'm not too crazy about automatically calling an external .exe anyways, but would rather call it from a makefile. – Daniel J. Greenhoe Nov 22 '17 at 3:59
  • @DanielJ.Greenhoe: You can notify only one of the commenters ;-) And actually makeindex is run automatically by imakeidx, provided shell-escape is enabled – user31729 Nov 22 '17 at 4:50
  • 1
    It's not clear why using an index for producing the table of contents. – egreg Feb 3 '18 at 22:45

The (very old) package index overrules the additions in latex.ltx about \protected@write to the index file, so \arabic{section} in an index entry is not expanded first and written then to the .idx.

The very sophisticated package imakeidx supersedes index and allows for multiple indexes, if shell-escape (\write18) is enabled, makeindex or texindy is run automatically.



\section{First  Section} \index{first \textbf{Hello} \arabic{section}}
\section{Second Section} \index{second \arabic{section}}
\section{Third  Section} \index{third  \arabic{section}}
\section{Fourth Section} \index{fourth \arabic{section}}
\section{Fifth  Section} \index{fifth  \arabic{section}}
\section{Sixth  Section} \index{sixth  \arabic{section}}

enter image description here

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.