[Tex/LaTex] The numbering of exercises automatically on table

exercisesnumberingpage-numbering

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}

Best Answer

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}
\hypersetup{colorlinks=true,linkcolor={blue}}  % <- UPDATE to show the links
\usepackage{etoc}


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

\def\exercisetotoc{\addcontentsline{toc}
         {exercise}
         {\protect\numberline{\theexerci}\thechapter}}

% actually as we will use only \etocnumber, we could have done here rather
% \def\exercisetotoc{\addcontentsline{toc}{exercise}{\theexerci}}
% 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
% \def\exercisetotoc{\addtocontents {toc}{\protect\contentsline 
%       {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}
{}
{}
{\noindent\etocnumber\strut\leaders\etoctoclineleaders\hfill\etocpage\par}
{\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
\advance\exo by 1
\repeat

\chapter{momentum}

\exo400

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<499
\advance\exo by 1
\repeat


\chapter{energy}

\exo500

\loop

\begin{exercise}
stuff
\end{exercise}

\ifnum\exo<599
\advance\exo by 1
\repeat

\end{document}

first page

second page

Related Question