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
\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}
  • A first version, I will update – user31729 Aug 3 '16 at 11:23
  • Thanks so far. How can I modify your solution if we're talking about this situation: INDEX 1 should list all keywords of CHAPTER 1 AND 2 and INDEX 2 should list all keywords of CHAPTER 3 TO 5? – Sr. Schneider Aug 3 '16 at 11:36
  • @Sr.Schneider: That's a question change, actually. That's not in your post above and I did not address this! – user31729 Aug 3 '16 at 11:39
  • @Christan: Should I ask this separately? – Sr. Schneider Aug 3 '16 at 11:40
  • @Sr.Schneider: Of course! – user31729 Aug 3 '16 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.