# Multiple Indexes: one index for one or more chapters

This is a follow-up question to Multiple Indexes: one index for every part of the book!

I'm working on a book project which is divided in several parts and chapters. At the end of some chapters (let's say at the end of chapter 2 and 3) I want an index which lists all the keyword mentioned since the last index.

Example

• Keywords in Chapter 1: \index{Aaa}\index{Bbb}\index{Caa}
• Keywords in Chapter 2: \index{Aaa}\index{Ddd}\index{Eee}
• Keywords in Chapter 3: \index{Aaa}\index{Ddd}\index{Fff}

I want an index at the end of chapter 2, which lists all keywords of chapter 1 and 2. At the end of chapter 3 there should be an index too, which lists only the keywords of chapter 3.

Here is what I've tried so far (MWE):

\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\makeindex

\begin{document}

\blindtext[2]

\index{Aaa}\index{Bbb}\index{Caa} % keywords for INDEX 1

\blindtext[2]

\index{Aaa}\index{Ddd}\index{Eee} % keywords for INDEX 1

\printindex % INDEX 1

\index{Aaa}\index{Ddd}\index{Fff} % keywords for INDEX 2

\printindex % INDEX 2

\end{document}

What I don't want

I know about splitidx. In the manual it says:

Separate indices are declared and given unique shortcut identiers with the \newindex command.

My question is: Is there a way to avoid the \nexindex command? The book is already written and I don't want to change for example \index{Aaa} to \newindex{index1}{Aaa}.

• The last objection isn't that critical: you can always redefine command \index to mean \newindex{index1}. In that way you would only have to do minor changes, each time you need a new index. – Christoph Frings Aug 3 '16 at 18:22
• I'll try to 'attack' this problem later on... in fact, the other (newer) question seems to be very related – user31729 Aug 4 '16 at 7:59
• @ChristianHupfer: Yes, actually I was inspired by Christoph's comment to the question you mentioned. Should I close this one? – Sr. Schneider Aug 4 '16 at 8:10

You can do as follows:

\documentclass{book}
\usepackage{imakeidx}
\usepackage{xpatch}
\usepackage{blindtext}

\makeindex

\makeatletter
\let\makepartialindex\makeindex
\AtBeginDocument{%
\edef\imki@jobname{\imki@jobname\noexpand\theidx@count}%
}
\newcounter{idx@count}
\xapptocmd{\printindex}{\stepcounter{idx@count}}{}{}
\makeatother

\begin{document}

\makepartialindex

\blindtext[2]

\index{Aaa}\index{Bbb}\index{Caa} % keywords for INDEX 1

\blindtext[2]

\index{Aaa}\index{Ddd}\index{Eee} % keywords for INDEX 1

\printindex % INDEX 1

\makepartialindex

\index{Aaa}\index{Ddd}\index{Fff} % keywords for INDEX 2

\printindex % INDEX 2

\end{document}

In order to accomplish the reuse of the same index file, all that needs to happen is for us to reopen the file for writing, thus clobbering the original. Try sticking this in your preamble:

\makeatletter
\renewcommand*{\printindex[1]}[\imki@jobname]{%
\@ifundefined{#1@idxfile}{\imki@error{#1}}%
{\imki@putindex{#1}%
\immediate\openout\csname #1@idxfile\endcsname #1.idx}%
}
\makeatother

This redefines \printindex to reopen the file for writing after it's done printing the first index.

As written, I don't think it'll work with the splitindex option. Maybe I'll write one for that later.