[Tex/LaTex] Table of contents: Page numbering for questions and solutions side-by-side

table of contents

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}

Best Answer

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.

Related Question