目录:各章节并排显示在目录中

目录:各章节并排显示在目录中

以下代码改编自 这里 这个想法是创建一本包含两个部分(包含问题和解决方案)的书,这两个部分具有相同的(编号)章节标题和(未编号)节标题。为了保持目录较小,两个部分的页码应并排出现在目录中(因为两个部分的结构非常相似)。此外,节(虽然没有编号)应出现在目录和页眉中。以下是基于以下 MWE 留下的问题:

  • 解决方案部分的页码未出现在目录中(仅出现“??”)
  • 章节和部分的页码似乎没有(垂直)正确对齐,特别是当某些解决方案位于 >= 100 页(3 位数字)时。这肯定是由于\hspace问题页码和其解决方案之间的页码不一致造成\titlecontents的。(似乎表格式目录是确保页码右对齐的方法,但我没能让它工作)。

如何解决这两个问题?

\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}

答案1

由于这个问题是 KOMA 脚本特有的(而且我当时没有收到答复),所以我也将其发布在komascript 论坛。Markus Kohm 想出了一个绝妙的解决方案(非常感谢,Markus!);请参阅 Markus 的回答了解详情。

相关内容