1

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.

1

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.

Resulting Index

1

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 "."

Resulting index

  • 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
  • Then you must define \theAufg to include that. Or you could do something like \newcommand\AufgPrefix{2013 BII \thesubsection} and \renewcommand\theAufg{\AufgPrefix.\arabic{Aufg}} and change \AufgPrefix every time when you need something else. Of course this has the same effect as changing \theAufg directly, but it might be a bit more structured. – Piet van Oostrum Nov 30 '16 at 18:02
  • 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
  • Ok, thank you, I think I can manage this part now. One last thing: Is there a possibility to use spaces in the Index too? For example if I write \renewcommand{\theAufg}{\arabic{Jahr}.\thesection.\arabic{Aufg}} it works but gives me 2010.1.1 which looks a little stupid. I want \renewcommand{\theAufg}{\arabic{Jahr} \thesection.\arabic{Aufg}} So it gives me 2010 1.1; but it don't work, because I think makeindex have a problem with the space an nothing is written in the .aind file. Sorry, I don't quite understand the structure of this system, it's new to me. – buecki Dec 3 '16 at 10:38
  • Well, makeindex is quite picky about the "page numbers" it gets. You can't have spaces in it. And as you have seen, to use dots (.) in it you have to specify that in the .ist file. So you must do a trick to get a space in it. This works: Define \theAufg with the dot instead of a space: \renewcommand{\theAufg}{\arabic{Jahr}.\thesection.\arabic{Aufg}}. Then in the index entry specify a function to be applied to the "number": \index[aufg]{Alpha|myyear} etc. Now define \myyear to convert the first dot to a space: \newcommand\myyear[1]{\MyYear#1\relax} \def\MyYear#1.#2\relax{#1 #2} – Piet van Oostrum Dec 3 '16 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.