# Index for part of document

I have a document with hundreds of \index entries. I'm trying to generate the index for some part of the document.

The best I could come up with is to disable the \index command temporarily. I did it like this:

\let\origindex\index
\newcommand\indexon{\let\index\origindex}


I use it like this to generate the index of, say, chapter 2 only:

\indexoff
\include{chapter1}
\indexon
\include{chapter2}
\indexoff
\include{chapter3}


Is there a better or cleaner way? (My experience with this sort of macros is rather limited. I'm not even sure what \let does exactly.)

-
There would be other syntax choices but that looks OK to me. \let defines the new command to have the same definition as the old one so it's like going \newcommand\origindex{... whatever the definition of \index is ....} –  David Carlisle Jul 4 '12 at 19:44
–  cmhughes Jul 4 '12 at 19:45

You do not have to do all these changes all over the place, there is in fact a very simple out of the box solution.

As you are using \include you can use \includeonly to process only some of your included files (and I guess that fits with what you are looking for anyway :-). In that case the .idx will only contain index entries from the files that you included, e.g., in your example use

\includeonly{chapter2}


and you get only the entries from the file chapter2.tex. Or if you want chapter2 and 3 use

\includeonly{chapter2,chapter3}


The \include mechanism will save information for cross-references and the TOC but it will not preserve data that goes directly to files other than the .aux files. Thus this offers you precisely what you need and the page references will be correct too as long as you use the include mechanism correctly, i.e., all chapters have been compiled or recompiled when earlier chapters got changed.

There are also packages that offer an interactive usage for \includeonly, for example, the package askinclude.

-