[Tex/LaTex] Symbol index sorted by occurrence

indexingsorting

I would like to make an symbol index which is sorted by occurence of symbol.

For example: Something like

... $\omega$\index{$\omega$} ... $A^P_k$\index{$A^P_k$} ... $\{\}$\index{$\{\}$}

should produce an index which first mentions $\omega$ then $A^P_k$ and then $\{\}$.

I did not find any option for makeidx, so I tried to use a workaround and define a command which does that for me:

\newcounter{occurrence}
\newcommand{\is}[1]{\index{\arabic{occurrence}@{#1}}\addtocounter{occurrence}{1}}

In fact this works to some extend but some symbols get rejected (I do not know which one) and some will not be printed appropriate for example \is{$\langle \rangle$} results in the entry "hAB".

Best Answer

The problem is that \index expands its contents when it appears as the argument to another command, while it doesn't if it appears at the top level, so with \is{$\langle\rangle$} you get, in the .idx file,

\indexentry{0@{$\delimiter "426830A \delimiter "526930B $}}{1}

and, in the .ind file,

\item {$\delimiter 426830A \delimiter 526930B $}, 1

(notice that the double quotes get lost).

Now TeX finds \delimiter followed by the number 426830, which is hexadecimal "6834E and since it's in normal operations, it typesets the math code "0068: in position "68 of font family 0 there's an h. Similarly, 526930 is hexadecimal "80A52, but in position "80 there's no character! So you get a roman h, followed by italics A and B. Wow!

A way out is telling TeX not to interpret the characters:

\newcommand{\is}[1]{%
  \index{\arabic{occurrence}@\detokenize{#1}}%
  \stepcounter{occurrence}}
Related Question