# The numbering of exercises automatically on table

I have several hundred exercises in form

\begin{exercise}

\end{exercise}


where each one is a small own file. I input those through \input{...} on master documend.

I have done some other questions about these exercises and I explain my purpose here

Here's my question: it is possible the numbering of the exercises shown in my final .pdf document to appear somehow automatically through loops in a tabular with three columns where the first column shows the chapter to which exercise belongs, in the second column is the number of exercise and the third column is a page on which the exercise occurs.

Unfortunately I do not know if this is possible and obviously if it is possible how could to become ... for this reason I can not give my own example.

All I can think of is via \label \ ref but this I think it's quite hard to be done every time I decide to introduce some exercise in the middle of the text, I must modify whole table

Below is the structure of my document

\documentclass[a4paper,10pt]{book}
\usepackage[utf8x]{inputenc}

\newtheorem{exercise}{\Large \bfseries}[chapter]

\begin{document}

\chapter{kinetic }
\input{exercise.435.tex}

\chapter{momentum}
\input{exercise.436.tex}

\end{document}


and the exercise.435.tex, exercise.436.tex:

\begin{exercise}
Here is the text of the exercise
\end{exercise}

-
What do you mean by “number of page where placements”? –  clemens Jan 21 '13 at 20:27
sorry for my english and for confusion .... I mean the page on which the exercise occurs in the final pdf file –  karathan Jan 21 '13 at 20:36
thank you @cgnieder .... I did not notice –  karathan Jan 21 '13 at 20:51

With package etoc you can use a table of contents for that.

\etocname, \etocnumber and \etocpage are what etoc extracts from the .toc file. Here I used \etocnumber as it will contain already the chapter number. The code is set-up so that the 'name' of the exercise, as written to the .toc file is the number of the chapter. So \etocname gives this number if needed.

With hyperref, these things are links (use hyperref option linktoc=all to have also the page numbers linked).

Edit 1: I have added some final touches to achieve grid-like typesetting. As one knows this is always somewhat difficult in TeX, with all the interline glues and so on added. I replace the images of the output with new ones, with the links colored to show that they are indeed links.

Note that this fancy looking update has nothing to do with the original problem, it is just a question of finalizing the looks of the output, in the chosen multi-columned style. (update: a simpler method is indicated in the commented-out code).

Edit 2: I add a variant to the macro \exercisetotoc which avoids the creation by hyperref of a bookmark for each exercise (showing up in the final pdf file). Depending on the case, one may or may not want to have such bookmarks created by hyperref. In both variants though, the exercise numbers in the list produced by etoc are correctly hyperlinked.

Nota Bene: this use of \tableofcontents to get etoc to display the list of exercises does not at all preclude a standard \tableofcontents in the document. Assume for example we have chapters, sections and subsections. They are at levels 0, 1, and respectively 2, in etoc linguo. Then

\begin{document)
\etocsetlevel{exercise}{3}
\setcounter{tocdepth}{2}
\tableofcontents
%....


displays a completely standard table of contents, with chapters (also parts possibly), sections and subsections. Later in the document

\setcounter{tocdepth}{1}
\etocsetlevel{exercise}{1} % 'exercise' .toc entries will be included
\etocsetlevel{section}{2}  % so we get rid of sections (subsections are already at level 2)
%...
%(the stuff below with \etocsetstyle{exercise} and \etocsetstyle{chapter}
%...
\tableofcontents % this table is the list of exercises as seen below
% see the code excerpts below to see how it is done


In this way one can use etoc to selectively prints "list of.." anything, all of that with a single .toc file...

\documentclass[a4paper,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{hyperref}
\usepackage{etoc}

\newcount\exo % just for dummy generation of exercise numbers here

{exercise}
{\protect\numberline{\theexerci}\thechapter}}

% actually as we will use only \etocnumber, we could have done here rather
% and then we would have used below \etocname, rather than \etocnumber

% if one does NOT want hyperref to create bookmarks for each exercise, then
% one should use:
%
% \makeatletter
%       {exercise}{\theexerci }{\thepage }{\@currentHref }}}
% \makeatother
%
% (here I am using the variant where below one would have used \etocname,
%  not \etocnumber )
%
% Note that the exercise numbers are still hyperlinked, the only difference
% is that they don't appear among the bookmarks of the PDF

\newtheorem{exerci}{\Large \bfseries}[chapter]
\newenvironment{exercise}{\begin{exerci}\exercisetotoc}{\end{exerci}}

\begin{document}\thispagestyle{empty}

% setting up exercise for use with etoc
\etocsetlevel{exercise}{1}

\etocsetstyle{exercise}
{}
{}
{\pagebreak[2]\vskip\baselineskip}

