# Index entries duplicated for captions

In document class book, and package caption, creating index with makeidx. Index entries for the main text and for footnotes collate properly; but those for captions create their own entries.

Example: for the tag \index{New York!Custom House!\textit{Cartouche}}, text and footnotes collate properly; but used in a caption, a new entry is created at the third level, and is also out-of-alphabetic-sequence. Note: the top level, and first sub are correct; only the second sub is duplicated.

How to get the caption index entries under the same entry?

-
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Benedikt Bauer Dec 27 '12 at 23:14

The problem is in the fact that a "normal text" appearance of

\index{New York!Custom House!\textit{Cartouche}}


writes in the .idx file a line such as

\indexentry{New York!Custom House!\textit{Cartouche}}{1}


while the same in the argument of \caption will have

\indexentry{New York!Custom House!\textit  {Cartouche}}{3}


(the numbers will be different for your document). This is due to how LaTeX processes caption texts. You see that the two lines are different and they are actually very different to MakeIndex.

There's also a problem in how you write the index annotations: formatting instructions are bound to confuse the collating process by MakeIndex. So use

\index{New York!Custom House!Cartouche@\textit{Cartouche}}


in normal text and, for the caption, write something like

\begin{figure}
...
\index{New York!Custom House!Cartouche@\string\textit{Cartouche}}}
\end{figure}

-
Good - this works - thanks - lingering problem: this throws the sort ordering off for the third-level. The entries treated as above have their own sort-order. I'm experimenting, but it looks like it is descending alphabetical. –  calmbob Dec 28 '12 at 1:46
@calmbob Can you make an example? MakeIndex bases the sorting on what comes before the @ –  egreg Dec 28 '12 at 10:40
I think I found the flaw in my thinking - I rigorously harmonized use of \textit across all the entries, and all works magically - correctly, that is –  calmbob Dec 28 '12 at 14:04
@egreg -- two points: (1) makeindex bases the combining (if not the sorting) on the entire string within the argument of \index{...}, and (2) \protect will not work here -- it will leave a single space after \textit (unprotected command gets two spaces as seen in your example) -- so it is absolutely necessary to use \string. very un-latex-y, but that's the way it is. –  barbara beeton Dec 28 '12 at 16:35

As an alternative mechanism

An alternative to egreg's solution I was going to post

\newcommand\hmm[1]{#1}


Then you can use

 8\hmm{\index{....}}


You could redefine \index to always do this, this just always prevents the verbatim nature of \index from activating, so you have to be a bit more careful about what you put in the argument, but it does then work consistently in all contexts.

-