How to make index entries refer to section numbers and/or figure/table numbers, and replace comma with leaders?

In order to use section numbers instead of page numbers it is necessary to redefine the way, the index entry is written to \jobname.idx. That is basically easy, if the package imakeidx is used.

In its manual, imakeidx the author writes, that \imki@wrindexentry should be redefined to apply for other styles. Looking into the package, there is a similar command used with \thepage as argument, so redefined it, using \thesection.

However, makeindex as external processor complains about the new format if \thesection writes its numbers as 1.1 etc., i.e. with dots. Using xindy solves this problem, by applying a new alphabet or location-class, this is done in sectionindex_xindy.xdy, as well as removing the , separator and inserting ----- instead.

Content of sectionindex_xindy.xdy

(markup-locclass-list :open " ----- " :sep "")

( define-location-class "numericsections"
                        ("arabic-numbers" :sep "." "arabic-numbers"))

( define-location-class "numericchapteralphasections"
                        ("arabic-numbers" :sep "." "ALPHA"))

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

\usepackage{letltxmacro}
\usepackage[xindy]{imakeidx}

%\usepackage{hyperref}% Leads to linking to wrong positions%





\makeatletter
% Global redefinition of indexentry to section instead of page%
\renewcommand{\imki@wrindexentrysplit}[3]{%
 \expandafter\protected@write\csname#1@idxfile\endcsname{}%
    {\string\indexentry{#2}{\thesection}}%
}%


\LetLtxMacro{\LaTeXStandardFigure}{\figure}
\let\LaTeXStandardEndFigure\endfigure%

\renewenvironment{figure}[1][tpb]{%
%redefine the index write command to use figure number instead of section number
\renewcommand{\imki@wrindexentrysplit}[3]{%
\expandafter\protected@write\csname##1@idxfile\endcsname{}%
    {\string\indexentry{##2}{\thefigure}}
}%
\LaTeXStandardFigure[#1]%
}{%
\LaTeXStandardEndFigure%
}%

\LetLtxMacro{\LaTeXStandardTable}{\table}
\let\LaTeXStandardEndTable\endtable%



\renewenvironment{table}[1][tpb]{%
%redefine the index write command to use table number instead of section number
\renewcommand{\imki@wrindexentrysplit}[3]{%
\expandafter\protected@write\csname##1@idxfile\endcsname{}%
    {\string\indexentry{##2}{\thetable}}
}%
\LaTeXStandardTable[#1]%
}{%
\LaTeXStandardEndTable%
}%

\makeatother

\makeindex[options=-M sectionindex_xindy.xdy]

\renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}%
\renewcommand{\thesection}{\arabic{chapter}.\Alph{section}}%
\renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}%

\begin{document}
\listoffigures
\listoftables

\chapter{First}

\begin{figure}
\caption{A first figure with index entry, say Figure\index{Figure!First}}
\end{figure}


\section{First section of chapter~\arabic{chapter}}

Here is a table (well, the environment actually)

\begin{table}
\centering
\begin{tabular}{lll}
\hline
A & dummy & table \tabularnewline
\hline
\end{tabular}
\caption{A table with some index entry in it: \index{Table}{First}}
\end{table}
See also the keyword Index\index{Index}

\chapter{Two}

\section{First}
Index\index{Index} and its usage\index{usage}

\begin{figure}
\caption{A dummy figure with an index entry, say DummyFigure\index{Figure!DummyFigure}}

\end{figure}


\printindex


\end{document}

In order to make this work for figures/tables, I just redefined the command again within wrappers for figure and table environment. You have redefine \thefigure and \thetable explicitly, since it seems, that they introduce a trailing \relax command just before the figure/table number.

Please note, that this does not work correctly with hyperlinks so far,regarding the figure/table index entries.

Enabling --shell-escape is mandatory.

enter image description here


Well, I find another solution that is more elegant than Hupfer's, from my point of view.

Since makeindex(.exe) could only handle positive number as page numbers, and would complain about something like 1.1, which is, in general, the form of subsection numbers, we have two choices: bypassing this obstruction by using other indexing program, such as xindy (which is described by Hupfer); encoding 1.1 in base-100 number system, i.e. 101, before the .idx file is processed by makeindex, and modifying the .ind file before being loaded by \printindex.

To achieve this, I mean, the second choice, I borrowed some code from [email protected] and [email protected]. And finally, I got the following:

\begin{filecontents*}{indexsec.ist}
preamble = "\begin{theindex}\def\seename{See}\def\alsoname{Also see}\providecommand*\indexgroup[1]{\indexspace\item \textbf{#1}\nopagebreak}"

postamble "\n\n\\end{theindex}\n"

group_skip "\n\n  \\indexspace\n\n"


delim_0 "\\xleaders\\hbox{$\\cdot$}\\hfill "
delim_1 "\\xleaders\\hbox{$\\cdot$}\\hfill "
delim_2 "\\xleaders\\hbox{$\\cdot$}\\hfill "

headings_flag 1
heading_prefix "\\mbox{}\\hfill{}\\textbf{"
heading_suffix "}\\hfill\\mbox{}"

numhead_positive "Number"
numhead_negative "Number"
symhead_positive "Symbol"
symhead_negative "Symbol"
\end{filecontents*}

\documentclass{article}
\usepackage{imakeidx}
\makeindex[options = -s indexsec.ist, columns = 1]

\makeatletter
\def\idx@encode#1#2{%
  \the\numexpr#1 * 100 + #2\relax}
\newcount\idx@low
\newcount\idx@high
\def\idx@decode#1{%
  \idx@high=#1
  \divide\idx@high by 100
  \idx@low=\numexpr#1-100*\idx@high\relax}

\def\subsecindexpage#1{%
  \idx@decode{#1}%
  \the\idx@high.\the\idx@low}

\let\idx@wrindex@subsec\@wrindex
\patchcmd\idx@wrindex@subsec
  {\thepage}
  {\idx@encode{\arabic{section}}{\arabic{subsection}}}
  {}{}
\patchcmd\idx@wrindex@subsec
  {#2}
  {#2|subsecindexpage}
  {}{}

\newcommand\subsecindex[1][\imki@jobname]{%
  \@bsphack
    \@ifundefined{#1@idxfile}%
    {\PackageWarning{imakeidx}{Undefined index file `#1'}%
    \begingroup
    \@sanitize
    \imki@nowrindex}%
    {\edef\@idxfile{#1}%
     \begingroup
     \@sanitize
     \idx@wrindex@subsec\@idxfile}}

\makeatother

\begin{document}

\section{Foo}
\subsection{Bar}

foo, bar, baz
\subsecindex{foo}\subsecindex{bar}\subsecindex{baz}

\subsection{Hoge}

hoge, fuga, piyo
\subsecindex{hoge}\subsecindex{fuga}\subsecindex{piyo}

\printindex

\end{document}

BTW, in this case, enabling --shell-escape is mandatory, too.

Tags:

Indexing