2

I'm working on a book project which is divided in several parts. Basically at the end of every part I want an index. The index should only lists these words which are mentioned in this part. What is the easiest way to realise this?

In my MWE below you can see what I tried so far. I want the following:

  • INDEX 1 should list Aaa, Bbb and Ccc
  • INDEX 2 should list Aaa, Ddd and Eee

In my "solution" INDEX 2 lists the same as INDEX 1.

MWE

\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\makeindex


\begin{document}

\part{My Headline}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex % INDEX 1

\part{Two}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

    \index{Aaa}\index{Ddd}\index{Eee} % 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}.

1 Answers1

2
\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\usepackage{letltxmacro}
\makeindex[name=index1]

\makeindex[name=index2]


\makeatletter
\AtBeginDocument{%
\let\latex@@index\index
\renewcommand{\index}[1]{%
  \latex@@index[index\number\value{part}]{#1}%
}
}

\makeatother


\begin{document}

\part{My Headline}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex[index\number\value{part}] % INDEX 1

\part{Two}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex[index\number\value{part}] % INDEX 2

\end{document}

Improved version

Automatically defined \makeindex[index...] in a loop and \printindex

\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\usepackage{letltxmacro}

\makeatletter
\newcounter{loopcntr}
\newcommand{\setupindexfiles}[1]{%
\setcounter{loopcntr}{1}%
\loop\unless\ifnum\value{loopcntr}>#1%
\makeindex[name=index\number\value{loopcntr}]
\stepcounter{loopcntr}%
\repeat
}%




\setupindexfiles{3}


\AtBeginDocument{%
\LetLtxMacro\latex@@index\index
\LetLtxMacro\latex@@printindex\printindex
\renewcommand{\index}[2][]{%
  \def\first@arg{#1}%
  \ifx\first@arg\empty
  \latex@@index[index\number\value{part}]{#2}%
  \else
  \latex@@index[#1]{#2}%
  \fi
}
\renewcommand{\printindex}[1][]{%
  \def\first@arg{#1}%
  \ifx\first@arg\empty
  \latex@@printindex[index\number\value{part}]%
  \else
  \latex@@printindex[#1]%
  \fi
}
}


\makeatother


\begin{document}

\part{My Headline}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex

\part{Two}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex%




\part{Three}

    \chapter{My Headline}
    \blindtext[2]

    \chapter{My Headline}
    \blindtext[2]

    \index{Aaa}\index{FFFFFF}\index{AAAAAAA} % keywords for INDEX 2

    \printindex%



\end{document}