# How can I have a word index for each chapter as well as a word index for the whole book?

In other words, just the way I get a table of contents at the beginning of each chapter with titletoc, I would like to have a word index at the end of each chapter. (In addition to the index for the whole book at the end of the book.)

-

Here's a prototype using the imakeidx package.

\documentclass{book}
\usepackage[noautomatic,splitindex]{imakeidx}

%%% Allocate enough chapter indices
\count255=0
\loop\ifnum\count255<50
\begingroup\edef\x{\endgroup
\noexpand\makeindex[name=\number\count255 chap,title=Word index for Chapter \number\count255]}\x
\repeat
%%% A global index
\makeindex

%%% The user level macros
\newcommand{\gindex}[1]{%
\index{#1}% global index
\expandafter\index\expandafter[\thechapter chap]{#1}%
}
\newcommand{\printchapterindex}{%
\begingroup
% We want the index as an unnumbered section
\let\chapter\section
% Also disable the page break
\let\cleardoublepage\relax
% Print the index
\expandafter\printindex\expandafter[\thechapter chap]%
\endgroup}

\begin{document}

\mainmatter

\chapter{First}

First\gindex{first} and abc\gindex{abc}

\printchapterindex

\chapter{Second}

Second\gindex{second} and def\gindex{def}

\printchapterindex

\printindex

\end{document}


We can't use the automatic features of imakeidx that can work only if the indices are the last thing in the document (or, more precisely, nothing to be indexed follows one of the indices), so the procedure is to call

pdflatex filename
splitindex filename
pdflatex filename


Image of Chapter 1

Image of Chapter 2

Image of the global index

## Appendix: how to add an engine to TeXShop for building this file

1. Navigate in the Finder opening the folder ~/Library/TeXShop/Engines (where ~ stands for your Home.

2. Duplicate the XeTeX.engine file.

3. Rename the newly created file to splitindex.engine.

4. Right click on this file and choose "Open with TeXShop.app"

5. Change the entire contents of the file with

#! /bin/bash
PATH=/usr/texbin:/usr/local/bin:${PATH} bfname=${1%\.*}
splitindex "$bfname"  6. Save the file and quit TeXShop On reopening TeXShop the dropdown menu next to the Typeset button will have an entry called splitindex When you want to run splitindex, just select the item from the dropdown menu and push the Typeset button. This is equivalent to running the command splitindex filename  I talked before of. For the next typesetting, choose your default engine from the menu, be it LaTeX or Latexmk. - Didn't expect such a fast answer so I don't have the time to try right now. I don't understand where the smaller "Chapter" heading comes from but I guess a bit of experimenting will let me find out. In any case, very grateful regards. schremmer – schremmer Jan 21 at 18:00 @user15042 "Chapter 1", "Chapter 2" and "Final index" are just tags preceding the images. I'll make it clearer. – egreg Jan 21 at 18:05 Got it. Seems just like what I wanted. Will report in a couple of days. Very grateful regards. – schremmer Jan 21 at 18:20 Couldn't resist trying. The pdf doesn't show any index. Looked at imakeidx doc but to no avail. I don't know how to enter the console properly here but some of it is: Chapter 2. No file Stack-2chap.ind. [3] [4] No file Stack-Stack.ind. Package imakeidx Warning: Remember to run (pdf)latex again after calling (imakeidx) splitindex' and processing the indices. I tried to use pdflatexmk (which I always use) but no change. I am using TeXLive2011 under OSX10.5.8 on a PPC. Best regards. – schremmer Jan 21 at 19:45 @schremmer You don't call makeindex; it's splitindex that takes care of that. – egreg Jan 21 at 20:16 show 10 more comments It appears that latexmk can't detect that an idx file has been written via the imakeidx package so "makeindex" isn't run. I placed "makeindex" in quotes because what latexmk actually runs is determined by a Perl variable, $makeindex, and that can be changed in a configuration file. As far as I can tell, if latexmk could detect that the idx file was created via the log file one need only set the \$makeindex variable to run splitindex (which splits the index and runs makeindex on each split) with the appropriate argument. I'd get in touch with John Collins, the maintainer of latexmk, and discuss the what has to be done with him.

PS: the splitindexmk engine for TeXShop that I wrote is really a fudge that takes advantage of the fact that latexmk does nothing. Basically it first runs pdflatexmk (an TeXShop engine using latexmk), then splitindex and finally a single run of pdflatex` with appropriate options so that TeXShop can sync properly and also Go

-