% UPDATE FOR GRID LIKE LOOK:
% the following is to ensure "grid-like" typesetting, also with the chapter
% headings in the list (which are done with \large' size hence upset
% the line spacings)
% (it is assumed here that the chapter name fits on one or two
% column-width lines, else the code would need some additions)
%
% or, one can just say \etocsetlevel{chapter}{2} to not have these chapter
% headings included. Or, one can just be happy with the way etoc by default
% typesets these headings in the TOC, which here however would lead to a
% somewhat "ragged" look of the columns

\newbox\chapbox
\etocsetstyle{chapter}
{}
{}
{\setbox\chapbox=\vtop{\bfseries        % .5em would be better than 1ex here
\noindent\strut\large\etocnumber\hskip1ex\etocname\par}%
\unvcopy\chapbox\nointerlineskip
\kern-\ht\chapbox\kern-\dp\chapbox\kern3\baselineskip\nopagebreak[3]}
{}

% update: the following would be a much simpler way to get the grid-like
% typesetting, but the chapter titles might be a bit cramped as the
% baselineskip is kept equal to the ones for normalsize, not large

%\etocsetstyle{chapter}
%{}
%{}
%{\noindent\strut{\bfseries\large\etocnumber\hskip.5em\etocname}\par
% \vskip\baselineskip}
%{}

%
\setcounter{tocdepth}{1}

% 4 columns
\etocruledstyle[4]{\Large\bfseries Index to the exercises}

\setlength{\columnseprule}{.4pt}

\tableofcontents

\chapter{kinetic }

\exo300

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<399
\repeat

\chapter{momentum}

\exo400

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<499
\repeat

\chapter{energy}

\exo500

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<599
\repeat

\end{document}


-
It will be easy to explain please, the use of \newenvironment{exercise}{\begin{exerci}\exercisetotoc}{\end{exerci}}. Is here the link with hyperref, or I get it wrong? –  karathan Jan 23 '13 at 23:53
What it does is simply to add some information to the .toc file, like \section or \subsection do. There is no connection with hyperref, but hyperref enhances what is written to the .toc file with some information of its own. The final result is that the date inside the table of contents consists of hyperlinks, when hyperref is used. –  jfbu Jan 24 '13 at 10:42
in the above I meant 'data' not 'date'. –  jfbu Jan 24 '13 at 11:14
Very informative, thank you. And something else, in my document, I have the exercises in two columns. After entering your code, appears one vertical line between columns, that I didn't want it. –  karathan Jan 24 '13 at 12:53
@karathan I guess adding \setlength{\columnseprule}{0pt} before the place where you don't want the vertical line should do it. –  jfbu Jan 24 '13 at 13:49

Here's a way that uses etoolbox's convenient macros. With \AtEndEnvironment a macro is added to the exercise environment that stores the three values in a list (etoolbox has tools for this, too). In the table we then loop over this list.

This way only exercises set before the table are stored in the list when the table is set. But you didn't say where you wanted the table, so...

\documentclass[a4paper,10pt]{book}
\usepackage[utf8x]{inputenc}

\newtheorem{exercise}{\Large \bfseries}[chapter]

\usepackage{filecontents}
\begin{filecontents}{exercise.435.tex}
\begin{exercise}
435-1
\end{exercise}
\begin{exercise}
435-2
\end{exercise}
\end{filecontents}

\begin{filecontents}{exercise.436.tex}
\begin{exercise}
436-1
\end{exercise}
\begin{exercise}
436-2
\end{exercise}
\end{filecontents}

\usepackage{etoolbox,booktabs}
\newcommand*\saveexercise{%
\thechapter&\theexercise&\thepage\noexpand\\}
}
\newcommand*\writeexercise[1]{#1}
\AtEndEnvironment{exercise}{\saveexercise}

\begin{document}

\chapter{kinetic}
\input{exercise.435.tex}

\chapter{momentum}
\input{exercise.436.tex}

\chapter{Overview}
\begin{tabular}{ccc}
\toprule
Chapter & Exercise & Page \\
\midrule
\forlistloop{\writeexercise}{\savedexercises}
\bottomrule
\end{tabular}

\end{document}


-
Admittedly very elegant way @cgnieder, but I have over 500 exercises in one of the books I write, Ι will have to write 500 times \begin{filecontents}.... , or have I understand something wrong? –  karathan Jan 23 '13 at 22:02
No, I only used the filecontents environment because I don't have your files to input. The filecontents environment created sample files for me (and everyone who copies the code and compiles it). You can leave out that part completely. –  clemens Jan 23 '13 at 22:05
Οh...I did not know this...I learned something new today :) –  karathan Jan 23 '13 at 22:14
I notice that in my text exercises begins with the number 1.1 ... 2.1....and so on but in table the numbering begins from zero 1.0,1.1......2.0,2.1,...... Is there a way to fix it? –  karathan Jan 23 '13 at 23:00
Ah, right... Change \AtBeginEnvironment into \AtEndEnvironment` –  clemens Jan 23 '13 at 23:04