[Tex/LaTex] makeindex – sorting by page number

indexing

I'm trying to create an index of notation for my paper.
I'd like to sort the index items chronologically by order of appearance.
In other words, if I have the symbol $Z$ occurring on the second page,
the symbol $C$ on the 8th page and the symbol $P$ on the last page
I would like the index to appear as

Z, 2 
C, 8
P, 97

Do you know how to do it automatically?

Best Answer

here's an approach that uses labels for the symbols and \pageref to keep them in input order. the \pagerefs need to be expanded before writing to the .idx file; ordinarily index material is written out verbatim. this even permits multiple page references to the same symbol; in such a case, only the first appearance should get a label.

the routine needs some more work, since at the moment it trims off leading zeros from \pageref numbers, so if there are any entries on pages 1-9 they will be out of order in an index for a book of up to 99 pages, etc. (it would probably be best to normalize on 3-digit page numbers.) i'll work on that when time permits, but if someone else gets there first, go for it!

i'm also not sure what would happen with a symbol first introduced in a preface, or on some other page numbered with roman numerals. another open problem.

\documentclass{book}
\usepackage{makeidx}
\makeindex
\newcommand{\xindex}[2]{\expandafter\index\expandafter{\pageref{#1}#2}}
\begin{document}
\chapter{One}
some text $Z$\label{not:Z}\xindex{not:Z}{@$Z$} some more text.

\newpage
some text $C$\label{not:C}\xindex{not:C}{@$C$} some more text.

\newpage
some text $P$\label{not:P}\xindex{not:P}{@$P$} some more text.
and a second reference to a symbol that first appeared on a previous page
$C$\xindex{not:C}{@$C$} some more text.

\printindex

\end{document}
Related Question