# Index with exercise numbers

I want to create a collection of exercises. For this I constructed an environment for the exercises:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\newcounter{wf}
\setcounter{wf}{0}

\newcounter{Aufg}
\setcounter{Aufg}{0}

\newcommand{\Punkte}{0}

\newenvironment{Aufgabe}[1]{%
\renewcommand*{\Punkte}{#1}%
\nopagebreak%
\begin{list}{}{}
\item[]%
\makebox[0pt][l]{%
\hspace{-1cm}\refstepcounter{Aufg}\textbf{\arabic{Aufg}}}%}%
\ignorespaces}{
\ifnum \Punkte>0 \hspace*{0pt} \hfill \mbox{(\Punkte\ BE)} \fi%
\end{list}%
}

\begin{document}
\begin{Aufgabe}{5}
This is exercise number one. Topic: Alpha
\end{Aufgabe}
\begin{Aufgabe}{4}
And here we have exercise number two. Topic: Beta
\end{Aufgabe}
\begin{Aufgabe}{2}
Finally exercise number three. Topic: Gamma
\end{Aufgabe}
\end{document}


I also have substructures for the exercises (exercise A2.1.1 or so) but I want to keep it simple here and just take a look on the exercise numbers.

For an index normally I would write \index{Alpha} in exercise number one (and Beta in two and gamma in three) and this would give me:

Alpha 1

Beta 1

Gamma 1

Because it refers to the page number. But what I really want is an index which gives me the exercise number. So for example using \myindex{Alpha} in exercise one (and Beta in two and Gamma in three) and it should result to

Alpha 1

Beta 2

Gamma 3

Or if there is a more complex structure and exercises with the same topic it should be like this:

Alpha A2.2; A3.4

Beta B1.1; B1.2.1; B1.2.2

Gamma A1; B5.4

and so on.

Do you have any ideas for this special problem? Thank you in advance for your help.

Here is the second setup with the exercise reference containing a year with a space followed by the section number. We use xindy instead of makeindex, because it allows us to define the structure of the references.

First the document.The main difference is the definition of \theAufg.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\newcounter{Aufg}[section]
\setcounter{Aufg}{0}

\newcounter{Jahr}
\setcounter{Jahr}{2016}
\renewcommand{\theAufg}{\arabic{Jahr} \thesection.\arabic{Aufg}}

\newcommand{\Punkte}{0}

\newenvironment{Aufgabe}[1]{%
\renewcommand*{\Punkte}{#1}%
\nopagebreak%
\begin{list}{}{}
\item[]%
\makebox[0pt][l]{%
\hspace{-1cm}\refstepcounter{Aufg}\textbf{\arabic{Aufg}}}%}%
\ignorespaces}{
\ifnum \Punkte>0 \hspace*{0pt} \hfill \mbox{(\Punkte\ BE)} \fi%
\end{list}%
}

\usepackage{index}
\newindex[theAufg]{aufg}{aidx}{aind}{Aufgaben Index}

\begin{document}
\section{A Section}

\begin{Aufgabe}{5}
This is exercise number one. Topic: Alpha \index[aufg]{Alpha}
\end{Aufgabe}
\begin{Aufgabe}{4}
And here we have exercise number two. Topic: Beta \index[aufg]{Beta}
\end{Aufgabe}

\section{Another Section}

\begin{Aufgabe}{2}
Finally exercise number three. Topic: Gamma \index[aufg]{Gamma}
\end{Aufgabe}

\printindex[aufg]
\end{document}


Now we make a xindy "module" aufg.xdy:

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


This supports a number followed by space followed by 2 numbers separated by ".". You can add more cases if you want, for example for an appendix letter use "ALPHA" instead of "arabic-numbers".

Now to sort the index use the command:

texindy -M aufg.xdy -o yourdocument.aind  yourdocument.aidx


and run LaTeX again.

The package index has provisions to create new indexes. You can also specify that a counter will be used instead of page number.

\usepackage{index}
\newindex[theAufg]{aufg}{aidx}{aind}{Aufgaben Index}

This is exercise number one. Topic: Alpha \index[aufg]{Alpha}

And here we have exercise number two. Topic: Beta \index[aufg]{Beta}

Finally exercise number three. Topic: Gamma \index[aufg]{Gamma}


Then you have to run

makeindex -o yourdocument.aind yourdocument.aidx


to sort and process that index.

Now the question about the complex structure.

Just define \theAufg to show the structured number of the exercise (e.g. section number . exercise number. Then you will have to add an index style that specifies that . is the separator, otherwise makeindex will complain.

Here is a complete example:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\newcounter{Aufg}[section]
\setcounter{Aufg}{0}
\renewcommand{\theAufg}{\thesection.\arabic{Aufg}}

\newcommand{\Punkte}{0}

\newenvironment{Aufgabe}[1]{%
\renewcommand*{\Punkte}{#1}%
\nopagebreak%
\begin{list}{}{}
\item[]%
\makebox[0pt][l]{%
\hspace{-1cm}\refstepcounter{Aufg}\textbf{\arabic{Aufg}}}%}%
\ignorespaces}{
\ifnum \Punkte>0 \hspace*{0pt} \hfill \mbox{(\Punkte\ BE)} \fi%
\end{list}%
}

\usepackage{index}
\newindex[theAufg]{aufg}{aidx}{aind}{Aufgaben Index}

\begin{document}
\section{A Section}

\begin{Aufgabe}{5}
This is exercise number one. Topic: Alpha \index[aufg]{Alpha}
\end{Aufgabe}
\begin{Aufgabe}{4}
And here we have exercise number two. Topic: Beta \index[aufg]{Beta}
\end{Aufgabe}

\section{Another Section}

\begin{Aufgabe}{2}
Finally exercise number three. Topic: Gamma \index[aufg]{Gamma}
\end{Aufgabe}

\printindex[aufg]
\end{document}


Run makeindex with:

makeindex -o yourdocument.aind -s aufg.ist yourdocument.aidx


where aufg.ist contains

page_compositor "."


• Wow, that's a very good solution. Thank you. To show the whole complex structure, I want <Year>*Space*<Exercise Group><Exercise Group Number>*Space*<Exercise Number>.<Exercise Subnumber>.<Exercise SubSubnumber> e.g: 2012 BII 2.3.1 I think I can create some if-cases because it's not always Number.Subnumber.Subsubnumber. Sometimes there is just Number oder Number.Subnumber. But is there a possibility to Include the whole path (e.g. 2013 BII 2.2.2) in the index too? – buecki Nov 30 '16 at 17:49
• If you put arbitrary stuff in the number you will have to be careful to see if makeindex can properly sort it. – Piet van Oostrum Nov 30 '16 at 18:03