3

I have a document with numbered paragraphs along the margin, setup as a simple counter. I now want my index using imakeidx to show the relevant number from my counter rather than \thepage, as is done in UK legal textbooks (pictured below). How can I do this?

textbook page showing numbered paragraphs

enter image description here

1 Answer 1

2

You can patch \@wrindex to use the special counter.

Here I mocked your setup, adjust to your setting.

\documentclass{book}
\usepackage{imakeidx}
\usepackage{xpatch}

\makeindex

\newcounter{legalsection}[chapter]
\renewcommand{\thelegalsection}{\thechapter-\threedigits{legalsection}}
\newcommand{\threedigits}[1]{%
  \ifnum\value{#1}<10 0\fi
  \ifnum\value{#1}<100 0\fi
  \arabic{#1}%
}

\newcommand{\legalsection}{%
  \par
  \noindent
  \refstepcounter{legalsection}%
  \makebox[0pt][l]{\hspace*{\textwidth}\quad\textbf{\thelegalsection}}%
}

%%%% This is the main part!
\makeatletter
\patchcmd{\@wrindex}{\thepage}{\thelegalsection}{}{}
\makeatother
%%%%

\begin{document}

\chapter{First}

\legalsection
(a) This is the first section where we deal with apples\index{apple}.

\legalsection
(b) This is the second section where we deal with bananas\index{banana}.

\chapter{Second}

\legalsection
Here we deal with cherries\index{cherry}

\printindex

\end{document}

enter image description here

1
  • Lovely! Thank you! Commented Jan 3, 2020 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .