The following code is adapted from here The idea is to create a book with two parts (containing problems and solutions) which have equal (numbered) chapter and (unnumbered) section headings. In order to keep the toc small, the page numbers for both parts should appear side-by-side in the toc (due to the very similar structure in both parts). Furthermore, the sections (although not numbered) should appear in the toc and the page heads. Here are the problems left based on the MWE below:
- The page number for solution sections don't appear in the toc (only '??' appear)
- The page numbers of chapters and sections don't seem to be (vertically)
aligned correctly, especially in the case where some solutions are on a page >= 100
(3-digits). This certainly comes from the
\hspace
between the page numbers of problems and their solutions in\titlecontents
below. (It seems that a table-like toc would be the way to go to ensure right-alignment of the page numbers, but I didn't manage to get this to work).
How can these two issues be fixed?
\documentclass{scrbook}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}
\usepackage{titletoc}
\usepackage{hyperref}
% TOC
% {section}, [left], {above}, {before with label (left side)}, {before wo label
% (left side)}, {filler and page}, [after]
\titlecontents{chapter}[1.9em]{\addvspace{1pc}\sffamily\bfseries}{\contentslabel{1.7em}}{}
{\hfill\contentspage\hspace{5.4em}\pageref{\thecontentslabel}\hspace*{-1.4em}}
\titlecontents{section}[2em]{\addvspace{0.1pc}}{\contentslabel{2.5em}}{}
{\hfill\contentspage\hspace{6.02em}\hyperlink{\thecontentslabel}{\pageref{\thecontentslabel}}\hspace*{-1.5em}}
\newcommand{\addsecsol}[2]{\hypertarget{#2}{\addsec{#1}\label{#2}}}% define solution section, which is shown in toc
\begin{document}
\pagestyle{scrheadings}
\pdfbookmark[0]{\contentsname}{toc}
\addtocontents{toc}{\vspace{1em}\hfill Problems\hspace*{1.4em}Solutions\par}% header for toc
\tableofcontents% without number; add bookmark on level 0
\chapter{Chapter A problems}\label{chap:1}
\addsec{Section 1}
\lipsum[1-100]
\addsec{Section 2}
\lipsum[101-200]
\addtocontents{toc}{\protect\setcounter{tocdepth}{-1}}% remove toc entries after this line
\chapter{Chapter A solutions}\label{2}
\addsecsol{Section 1}{2.1}
\lipsum[201-300]
\addsecsol{Section 2}{2.2}
\lipsum[301-400]
\addtocontents{toc}{\protect\setcounter{tocdepth}{2}}% display contents (down to subsections) in toc again
\end{document}
titletoc
with a KOMA class?\titlecontents
commands (?)titletoc
might not be fully compatible with KOMA classes (titlesec
is not). With KOMA classes, I think, one should usetocstyle
(from the same author of the classes).