1

The book "One thousand exercises in probability" (Grimmett, Stirzaker) has a very nice table of contents: enter image description here

As it looks like, the book only consists of chapters: First, all exercise chapters (including sections) appear, then the corresponding solutions (with the same chapter/section structure). I reset the chapter numbering (see below) to reflect that, but how can one create a table of contents as shown above?

Here is a basic template:

\documentclass{scrbook}
\usepackage[american]{babel}

\begin{document}
\tableofcontents
\chapter{Chapter 1}
\section{Section 1}
\section{Section 2}
\section{Section 3}
\clearpage
\setcounter{chapter}{0}
\chapter{Solutions to Chapter 1}
\section{Solutions to Section 1}
\section{Solutions to Section 2}
\section{Solutions to Section 3}
\end{document}
  • related to tex.stackexchange.com/q/132901/4686, see this answer tex.stackexchange.com/a/133559/4686 (which possibly might be improved upon, package etoc has evolved since) – user4686 Sep 11 '15 at 16:53
  • Hi, thanks for helping. I had a quick look at it and it looks good, but I think it addresses a different problem (namely how to produce a split toc after a standard toc). In that sense, I didn't immediately see how this might be helpful here, but if you see how, please post a solution (based on etoc). – Marius Hofert Sep 11 '15 at 20:27
2

A bit hacky, but it gives the desirable result... Currently, it's still required to manually add the label per each solution section. It would be desirable if this also could be automated.

\documentclass{scrbook}
\usepackage[american]{babel}
\usepackage{hyperref}

\usepackage{titletoc}

% {section}, [left], {above}, {before with label}, {before wo label},
% {filler and page}, [after]
\titlecontents{chapter}[1.5em]{\addvspace{1pc}\bfseries}{\contentslabel{1.2em}}{}
    {\hfill\contentspage\hspace{5.2em}\pageref{\thecontentslabel}\hspace*{-1.4em}}
\titlecontents{section}[4em]{\addvspace{0.1pc}}{\contentslabel{2.5em}}{}
    {\hfill\contentspage\hspace{6.1em}\hyperlink{\thecontentslabel}{\pageref{\thecontentslabel}}\hspace*{-1.6em}}
\newcommand{\sectionsol}[2]{\hypertarget{#2}{\section{#1}\label{#2}}}

\begin{document}
\tableofcontents
\addtocontents{toc}{\hfill Questions\hspace{1em}Solutions\par}

% Exercises
\chapter{Chapter 1}
\section{Section 1}
\section{Section 2}
\section{Section 3}

% Solutions
\setcounter{chapter}{0}
\addtocontents{toc}{\setcounter{tocdepth}{-1}}% removes all toc entries of after this line
\chapter{Solutions to Chapter 1}\label{1}
\sectionsol{Solutions to Section 1}{1.1}
\sectionsol{Solutions to Section 2}{1.2}
\sectionsol{Solutions to Section 3}{1.3}
\setcounter{tocdepth}{2}% display contents (down to subsections) in toc again

\end{document}

enter image description here

Removes all table of contents entries of any level from this line. You'll have to change this behavior back if you want for example appendices to appear in the toc after this line.

Overall, the idea is to redefine the format of the table of contents. Add manually a cross-reference to the section label. And manually add the label. There is most certainly a more gracious way of doing this, though.

Edit: Added a manual fix for the hyperref package. Manually a hypertarget is added by the new defined \sectionsol command, which defines both the target and a label. Finally, a hyperlink is added in the toc, which points to the hypertarget.

Edit: Copied over the adjusted solution by Marius Hofert, which has better spacing. And to save it in case a better solution comes along.

When using the mathtools package the \setcounter macro has to be protected. See this answer for more details.

  • Hi, thanks for helping. I also get the solution pages first from the code. Also, do you know how to get the titles "Questions" and "Solutions" (otherwise the reader doesn't recognize which is which)? – Marius Hofert Sep 11 '15 at 10:54
  • 1
    The manual \section labels might become tedious – user31729 Sep 11 '15 at 11:01
  • I plan to link to the chapters/sections anyways, so they will have labels. More important would be to have 'labels' for the two columns of page numbers in the toc. – Marius Hofert Sep 11 '15 at 11:05
  • 2
    If hyperref is used, the links will go wrong -- this is due to the counter resetting of chapter for the solution chapter – user31729 Sep 11 '15 at 11:28
  • 1
    You'll have to change the \addtocontents line to \addtocontents{toc}{\protect\setcounter{tocdepth}{-1}}. For an explanation, see tex.stackexchange.com/questions/65029/… – QStar Sep 16 '15 at 14:54
0

As I mentioned in the comments, there is a problem with the QStar's suggested solution. Since I didn't hear back from him, I opened another question. See here and more so here (in German) for the solution.

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.