# Automatically squashing subentries in indexes

I have a long LaTeX file use to generate several variants of a document, including or excluding a different collection of sections. The document has an index of people generated with \index{Doe!John} which are turned into an index using makeindex. However, if there's only one person surnamed Doe, I'd like to collapse this to a single line, as if it were generated with \index{Doe, John}. I can't easily modify the \index invocations in the document because often the number of people named Doe will depend on which sections are being excluded. Is there a way of automating this?

Edit Example code:

\documentclass{article}
\usepackage{makeidx,comment,xspace}
\makeindex
\newcommand{\name}[2]{#1\ #2\index{#2!#1}\xspace}

% Either include or exclude this section
\includecomment{extra}
%\excludecomment{extra}

\begin{document}
\name{John}{Doe} and \name{Joe}{Bloggs}.

\begin{extra}
\name{Jane}{Doe}.
\end{extra}

\printindex
\end{document}


I'd like the index to read as follows when extra is included:

Bloggs, Joe, 1
Doe,
Jane, 1
John, 1


and as follows when extra is excluded:

Bloggs, Joe, 1
Doe, John, 1

• It would be helpful if you composed a fully compilable MWE that sets up the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. That way those trying to help can focus on the solution as opposed to setting up the problem. – Peter Grill Jun 5 '14 at 19:54
• Added MWE as requested. – richard Jun 5 '14 at 20:30

The following example redefines \item of environment theindex: If only one \subitem follows, then the entry is merged.

An additional style file \jobname.mst is used to keep the programming costs smaller. An empty line is added before each \item, thus that each \item should end with an empty line, which becomes \par in TeX. Makeindex will automatically look for a style file with extension .mst.

\documentclass{article}
\usepackage{makeidx,comment,xspace}
\makeindex
\newcommand{\name}[2]{#1\ #2\index{#2!#1}\xspace}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.mst}
item_0 "\n\n  \\item "
\end{filecontents*}

\makeatletter
\let\org@item\item
\let\item\name@item
}
\def\name@item#1\par{%
\name@item@analyze#1\subitem\subitem\@nil{#1}%
}
\def\name@item@analyze#1\subitem#2\subitem#3\@nil#4{%
\def\name@item@test{#2#3}%
\ifx\name@item@test\@empty
\org@item#1\par
\else
\def\name@item@test{#3}%
\ifx\name@item@test\name@item@subitem
\org@item#1\ifhmode\unskip\fi, #2\par
\else
#4\par
\fi
\fi
}
\def\name@item@subitem{\subitem}
\makeatother

% Either include or exclude this section
\includecomment{extra}
%\excludecomment{extra}

\begin{document}
\name{John}{Doe} and \name{Joe}{Bloggs}.
\name{Foo}{Bar}

\begin{extra}
\name{Jane}{Doe}.
\end{extra}

\printindex
\end{document}


File \jobname.ist:

\begin{theindex}

\item Bar
\subitem Foo, 1

\item Bloggs
\subitem Joe, 1

\indexspace

\item Doe
\subitem Jane, 1
\subitem John, 1

\end{theindex}


• Thanks. The warnings about overwritten files can be removed by adding: \usepackage{silence}\WarningFilter{latex}{Overwriting file} – richard Jun 6 '14 at 0:20