# [texhax] indexing problem

Oleg Katsitadze olegkat at gmail.com
Sat May 20 14:09:34 CEST 2006

Hi,

As nobody answered your question, I try to give you some
clues.  Hope I'm not too late.

Since I am a plain TeX user, I can only guess at what LaTeX
does and why what you want is not working, but I can't give
you a proper LaTeX solution.

First of all, take a look at the file with the same basename
as your document and the extension .idx'.  This is the
intermediate file where LaTeX dumps all your index entries.
This should clarify many things for you.

You'll notice that in the entries from the text body, the
macros like \pie and \ind are written "as is" (unexpanded),
while in the entries from the footnote these macros are
replaced by their definitions.

The reason for this is as follows.  Normally, when TeX reads
an argument of a macro, each token in the argument is parsed
and converted into internal representation.  This means,
among other things, that a string like "\ind" in the
argument will become a single token for the macro \ind, not
the four characters "\", "i", "n", and "d".

On the other hand, LaTeX's \index macro is designed in a way
that allows it to read the argument verbatim.  That is, a
string like "\ind" in the argument of \index remains a
four-character string "\ind", not the single token for the
macro \ind.

The consequence of the above is that in the text body, the
argument of \index becomes a _verbatim_ string, and you can
see that in the .idx file.  And what happens when \index is
placed inside \footnote?  Well, \footnote does not read it's
argument verbatim, therefore all macros (like \ind) in its
argument become single tokens, not the verbatim strings.
This means that by the time \index inside \footnote starts
reading it's argument, all instances of \ind, etc., have
already been converted into single tokens, and that's what
\index gets.  When \index then writes its argument to the
.idx file, these macros are expanded, which you can also see
in the .idx file for the entries coming from the footnote.

The outcome is that two instances of the same index entry
appearing in the body and in the footnote are regarded by
MakeIndex as different.  Even though they result in the same
text in LaTeX, MakeIndex doesn't know that.

Another consequence is that when you, as you say, define
\pie as a@\textbf{PROTO-INDO-EUROPEAN}, the sorting doesn't
work.  This is because, inside text body, \pie is written
out _verbatim_ and MakeIndex doesn't see what you want it to
see -- it sees \pie' instead of a@\textbf{PROTO-INDO-EUROPEAN}.

One solution might be making \index parse its argument
non-verbatim, plus expanding the first token of the argument
(e.g., \pie).

Unfortunately, I don't know how to do this in LaTeX, but in
plain TeX you can define something like this:

\let\oldindex\index
\def\index#1{\oldindex{#1}}

(Putting it after \makeindex in LaTeX seems to work, see the
attached file.)  This works because the redefined \index now
reads and parses its argument non-verbatim before passing it
on to the original \index, so \pie will become a token
regardless of whether it is inside a footnote or text body;
it will be expanded when \oldindex writes it to the .idx
file, so MakeIndex will see, e.g.,
a@\textbf{PROTO-INDO-EUROPEAN} instead of \pie.

But keep in mind that all macros inside the argument of the
new \index are now expanded when written to the .idx file,
which might cause problems with some macros.  Furthermore,
\index and \oldindex read their arguments differently,
therefore don't mix them.  Finally, probably my solution is
seriously flawed since I'm not familiar with LaTeX; and
certainly there are better solutions around (hopefully
someone will point them out).

Hope this helps,
Oleg

PS.  In the sample you provided I see lines like this:

> \index{a@\pie!Natio}Natio \index{c@\ind!1@\sk!est}est \index{b@\an!
> omnis}omnis \index{c@\ind!2@\mind!Gallorum}Gallorum \index{c@\ind!1@

It is a very bad idea to break argument of \index across
lines, because the newline is converted into space which
ends up in the .idx file.  Because of this space, MakeIndex
might treat two entries which you expect to be the same as
different.  I've tried to correct this in the attached file.

PPS.  I think there are packages/classes providing \footnote
which does not read its argument at all (memoir.cls?).  With
such a package, \index would work the same both in the text
body and in the footnote, so a better solution might be
possible.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: latex.tex
Type: text/x-tex
Size: 1883 bytes
Desc: not available
Url : http://tug.org/pipermail/texhax/attachments/20060520/f9531715/attachment.bin