# \index*{phrase with spaces} shows visible spaces (with the index package)

I use the index package, and in particular its starred variant of the \index command: \index*{word} both typesets word and adds it to the index.

When indexing a phrase, the spaces are typeset as character 32 in the current font, instead of being spaces. For example I get a stroke with CM fonts, or a ␣ with T1 fonts. This is only a problem in the running text, the index contains a normal space (unsurprisingly as a single space is written to the .idx file).

\documentclass{report}
\usepackage{index}
\makeindex
\begin{document}
\index*{hello  world}
\printindex
\end{document}


If the source contains multiple spaces, they're all typeset. I presume that \index* is overenthusiastically changing catcodes but I don't know how to fix that.

Is there a way to fix this without ditching the index package? If not, what other package(s) provide \index* and supports multiple indexes with \index[things]{word}?

• \imakeidx supports multiple indexes, but has no \index*, but this should be possible to obtain. index itself does only change the catcodes for _ and ^ – user31729 May 26 '16 at 18:58

You can patch index to rescan the argument when necessary to print it.

\documentclass{report}
\usepackage{index}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@@index}
{\@silentindextrue #2}
{\@silentindextrue\scantokens{#2\relax}}
{}{}
\makeatother
\makeindex
\begin{document}
X\index*{hello  world}X
\printindex
\end{document}


The .idx file contains

\indexentry {hello world}{1}


A work around with xparse and imakeidx.

Such things with stuff!substuff aren't useful with \index* however, in my point of view.

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{letltxmacro}

\makeatletter

\AtBeginDocument{%
\LetLtxMacro\@egregsindex\index
\RenewDocumentCommand{\index}{som}{%
\IfBooleanT{#1}{%
#3%
}%
\IfValueTF{#2}{%
\@egregsindex[#2]{#3}%
}{%
\@egregsindex{#3}%
}%
}%
}

\makeatother

\makeindex
\makeindex[name=myotherindex]
\begin{document}
\index*{hello  world}
\index{Other stuff}
\index[myotherindex]{Other index entry}
\index*[myotherindex]{Other index entry again}
\printindex

\printindex[myotherindex]
\end{document}