3

This question already has an answer here:

To index a nontrivial TeX expression expr, we use the command \index{str@expr}, where str is a simplified (typically plain ASCII) representation of expr to be used for sorting.

However, str@expr is parsed incorrectly inside a footnote:

\documentclass{report}
\usepackage{imakeidx}
\makeindex

\def\gnu{\texttt{Gnu}}
\def\gnat{\texttt{Gnat}}
\begin{document}
Some text\index{Gnu@\gnu}\index{Gnat@\gnat}.

\pagebreak
Some more text\index{Gnu@\gnu}.\footnote
{A footnote.\index{Gnat@\gnat}}

\printindex
\end{document}

This generates the following .idx file

\indexentry{Gnu@\gnu}{1}
\indexentry{Gnat@\gnat}{1}
\indexentry{Gnu@\gnu}{2}
\indexentry{Gnat@\texttt  {Gnat}}{2}

The two index entries for 'Gnat' differ, and therefore \Gnat appears twice in the index, as shown in this screenshot:

enter image description here

How does \footnote modify the \index command, and what can be done about it?

marked as duplicate by egreg, Svend Tveskæg, Paul Gessler, LaRiFaRi, Andrew Swann May 4 '15 at 12:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    This is a known problem due to the fact that when \index is in the argument to another command, the special treatment to the argument cannot be performed. It's been in LaTeX from day one. I linked a duplicate (it's about \caption, but the phenomenon is the same). For the special case of footnotes, loading the bigfoot package solves the issue. – egreg May 4 '15 at 12:04
  • The solution is duplicate, but not the problem. Only if you know the solution, or if you are operating at a level of abstraction where footnote and caption are essentially the same, the problem asked here may appear to you as a duplicate of the problem solved there. – Joachim W May 4 '15 at 12:30
  • @JoachimWuttke I guess the point is that we are talking '\index inside an argument' in both cases, and that problem is the same. – Joseph Wright May 4 '15 at 12:47
3

It is not so much a question of the @ construction, but of expansion of control sequences. A work around based on https://groups.google.com/forum/#!topic/comp.text.tex/_Hwo6Hapcng suggests providing a command we can use to protect the backslash in the index entry in the footnote via \def\indexprotect#1{\string#1}. As Ulrike Fischer points out \string is already good enough to do that on its own:

Sample output

\documentclass{report}
\usepackage{imakeidx}
\makeindex

\def\gnu{\texttt{Gnu}}
\def\gnat{\texttt{Gnat}}
\begin{document}
Some text\index{Gnu@\gnu}\index{Gnat@\gnat}.

\pagebreak
Some more text\index{Gnu@\gnu}.\footnote
{A footnote.\index{Gnat@\string\gnat}}

\printindex
\end{document}
0

The solution by @Andrew Swann and @Ulrike Fischer (use \string to escape the backslash in expr) works fine, but requires repeated effort in all concerned footnotes or captions. Therefore the solution mentioned in the comment by @egreg,

\usepackage{bigfoot}

which solves the problem once and for all, appears preferable to me.

  • While bigfoot works fine in the above MWE, it causes serious trouble in my long report: it generates two empty pages at the end of each chapter. – Joachim W May 5 '15 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.