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}
As mentioned in the comments the exsheets
package can be used for this. The titles/names of the exercises can be given as a subtitle using the question
environment's subtitle
option. To actually print the subtitles a headings instance must be used that typesets it. The predefined block-subtitle
instance nearly looks the same as your own defined headings so it can be used.
This means we set
\SetupExSheets{
headings = block-subtitle ,
headings-format = \large\bfseries\sffamily ,
% needs v0.16 2014/09/14 to work:
subtitle-format = \large\bfseries\sffamily
}
which mimics the definitions in your question.
Next thing we need is obviously some mainquestion
counter which can be used to print the question numbers as 1.1, 1.2, 2.1, etc. Like you I use the section
counter for the task. Then we need
\SetupExSheets{
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
}
\IfQuestionSubtitleT{:}
ensures that the colon only is typeset when a subtitle is given.
For managing the exercises they can be defined in an external file, myexercises.tex
, say. For the example below I saved it with the following contents:
\begin{question}[subtitle=This is the Name of the first Exercise,ID=Q1]
This is the problem given for the first Exercise.
\end{question}
\begin{solution}
This is the first solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the second Exercise,ID=Q2]
This is the problem given for the second Exercise.
\end{question}
\begin{solution}
This is the second solution.
\end{solution}
\begin{question}[subtitle=This is the Name of the third Exercise,ID=Q3]
This is the problem given for the third Exercise.
\end{question}
\begin{solution}
This is the third solution.
\end{solution}
If we put everything together we get:
\documentclass{scrartcl}
\usepackage{exsheets}[2014/09/14] % v0.16 or newer
\SetupExSheets{
headings = block-subtitle ,
headings-format = \large\bfseries\sffamily ,
subtitle-format = \large\bfseries\sffamily ,
counter-within = section ,
counter-format = se.qu\IfQuestionSubtitleT{:} ,
% solution/print = true % uncomment for tutors
}
% needed in earlier versions of exsheets:
% \DeclareInstance{exsheets-heading}{block-subtitle}{default}{
% subtitle-format = \large\bfseries\sffamily ,
% join = {
% title[r,B]number[l,B](.333em,0pt) ;
% title[r,B]subtitle[l,B](1em,0pt)
% } ,
% attach = {
% main[l,vc]title[l,vc](0pt,0pt) ;
% main[r,vc]points[l,vc](\marginparsep,0pt)
% }
% }
\begin{document}
\stepcounter{section}
\includequestions[IDs={Q1,Q2}]{myexercises}
\stepcounter{section}
\includequestions[IDs=Q3]{myexercises}
\end{document}
If you uncomment the line
% solution/print = true
in the above example you'll get
If you instead add \printsolutions
at the end you'll get
Unfortunately there's currently no way to add subtitles to solutions.
Best Answer
You can use the exam document class in both your documents. Then refer to chapter 8: including solutions.
Basically, anywhere in the document you can just specify
\printanswers
or\noprintanswers
or use theanswers
option when loading the document class.It’s easiest if you have the actual questions and answers in a separate document that you
\input
into both the exercise document and the sample solution document.If you want an extra section with the answers, you could instead
\input
the questions document twice, once before and once after the\printanswers
document: this will cause the first appearance of the questions to be printed without answers, and the second appearance with answers.