3

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.

The output should look like this

\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\makeindex[name=1]
\makeindex[name=2]

\begin{document}

\part{My Headline}

    \chapter{My Headline}
    \blindtext[2]

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

    \chapter{My Headline}
    \blindtext[2]

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

    \printindex[1] % INDEX 1

    \chapter{My Headline}

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

    \printindex[2] % INDEX 2

\end{document}

The problem is that the book is already written and I don't want to change all \index{} commands to \index[1]{} or \index[2]{} manually. The idea is to redefine the \index{} command depending on the chapter number.

I'm looking for something like:

\if \thechapter < 3
    \renewcommand{\index}{\index[1]}
\else
    \renewcommand{\index}{\index[2]}
\fi

The code above doesn't work and I don't know why.

My file looks now like this

\documentclass{book}

\usepackage{blindtext}

\usepackage{imakeidx}
\makeindex[name=1]
\makeindex[name=2]

% here is something missing like:
%
% \if \thechapter < 3
%   \renewcommand{\index}{\index[1]}
% \else
%   \renewcommand{\index}{\index[2]}
% \fi

\renewcommand{\index}

\begin{document}

\part{My Headline}

        \chapter{My Headline}
        \blindtext[2]

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

        \chapter{My Headline}
        \blindtext[2]

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

        \printindex[1] % INDEX 1

        \chapter{My Headline}

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

        \printindex[2] % INDEX 2

\end{document}
  • Your approach is syntactically and logically wrong: \if \thechapter can't work and even if it would work, at the time of writing the command it does not what you want – user31729 Aug 4 '16 at 7:23
3

This is a code similar to the other answer to a question by the OP I provided here

First grab the original definition of \index, taking care of the optional argument introduced by imakeidx and then branching into the original version if \index[...]{foo} is used or checking for the chapter number with

\ifnum\value{chapter} < 3
\else
\fi

Of course, this does work for this particular purpose only.

\documentclass{book}

\usepackage{blindtext}
\usepackage{letltxmacro}
\usepackage{imakeidx}
\makeindex[name=1]
\makeindex[name=2]

\makeatletter

\LetLtxMacro\latex@@index\index
\renewcommand{\index}[2][]{%
  \def\@firstarg{#1}%
  \ifx\@firstarg\empty
  \ifnum\value{chapter} < 3
  \latex@@index[1]{#2}%
  \else
  \latex@@index[2]{#2}%
  \fi
  \else
  \latex@@index[#1]{#2}%
  \fi
}
\makeatother

\begin{document}

\part{My Headline}

\chapter{My Headline}
\blindtext[2]

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

\chapter{My Headline}
\blindtext[2]

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

\printindex[1] % INDEX 1

\chapter{My Headline}

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

\printindex[2] % INDEX 2

\end{document}
0

You can simply change the default value for the optional argument into the current chapter number.

\documentclass{book}

\usepackage[noautomatic]{imakeidx}

\indexsetup{level=\section*,toclevel=section,noclearpage}

\makeindex[name=1,intoc]
\makeindex[name=2,intoc]
\makeindex[name=3,intoc]

\usepackage{blindtext}

\makeatletter
% The default value for the optional argument is the jobname.
% Change it into the current chapter number.
\renewcommand{\index}{%
  \@bsphack
  \@ifnextchar[{\@index}{\@index[\arabic{chapter}]}%
}
\makeatother

\begin{document}

\tableofcontents

\part{My Headline}

\chapter{My Headline}
\blindtext[2]

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

\printindex[2] % INDEX 1

\chapter{My Headline}
\blindtext[2]

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

\printindex[2] % INDEX 2

\chapter{My Headline}

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

\printindex[3] % INDEX 3

\end{document}

I also added the necessary bits in order for the local indices to be printed correctly. The noautomatic option is mandatory, because otherwise the index files will be closed upon processing the first \printindex command.

enter image description here

For a sequence of indices not tied to chapters, a different strategy can be used: we define a counter that's stepped at each printing of an index and use that instead of the chapter counter like in the previous solution.

\documentclass{book}

\usepackage[noautomatic]{imakeidx}

\indexsetup{
  othercode={\stepcounter{printindex}},
}

\makeindex[name=1,intoc]
\makeindex[name=2,intoc]

\usepackage{blindtext}

\makeatletter
% The default value for the optional argument is the jobname.
% Change it into the current printindex value, that's stepped
% at each \printindex command.
\newcounter{printindex}
\setcounter{printindex}{1}
\renewcommand{\index}{%
  \@bsphack
  \@ifnextchar[{\@index}{\@index[\arabic{printindex}]}%
}
\makeatother

\begin{document}

\tableofcontents

\part{My Headline}

\chapter{My Headline}
\blindtext[2]

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

\chapter{My Headline}
\blindtext[2]

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

\printindex[1] % INDEX 1

\chapter{My Headline}

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

\printindex[2] % INDEX 2

\end{document}

A picture of the first index:

enter image description here

  • In your solution it's possible to combine the keywords for chapter 1 and 2 in one index at the end of chapter 2? – Sr. Schneider Aug 4 '16 at 8:52
  • @Sr.Schneider Of course not. I'll make another one. But I can't see what could be a reason for this. – egreg Aug 4 '16 at 8:57

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.