# Symbol index sorted by occurrence

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}


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".

-
You find a transcript of MakeIndex workings in the .ilg file. – egreg Jan 15 '12 at 10:12

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}}

-

Here's another possibility that uses glossaries. You need an up-to-date version of glossaries, but you don't need to use makeindex or any other external application. Just run LaTeX twice.

\documentclass{article}

\usepackage[index]{glossaries}
\usepackage{glossary-mcols}

\makenoidxglossaries

\newterm[name={\ensuremath{\omega}}]{omega}
\newterm[name={\ensuremath{A^P_k}}]{Apk}
\newterm[name={\ensuremath{\{\}}}]{braces}

\begin{document}
\gls{omega}

\gls{Apk}

\gls{braces}

\printnoidxglossary[type=index,style=mcolindex,sort=use]
\end{document}


Of if you find \ensuremath abhorrent:

\documentclass{article}

\usepackage[index]{glossaries}
\usepackage{glossary-mcols}

\makenoidxglossaries

\newterm[name={$\omega$},text={\omega}]{omega}
\newterm[name={$A^P_k$},text={A^P_k}]{Apk}
\newterm[name={$\{\}$},text={\{\}}]{braces}

\begin{document}
$\gls{omega}$

$\gls{Apk}$

$\gls{braces}$

\printnoidxglossary[type=index,style=mcolindex,sort=use]
\end{document}

-

You can control the order of the things in the index by providing some additional information to the argument of the \index command.

By writing \index{<sort information by>@<information>}, you tell \index to put <information> in the index but sort it by <sort information by>.

For example, using the following:

• \index{.a omega@$\omega$}
• \index{.a alpha@$\alpha$}
• \index{.b APK@$A^P_k$}
• \index{.c \{\}@$\{\}$}
• \index{a}
• \index{A}

The entries in the index should be as follows:

• $\alpha$;
• $\omega$;
• $A^P_k$;
• ${}$;
• A;
• a.

In this example, I use . as a prefix for index material with mathematical content (it effectively forces the mathematical stuff before the letters), and <letter> as a presorting criterion.

The following shows how to adjust the \index command to make it 'sort' the indexes by occurrence. This solution is equivalent to the solution that was posted by egreg.

\makeatletter

\newcount\index@counter
\let\old@index\index
\def\myindex@#1#2{%
\old@index{#1@#2}%
}
\def\index#1{%
\expandafter\myindex@{\the\index@counter}{#1}%