[Tex/LaTex] Customizing indexentry

indexing

I am trying to create an index manually, where each index entry refers to a page and the corresponding chapter. So basically I am creating my own idx file, then use Latex's MakeIndex functionality to get ind file, and then produce the corresponding PDF file.

So my tex file simply looks like:

\documentclass[a4paper,10pt]{extarticle}
\usepackage[10pt]{extsizes}
\usepackage{makeidx}
\begin{document}
\printindex
\end{document}

My idx file has entries as follows:

\indexentry{Aims of regulation}{3}
\indexentry{Cost of regulation}{4}

Now I was wondering how to edit my indexentry command to allow for a chapter number, so the index looks something like:

Aims of regulation, …… Ch5.p3

Cost of regulation, ……. Ch5.p4

I was trying to use:

\indexentry{Aims of regulation}{Ch5.p3}
\indexentry{Cost of regulation}{Ch5.p4}

but that doesn't work since I assume only numbers are accepted.

My aim is simply to make use of MakeIndex so a sorted .ind file is created automatically, which I then simply print to a PDF file.

Any suggestions?

Thanks.

Best Answer

Makeindex supports composite page numbers. The following example uses the first part for the chapter number and the second part for the page number. A style file for makeindex is created to add macro markup that is defined to analyze the page number to add the formatting (adding Ch, dot, and p).

\documentclass[a4paper,10pt]{book}
\usepackage{makeidx}
\makeindex

\usepackage{etoolbox}
\makeatletter
% Write composite page numbers of the form <ch>-<pg>
\patchcmd\@wrindex{\thepage}{\thechapter-\thepage}{}{%
  \@latex@error{Patching `\string\@wrindex` failed}\@ehc
}

\newif\ifChapterPageRange
\def\ChapterPageRangetrue{\global\let\ifChapterPageRange\iftrue}
\def\ChapterPageRangefalse{\global\let\ifChapterPageRange\iffalse}
\newcommand*{\ChapterPage}[1]{%
  \expandafter\@ChapterPage\romannumeral-`\x#1\@nil
}
\def\@ChapterPage#1\@nil{%
  \@@ChapterPage#1--\@nil{#1}%
}
\def\@@ChapterPage#1-#2-#3\@nil#4{%
  \def\ChapterPage@Temp{#2}%
  \ifx\ChapterPage@Temp\@empty
    \global\let\ChapterPage@Chapter\@empty
    \romannumeral-`\x#4%
  \else
    \ifChapterPageRange
      \def\ChapterPage@Temp{#1}%
      \ifx\ChapterPage@Temp\ChapterPage@Chapter
        p#2%
      \else
        Ch#1.p#2%
      \fi
      \ChapterPageRangefalse
    \else
      Ch#1.p#2%
      \gdef\ChapterPage@Chapter{#1}%
    \fi
  \fi
}
\let\ChapterPage@Chapter\@empty
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{\jobname.mst}
page_compositor "-"
delim_0 ", \\ChapterPage{"
delim_1 ", \\ChapterPage{"
delim_2 ", \\ChapterPage{"
delim_n "}, \\ChapterPage{"
delim_r "}--\\ChapterPageRangetrue\\ChapterPage{"
delim_t "}\\ChapterPageRangefalse"
encap_infix "{\\ChapterPage{"
encap_suffix "}}"
\end{filecontents*}

\begin{document}
\chapter{First chapter}
\index{Aims of regulation|textbf}
\newpage
\null\index{Aims of regulation|textbf}
\newpage
\null\index{Aims of regulation|textbf}
\addtocounter{chapter}{3}
\chapter{Regulation}
Aims of regulation\index{Aims of regulation}
\newpage
Cost of regulation\index{Cost of regulation}

\printindex
\end{document}

Result

Raw index file \jobname.idx:

\indexentry{Aims of regulation|textbf}{1-1}
\indexentry{Aims of regulation|textbf}{1-2}
\indexentry{Aims of regulation|textbf}{1-3}
\indexentry{Aims of regulation}{5-5}
\indexentry{Cost of regulation}{5-6}

Sorted and formatted index file \jobname.ind:

\begin{theindex}

  \item Aims of regulation, \ChapterPage{
                \textbf{\ChapterPage{1-1}--\ChapterPageRangetrue\ChapterPage{1-3}}}, \ChapterPage{
                5-5}\ChapterPageRangefalse

  \indexspace

  \item Cost of regulation, \ChapterPage{5-6}\ChapterPageRangefalse

\end{theindex}

Remarks:

  • The index style file \jobname.mst is automatically loaded by makeindex.
  • Support for the encap and page range features of makeindex.
  • In page ranges, the second chapter prefix is suppressed if the chapter is the same.
  • The \romannumeral-\x` trick removes a starting space.
  • The format "Ch.p" is not looking too good.
Related Question