5

I'm trying to include an index at the end of my document using \usepackage{imakeidx} and the \makeindex and \printindex commands.

I would like the index to mention the section number as well, for instance:

37. Section 37 Make a mention of the word index

Index

index, s37, p1

Is this possible, and if yes, how?

lockstep
  • 250,273
konewka
  • 377

1 Answers1

6

Using xindy as index generator, it is possible to write additional information to index file, such as the section number s37,p100 or similar.

The relevant command is imki@wrindexentrysplit, the format can be specified in its 3rd argument, see the lines there.

However, xindy does not recognize this format initially, so there has to be an additional xindy module, the usage of it is specified within the options=-M sectionindex_sectionpage.xdy option to \makeindex.

I used a forloop to generate some dummy sections with dummy index entries, just for convenience. The index shows the format s{sectionnumber},p{pagenumber} .

\documentclass[paper=a4,12pt]{scrartcl}

\usepackage{blindtext}%
\usepackage{fmtcount}%
\usepackage{forloop}% 

\usepackage[xindy]{imakeidx}%




\makeatletter
% Global redefinition of indexentry to use section, then page%
\renewcommand{\imki@wrindexentrysplit}[3]{%
 \expandafter\protected@write\csname#1@idxfile\endcsname{}%
    {\string\indexentry{#2}{s\arabic{section},p\thepage}}%
}%
\makeatother


\makeindex[options=-M sectionindex_sectionpage.xdy]

\begin{document}

\newcounter{loopcounter}

\forloop{loopcounter}{1}{\number\value{loopcounter} < 13}{%
\section{Section \Numberstring{loopcounter}}

\blindtext

\vspace{\baselineskip}

This is a dummy index entry to \textbf{\Numberstring{loopcounter}}\index{\Numberstring{loopcounter}}

}



\printindex


\end{document}

The file sectionindex_sectionpage.xdy contains

( require "tex/inputenc/latin.xdy")
( require "texindy.xdy" )
( require "page-ranges.xdy") 
( require "book-order.xdy")


( define-location-class "sectionfirstthenpages"
                        ("alpha" :sep "" "arabic-numbers" :sep "," "alpha" :sep "" "arabic-numbers" ))

enter image description here

  • can this be done without xindy? just using package index using makeindex. If put \label for each section and put \ref{thatsectionlabel} in index arument it works..but i have many sections in a large document – Levan Shoshiashvili May 20 '15 at 19:35
  • @LevanShoshiashvili: I don't know at the moment, I'll take a look later on –  May 21 '15 at 04:21
  • This seems to break linking when using \usepackage{hyperref}. Does anyone know of a way make both work together? – BCS Aug 15 '21 at 23:00