# \index command adding spaces after underscores

I am writing a document with an index of commands that may contain an underscore symbol. Thus, I use entries like:

\index{a\_b}


However, the .idx file contains code of the form:

\indexentry{a\_ b}{23}


Of course, the resulting document reads "a_ b", which is obviously not what I want. Is there a way to avoid this behavior ? Thanks !

Here is a small example:

\documentclass{book}
\RequirePackage[T1]{fontenc}
\RequirePackage{multind}
\newcommand{\dcommand}[1]{\texttt{#1}\index{commands}{\texttt{#1}}}
\begin{document}
\dcommand{foo\_bar}
\dcommand{bar\_foo}
\printindex{commands}{Commands index}
\end{document}


[sorry for the time taken to edit, I am working on several projects including some very urgent ones]

• Welcome to TeX.SX! This doesn't happen in my experiment; can you add a minimal working example (MWE)? – egreg May 26 '16 at 16:40
• This post hasn't improved since the question was asked :-( – user31729 May 28 '16 at 9:17
• Are you sure you want to use a package made for LaTeX 2.09 and unmaintained since 1991? – egreg May 29 '16 at 14:31

The multind package is made for LaTeX 2.09 and has been unmaintained since 1991.

You can get the same effect (and much more) with imakeidx and just small changes.

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{imakeidx}

\makeindex[name=commands,title=Commands index]

\newcommand{\dcommand}[1]{\texttt{#1}\index[commands]{\texttt{#1}}}

\begin{document}

\dcommand{foo\_bar}
\dcommand{bar\_foo}

\printindex[commands]

\end{document}


If you insist in using multind, fix the definition of \@wrindex by using \protected@xdef instead of \xdef.

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{multind}

\makeatletter
\def\@wrindex#1#2{\let\thepage\relax
\protected@xdef\@gtempa{\@ifundefined{#1@idxfile}{}{\expandafter
\write\csname #1@idxfile\endcsname{\string
\indexentry{#2}{\thepage}}}}\endgroup\@gtempa
\if@nobreak \ifvmode\nobreak\fi\fi\@esphack}
\makeatother

\newcommand{\dcommand}[1]{\texttt{#1}\index{commands}{\texttt{#1}}}
\begin{document}
\dcommand{foo\_bar}
\dcommand{bar\_foo}
\printindex{commands}{Commands index}
\end{document}