I'm creating a technical document with lots of index entries. To reduce typing I defined an index command that typesets the word to be indexed using \emph{}:
\newcommand{\ind}[1]{\index{#1@\emph{#1}}}
I turns out the .ind file gets different entries when I use this command, compared to typing the \index{} command myself: the entries created with the \ind{} command get two spaces inserted before the { of the \emph command. Consequently, the index will contain two entries for the same word if I use both the \ind{} command and the \index{} command (e.g. when defining a region using
\index{someword@\emph{someword}\(}
to start a region for that word.
Here is an MWE:
\documentclass[a4paper]{article}
\usepackage{makeidx}
\makeindex
\newcommand{\ind}[1]{\index{#1@\emph{#1}}}
\begin{document}
Testing our self-defined command\ind{italictext}. This is the
normal way\index{italictext@\emph{italictext}}.
\printindex
\end{document}
The contents of the .ind corresponding file is:
\begin{theindex}
\item \emph {italictext}, 1
\item \emph{italictext}, 1
\end{theindex}
Note 1: my actual use case is not with \emph{}, but with \lstinline{} from the listings package, but this simplified example already shows the problem. I observed the same behaviour when using the imakeidx package.
Note 2: using \DeclareRobustCommand instead of \newcommand doesn't change anything.
Note 3: I am actually using a KOMA class documentclass, so the scrindex package would be an option since the spacing problem doesn't appear there (in fact, both entries end up as one index entry with a single space between the \emph and opening { in the .ind file. However, with that package I haven't managed to get the page numbers for the index entries hyperlinked when using the hyperref package.
\lstinlinewould work inside of\indexhowever – Jan 28 '18 at 10:18