newcommand for index inserts unwanted spaces when using {}
You could use \string\emph
inside of \ind
, preventing a premature expansion of \emph
etc. and insertion of a stray space character.
\documentclass[a4paper]{article}
\usepackage{makeidx}
\makeindex
\newcommand{\ind}[1]{\index{#1@\string\emph{#1}}}
\begin{document}
Testing our self-defined command\ind{italictext}. This is the
normal way\index{italictext@\emph{italictext}}.
\printindex
\end{document}
LaTeX's \index
reads its argument as a verbatim argument to prevent the expansion of commands. If \index
is used in the argument of another macro, then the argument of \index
is already read and the switching to verbatim read mode of \index
comes too late.
That \index
is called in side \ind
can be fixed by defining \ind
in the same manner as \index
, see its definition in latex.ltx
:
\def\index{%
\@bsphack % space handling around `\index`
\begingroup % localize category code changes
\@sanitize % change category codes to verbatim
\@wrindex % reads argument, writes the index entry and ends the group
}
Example with new \ind
:
\documentclass[a4paper]{article}
\usepackage{makeidx}
\makeindex
\makeatletter
\newcommand*{\ind}{%
\@bsphack
\begingroup
\@sanitize
\@ind
}
\newcommand*{\@ind}[1]{%
\@wrindex{#1@\string\emph{#1}}%
}
\makeatletter
\begin{document}
Testing our self-defined command\ind{italictext}. This is the
normal way\index{italictext@\emph{italictext}}.
\printindex
\end{document}
Now, the two index entries (.idx
file) are equal:
\indexentry{italictext@\emph{italictext}}{1}
\indexentry{italictext@\emph{italictext}}{1}