# redefining \index

I use several indices, e.g. 'names', 'subject' doing this with entries of the form \index{names}{Columbus}. These indices are created with the multind package.

Unfortunately some of the tex files have index commands of the form \index{Columbus}. I do not want to have to go through all of these files and replace \index{Columbus} with \index{names}{Columbus}.

Can I redefine \index to point to the correct index file?, i.e. from myfile.ind to names.ind

• \let\oldindex\index\renewcommand{\index}[1]{\index{names}{#1}} should work; it'd be great if you could provide us with a complete MWE to play with and test Feb 27 '14 at 20:22
• it's probably not helpful for this round, but for future documents, you should look into the imakeidx package. it uses the [...] optional notation to distinguish "other" indexes, so the basic index doesn't have to be specified. (there are other advantages as well, but that's perhaps the most obvious.) Feb 27 '14 at 21:09
• @cmhughes: I think this is going to need xparse as the OP wants \index{Columbus} to be the same as \index{names}{Columbus}. That is an optional parameter but using {}. And yes it would it would be very helpful if we had a fully compilable MWE including \documentclass and the appropriate packages that sets up a test case for the problem. Feb 27 '14 at 21:33

The syntax \index{names}{Columbus} and \index{Columbus} is ambigous, because the latter form could be followed by another curly braces group, e.g:

\index{abc}{xyz}


Is abc the register with entry xyz or is abc the name for the name register?

There are several ways to deal with the ambiguity:

• \index could check, whether the first argument is a known register name and uses then the two argument form.

Restrictions: \index must know all registers and the register name must not appear as entry.

• \index could read the first argument and check the next token. If an opening curly brace follows, use the two argument form, otherwise the first argument is already the name for the name register.

Restrictions: An opening curly brace must not follow the one-argument form.

The example below implements the latter case. \index is a little tricky:

• The entry argument is read verbatim with changed category codes to prevent the premature expansion. Rather the argument should go unmodified into the .idx file.

• \index tries to be invisble by looking for spaces before and after. It ignores a following space, if it has detected a previous. Therefore the code is surrounded by \@bsphack and \@esphack. In this case the commands of the original \index will continue to work, because the original \index is called after the argument checking.

Full example:

\documentclass{article}
\usepackage{multind}% Caution: old LaTeX 2.09 package
% Better alternatives:
% imakeidx: http://www.ctan.org/pkg/imakeidx
% index: http://www.ctan.org/pkg/index

\makeindex{names}

\makeatletter
\newcommand*{\org@index}{}
\let\org@index\index
\newcommand*{\index@param}{}
\renewcommand*{\index}{%
\begingroup % limit the scope for verbatim category codes
\@sanitize % verbatim category codes
\index@i
}
\newcommand*{\index@i}[1]{%
\gdef\index@param{#1}% remember the first argument in global macro
\endgroup
\futurelet\@let@token\index@ii % peek at next token
}
\newcommand*{\index@ii}{%
\ifx\@let@token\bgroup
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\org@index{\index@param}%
}{%
\org@index{names}{\index@param}%
}%
}
\makeatother

\begin{document}
\index{names}{Columbus}

\index{Knuth}

% Test spaces
[ \index{test} ][\index{test} ][ \index{test}][\index{test}]

\printindex{names}{Name register}
\end{document}
`