It is possible to do this without any post processing except concatenation of the .idx
files for the generation of the master index, but it needs a bit of macro juggling.
The trick is that in the separate documents, you have to make some low-level changes to the \index
command so that it includes what makeindex calls 'encapsulation'. You can do this by adding a | symbol followed by a macro name (without preceding backslash). Then you can give the \jobname
as an argument to that macro name, like so:
\documentclass{article}
\usepackage{makeidx}
\makeindex
\let\LATEXindex\index % save old definition to prevent recursion
\renewcommand\index[1]{\LATEXindex{#1|docname{\jobname}}}
This will create .idx
entries that look like this:
\indexentry{alpha|docname{testdoc}}{1}
which, after running makeindex
, is converted into the following .ind
entry:
\item alpha, \docname{testdoc}{1}
Now, back in the separate documents, you have to add a definition for the \docname
macro, for example like this:
\newcommand\docname[2]{#2}
After that, the separate documents should compile as before, except for any index entries that already used encapsulation (you will have to fix those manually).
Now for the global index creation all you have to do is concatenate all the separate .idx
files into a single file, run makeindex on the result, and use an input file like this:
\documentclass{article}
\usepackage{makeidx}
\newcommand\docname[2]{#1: #2}
\begin{document}
\printindex
\end{document}
Be careful: this document should not contain a \makeindex
command itself, or at least you should never run makeindex
for this file, as that will overwrite the combined .ind
file.