# makeindex - sorting by page number

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?

-
Welcome to TeX.SE. It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. I realize it is not complicated but any time you can save those trying to help would be helpful. – Peter Grill Jan 23 '12 at 17:44

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}

-
This is great! Perhaps How to output a counter with leading zeros? might help. – Werner Jan 23 '12 at 21:04
@Werner -- yes, that will almost certainly help, only i haven't got time now to pursue it. (i knew that had been solved somewhere; the "how to get that page number into the .idx file" was the more interesting part. even though i don't think it's a very good way to arrange a symbols index.) thanks for the link. – barbara beeton Jan 23 '12 at 21:09