9

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.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. your wish is something i'd like too, as one of my "future" projects is indexing tugboat, where volumes and issues are involved as well as page numbers. – barbara beeton Aug 7 '13 at 13:24
9

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.
  • 1
    Just got back from my holiday hence my late reply. Thanks for your answer. Very helpful. I agree that Ch p style is not the best. Now I am simply using 1-1, 5-3 and so on. – Barbados Aug 18 '13 at 21:57
  • Hi can you have a look at my follow up question below. My follow up question is too large to put in this comment box, and starting a new topic didn't make sense to me since it is a follow-up on your solution. Thanks. – Barbados Aug 21 '13 at 11:22
5

This workaround is the best I've been able to achieve... Hope it can be useful to you.

First of all, create a custom .ist file, call it myfile.ist and put it where your .tex file is.

myfile.ist

delim_0 ", \\dotfill Ch0.p"
delim_1 ", \\dotfill Ch0.p"
delim_2 ", \\dotfill Ch0.p"
delim_n ", Ch0.p"

Then suppose this is your .tex file

myfile.tex

\documentclass{book}
\usepackage{makeidx}

\makeindex

\begin{document}

\chapter{First}

Cost of regulation\index{Cost of regulation}. Aims of regulation\index{Aims of regulation}.

\chapter{Second}

Aims of regulation\index{Aims of regulation}.

\printindex

\end{document} 

At this point, run the sequence:

pdflatex myfile

makeindex -s myfile.ist myfile.idx

Now open the file myfile.ind which should have the contents:

\begin{theindex}

  \item Aims of regulation, \dotfill Ch0.p1, Ch0.p3

  \indexspace

  \item Cost of regulation, \dotfill Ch0.p1

\end{theindex}

Replace the chapter numbers with the correct ones:

\begin{theindex}

  \item Aims of regulation, \dotfill Ch1.p1, Ch2.p3

  \indexspace

  \item Cost of regulation, \dotfill Ch1.p1

\end{theindex}

At this point, run again

pdflatex myfile

and you will have

enter image description here

  • Just got back from my holiday hence my late reply. Thanks for your answer. Nice workaround. I have decided not to use the Ch p style anymore. Now I am simply using 1-1, 5-3 and so on. – Barbados Aug 18 '13 at 21:59
  • How to make them italic? I suspect to use delim_0 ", \itshape " but in this case the italic shape is not locally applied to page number; it goes to text items also. – Sigur Apr 9 at 23:03
0

I have adjusted your code as follows:

\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
        #2%
                %p#2%
      \else
              \textbf{#1}:#2%
        %Ch#1:p#2%
      \fi
      \ChapterPageRangefalse
    \else
          \textbf{#1}:#2%
      %Ch#1:p#2%
      \gdef\ChapterPage@Chapter{#1}%
    \fi
  \fi
}
\let\ChapterPage@Chapter\@empty
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{\jobname.mst}
heading_prefix "{\\bfseries\\hfil " % Insert in front of letter 
heading_suffix "\\hfil}\\nopagebreak\n" % Append after letter 
headings_flag 1 % Turn on headings (uppercase)

page_compositor "-"
delim_0 "\\dotfill \\ChapterPage{"
delim_1 "\\dotfill \\ChapterPage{"
delim_2 "\\dotfill \\ChapterPage{"
delim_n "}, \\ChapterPage{"
delim_r "}--\\ChapterPageRangetrue\\ChapterPage{"
delim_t "}\\ChapterPageRangefalse"
encap_infix "{\\ChapterPage{"
encap_suffix "}}"
\end{filecontents*}

So now an index entry is produced that looks like for example:

Cost of regulation .......... 1:1

But for example, if I have the same index entry on a different page, say page 4, my index entry will look like:

Cost of regulation ...... 1:1, 1:4

How would I adjust the code to get something like:

Cost of regulation ......... 1:1, 4

Thanks.

Apologies - I know this is asking for help and shouldn't be done here. But the comment box doesn't allow me to type in this much.

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.