2

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 http://tex.stackexchange.com/q/132901/4686, see this answer http://tex.stackexchange.com/a/133559/4686 (which possibly might be improved upon, package etoc has evolved since) –  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 Answers2

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.

QStar
  • 446
  • 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 –  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
  • I changed the answer to create something, which looks okayish. – QStar Sep 11 '15 at 11:19
  • 2
    If hyperref is used, the links will go wrong -- this is due to the counter resetting of chapter for the solution chapter –  Sep 11 '15 at 11:28
  • How about this manual fix? By manually adding a hypertarget on the section title and a hyperlink on the solution page number. That fixes the problem. – QStar Sep 11 '15 at 11:48
  • @QStar: It might work, but I can't test right now. Perhaps you need \phantomsection before each label –  Sep 11 '15 at 12:11
  • Well I tested it and it works at least at my end. I didn't had to use \phantomsection. – QStar Sep 11 '15 at 12:24
  • Hi, thanks. I provided an update with slightly nicer spacings etc. Feel free to take it and update your answer. I'll still leave it open for a bit, maybe someone comes up with a more elegant solution which does not require the labels being 1.1, 1.2... etc. – Marius Hofert Sep 11 '15 at 13:03
  • @QStar If you add \usepackage{mathtools}, pdflatex fails with Undefined control sequence. It doesn't like the \addtocontents{toc}{\setcounter{tocdepth}{-1}} it seems. Do you know a solution for that? – Marius Hofert Sep 16 '15 at 14:48
  • 1
    You'll have to change the \addtocontents line to \addtocontents{toc}{\protect\setcounter{tocdepth}{-1}}. For an explanation, see http://tex.stackexchange.com/questions/65029/problems-with-mathtools-package – QStar Sep 16 '15 at 14:54
  • @QStar Something else I realized: If there are chapters (like 'preface') which only have a single page number, the toc still shows the 'two-column layout'. Is there an easy way to switch the 'two-column layout' on/off? You can see this by adding \addchap{Preface} before the line starting with \addtocontents{toc}{\hfill Questions... in the example. It would be great if "Preface" would appear with its page number aligned with those of the solutions, then some vertical space, and then the 'two-column layout'. – Marius Hofert Sep 16 '15 at 17:52
  • ... and the same with \printbibliography... the page number appears in the left column and the right columns shows ??. – Marius Hofert Sep 16 '15 at 18:57
  • Okay, I managed to do it. I use \chapter*{Preface}\label{chap:pref} and then \addtocontents{toc}{{\bfseries Preface\hfill\pageref{chap:pref}}\par}. Similarly for the bibliography and index... just avoid having them being included in the toc (and thus being printed in 'two-column format') and then add them by hand via \addtocontents{toc}{...} – Marius Hofert Sep 16 '15 at 21:38
  • @QStar: I just realized that if your solutions appear on a page with page number having more than 1 digit, then the \hspace{5.2em} creates a fixed space between the page numbers for questions and solutions and that the page numbers for the questions are 'pushed further to the left'. Any idea how to avoid this? I tried to use a tabular environment but it didn't work (it might still be a good approach, though) – Marius Hofert Sep 30 '15 at 09:26
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.