table-of-contents – How to Align Added \dotfill Dots in ToC for scrbook Class

koma-scriptscrbooktable of contents

The ToC of the book I am preparing needs additional formatting. I'm working in the KOMA-script scrbook-class. The book contains a large number of lectures, which I treat as chapters, some of which end with a section called "Questions and answers". My issue is with the dots filling up the space in the ToC between chapter title and page number. With earlier help, and follow up help, and the answer I found here, the following is my MWE. What I am not satisfied with yet though, is the resulting misalignment of the dots in the ToC.

\DeclareTOCStyleEntry[
  linefill={},
]{tocline}{chapter}

\makeatletter
\newcommand \Dotfill {\leavevmode \cleaders \hb@xt@ 0.83em{\hss .\hss }\hfill \kern \z@}
\makeatother

\newcommand{\toclineinsert}[3][14mm]{%
    \Dotfill #2\makebox[#1][l]{#3\Dotfill}%
}

\begin{document}
\frontmatter
\tableofcontents

\mainmatter
\addchap
  [tocentry={Second lecture title \toclineinsert{\normalfont{2nd half January}}},head={}]
  {Second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

ToC example

There has got to be a better way than tweaking the em-size of \hb@xt@ part the \Dotfill command to get the dots in the ToC to match up.

Best Answer

With the KOMA-Script class scrbook the default line filling for section (and subsection etc.) entries in TOC is \TOCLineLeaderFill. The distance between the dots depends on the size of the dots and the font. So you have to use

\newcommand\Dotfill{{\normalfont\normalsize \TOCLineLeaderFill}}

Then the dots between the chapter title and the date are aligned with the dots after the section title.

But it is difficult to align the dots between the date and the page number with the dots of the section entries. Here is a suggestion that still needs adjustment depending on the font size and \@dotfil.

\documentclass[headings=optiontotocandhead]{scrbook}
\usepackage{lipsum}% only for dummy text

\DeclareTOCStyleEntry[
  linefill={},
]{tocline}{chapter}

\newcommand\Dotfill{{\normalfont\normalsize \TOCLineLeaderFill}}

\newlength{\leaderboxwidth}
\setlength{\leaderboxwidth}{43.8pt}% <- adjust here
\DeclareRobustCommand*{\toclineinsert}[1]{%
  {\normalfont\Dotfill {#1}\makebox[\leaderboxwidth][l]{\Dotfill}}%
}

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\addchap
  [tocentry={First lecture title \toclineinsert{4 January}},head={}]
  {Second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}
\addchap
  [tocentry={Second lecture title \toclineinsert{2nd half January}},head={}]
  {Second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}
\addchap
  [tocentry={Third lecture title \toclineinsert{Arpil/March}},head={}]
  {Third lecture title}
\addsec[tocentry={Questions and answers},head={}]{}
\end{document}


\documentclass[headings=optiontotocandhead]{scrbook}
\usepackage{lipsum}% only for dummy text

\DeclareTOCStyleEntry[
  linefill=\TOCLineLeaderFill,
  raggedentrytext=true
]{tocline}{chapter}
\DeclareRobustCommand*{\toclineinsert}[1]{{\ \normalfont\mbox{(#1)}}}

\begin{document}
\tableofcontents

\addchap
  [tocentry={first lecture title \toclineinsert{4 January}}]
  {first lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={second lecture title \toclineinsert{2nd half January}},head={different entry in page header}]
  {second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={third lecture title \toclineinsert{February/March}}]
  {third lecture title}
\addsec[tocentry={Questions and answers},head={}]{}
\end{document}

enter image description here

But I would suggest to use either

\documentclass[headings=optiontotocandhead]{scrbook}
\usepackage{lipsum}% only for dummy text

\DeclareTOCStyleEntry[
  linefill=\TOCLineLeaderFill,
  raggedentrytext=true
]{tocline}{chapter}
\DeclareRobustCommand*{\toclineinsert}[1]{{\normalfont\mbox{(#1)}}}

\begin{document}
\tableofcontents

\addchap
  [tocentry={first lecture title \toclineinsert{4 January}}]
  {first lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={second lecture title \toclineinsert{2nd half January}},head={different entry in page header}]
  {second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={third lecture title \toclineinsert{February/March}}]
  {third lecture title}
\addsec[tocentry={Questions and answers},head={}]{}
\end{document}

enter image description here

or

\documentclass[headings=optiontotocandhead]{scrbook}
\usepackage{lipsum}% only for dummy text

\DeclareTOCStyleEntry[
  linefill={},
]{tocline}{chapter}
\newcommand \Dotfill {{\normalfont\normalsize \TOCLineLeaderFill}}

\DeclareRobustCommand*{\toclineinsert}[1]{%
    {\normalfont\Dotfill\mbox{#1\quad}}%
}

\begin{document}
\tableofcontents

\addchap
  [tocentry={first lecture title \toclineinsert{4 January}}]
  {first lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={second lecture title \toclineinsert{2nd half January}},head={different entry in page header}]
  {second lecture title}
\lipsum[1-20]
\addsec[tocentry={Questions and answers},head={}]{}

\addchap
  [tocentry={third lecture title \toclineinsert{February/March}}]
  {third lecture title}
\addsec[tocentry={Questions and answers},head={}]{}
\end{document}

enter image description here

Related Question