# Defining synonyms for indexing

Is it possible to set things up so that indexing synonyms does not generate multiple entries? For example, if the text includes \index{cur}, \index{hound}, and \index{mutt}, can these be coalesced under the entry “dog”? (Assume these keys are being automatically-generated; otherwise I’d just type \index{dog} every time.)

Background: LaTeX3/expl3 offers the possibility of defining certain variants of functions together. In terms of the (experimental) class l3doc, an example might look like this:

% \begin{macro}[pTF]{\bool_if:N}
%   Blah blah blah.
%    \begin{macrocode}
\prg_new_conditional:Npnn \bool_if:N #1 { p , T , F , TF }
{ … }
%    \end{macrocode}
% \end{macro}


In the documented code listings, this displays both \bool_if:N(TF) and \bool_if_p:N in the margins; in the generated code, this defines the four functions \bool_if_p:N, \bool_if:NT, \bool_if:NF, and \bool_if:NTF.

[Note: The notation \bool_if:N(TF) is the closest I can come on this site to showing the actual notation; but it’s close enough for this discussion.]

The trouble is that the automatic indexing of csnames from doc.sty indexes this line as an instance of the non-existent function \bool_if:N!

[Note: l3doc adjusts catcodes so the colon & underscore are properly recognized.]

So…. Is there a way to tell the index-generation that the function variants are synonyms? I.e., can the various places within the code where the variants are used be coalesced under one index entry? (The entry would probably look something like \bool_if:N(TF), \bool_if_p:N.)

Alternate option: Is it possible to set things up so the single key \bool_if:N is recognized not as itself but as an instance of the four variants that actually exist?

-

If you don't need anything fancy in the \index argument (such as @ or !), then this should work:

\documentclass{article}
\usepackage{makeidx}
\makeindex

\usepackage{xparse}
\ExplSyntaxOn
\cs_set_eq:NN \solomon_index:n \index

\NewDocumentCommand{\definesynonyms}{ m m }
{
\solomon_define_synonyms:nn { #1 } { #2 }
}

\cs_new_protected:Npn \solomon_define_synonyms:nn #1 #2
{
\clist_map_inline:nn { #2 }
{
\tl_gset:cn { g_solomon_syn_##1_tl } { #1 }
}
}

\RenewDocumentCommand{\index}{ m }
{
\tl_if_exist:cTF { g_solomon_syn_#1_tl }
{
\solomon_index:v { g_solomon_syn_#1_tl }
}
{
\solomon_index:n { #1 }
}
}
\cs_generate_variant:Nn \solomon_index:n { v }
\ExplSyntaxOff

\definesynonyms{dog}{cur,hound,mutt}

\begin{document}
\show\index
A\index{dog}
\newpage
B\index{cur}
\newpage
C\index{hound}
\newpage
D\index{mutt}
\newpage
\printindex
\end{document}


This is the contents of the generated .idx file:

\indexentry{dog}{1}
\indexentry{dog}{2}
\indexentry{dog}{3}
\indexentry{dog}{4}

-
Cool! I’ll post a link to this on the LaTeX3 Issues list. – J. C. Salomon Nov 4 '12 at 1:03