2

I have a problem using the \index command in an argument to a user-defined command. For some reason, the problem only appears when I use the @ symbol. The following example illustrates the problem:

\documentclass[a4paper]{book}
\usepackage{makeidx}

\makeindex

\newcommand\myFoo[1]{#1}

\begin{document}

\chapter{The first chapter}

\index{one}
\index{\texttt{two}}

\myFoo{\index{one}}
\myFoo{\index{\texttt{two}}}

\printindex
\end{document}

When I typeset this source code (with the pdflatex and makeindex commands), the index looks like this:

two, 1
two, 1

one, 1

So, the "two" entry is displayed twice (in typewriter font), the "one" entry is (correctly) only displayed once. The same problem occurs when I use the @ operator.

The idx file generated by LaTeX is:

\indexentry{one}{1}
\indexentry{\texttt{two}}{1}
\indexentry{one}{1}
\indexentry{\texttt  {two}}{1}

I guess the problem is caused by these two spaces before the { character in the last index entry, but I do not understand why these spaces are there.

Can somebody explain why entries with formatting commands and entries without them give different results here, and suggest a solution?

Hoopje
  • 121
  • Welcome to TeX.SX! I'm quite sure this is a duplicate, which doesn't mean it's a bad question. – egreg Dec 02 '14 at 16:33
  • This is related: http://tex.stackexchange.com/questions/88354/index-entries-duplicated-for-captions – egreg Dec 02 '14 at 16:37
  • @egreg. I did search for related questions, but I could only find questions about using \index in command definitions, not in command arguments. But yes, the question you linked, and especially your answer there, are very helpful. Thanks! – Hoopje Dec 03 '14 at 09:11

0 Answers0