# How to produce a combined index from multiple documents?

I'm creating a set of documents which are contained in different LaTeX source files and are going to be compiled into different PDF files. Each one can have its own index, generated using the usual makeindex procedure. But since this documents cover related topics, I'd like to be able to produce one master index that contains all the terms from all the documents. Obviously the references in the index, instead of just being page numbers, will have to include both the page number and some sort of identifier for the document. Is there a way to do this?

Of course, I'm sure I could write a script to post-process the .idx files, but I'd prefer to use something existing.

If I need to dump makeindex and use some alternative, that should be fine.

-

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.

-