0

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}
  • Why using titletoc with a KOMA class? – Gonzalo Medina Oct 2 '15 at 0:02
  • Because of the \titlecontents commands (?) – Marius Hofert Oct 2 '15 at 4:15
  • Well, of course. My comment was meant to indicate that titletoc might not be fully compatible with KOMA classes (titlesec is not). With KOMA classes, I think, one should use tocstyle (from the same author of the classes). – Gonzalo Medina Oct 2 '15 at 14:10
  • This is also discussed on the KOMA-Script website, see komascript.de/node/1971 – esdd Oct 3 '15 at 16:41
  • yes, that was me -- I decided to post it there as well, as I know Markus Kohm a bit but am not sure whether he reads tex.stackexchange. Of course I provide a full solution once it's working as expected. – Marius Hofert Oct 3 '15 at 18:54
0

Since this problem is quite KOMA-script-specific (and since I didn't receive an answer by then), I also posted it in the komascript forum. Markus Kohm came up with a beautiful solution (thanks a ton, Markus!); see Markus' answer there for the details.

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.