3

I would like to create an index that refers to theorem numbers instead of the page numbers.

The following example creates the usual index with page numbers:

\documentclass[ngerman, 12pt]{article}
\usepackage{makeidx}
\makeindex

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
This theorem is about apples \index{apple} and bananas\index{banana}.
\end{theorem} 

\begin{theorem}
This theorem is about apples \index{apple} and pies\index{pie}.
\end{theorem} 

\newpage

\begin{theorem}
This theorem is about bananas\index{banana}.
\end{theorem} 

\printindex

\end{document}

The result looks like this:

Index
apple, 1
banana, 1, 2
pie, 1

However, I would need the theorem numbers in the index instead, i.e.

Index
apple, 1, 2
banana, 1, 3
pie, 2

Has anyone an idea how to do this? Thank you very much for your support!

Jack
  • 314
  • Somewhat related: https://tex.stackexchange.com/questions/165579/index-that-picks-up-on-example-numbers-not-page-numbers – John Kormylo Sep 08 '19 at 00:50

1 Answers1

4

\index writes out the value of \thepage. Just change it to \thetheorem.

\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage{makeidx}
\makeindex

\makeatletter
\patchcmd\@wrindex{\thepage}{\thetheorem}{}{}
\makeatother

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}
This theorem is about apples\index{apple} and bananas\index{banana}.
\end{theorem} 

\begin{theorem}
This theorem is about apples\index{apple} and pies\index{pie}.
\end{theorem} 

\newpage

\begin{theorem}
This theorem is about bananas\index{banana}.
\end{theorem} 

\printindex

\end{document}

enter image description here

egreg
  • 1,121,712
  • How did you change the value to \thethorem? Because if I execute your code, I still get an index showing the page numbers. – Jack Sep 14 '19 at 08:50
  • @Jack Really? The exact code? – egreg Sep 14 '19 at 08:56
  • Yes. And if I compare your code with mine, the only difference I can identify is that you have deleted the "ngerman" from the documentclass – Jack Sep 14 '19 at 10:17
  • @Jack In the example code, ngerman does nothing at all, except printing a warning on the console. Maybe you added babel? – egreg Sep 14 '19 at 10:35
  • It is fine, that you have removed ngerman. But still, how did you change the value of the index from \thepage to \thetheorem - I do not find this part in your code. – Jack Sep 14 '19 at 10:43
  • @Jack I am deeply sorry! Too late in the night and I didn't copy it right. – egreg Sep 14 '19 at 16:27