5

Styling my Thesis, the publisher requires at least two dots in the ToC - or the line needs to be broken (which, manually done, causes other problems). Are there any ideas two create a more elegant solution than the tocloft \renewcommand\cftdotsep{..} -thing (which creates new problems at other lines)?

MWE:

\documentclass[
paper=233mm:155mm,
fontsize=10.5,
cleardoubleempty,
headings=optiontohead,
bibliography=totoc,
idxtotoc,
pointlessnumbers]
{scrbook}


\usepackage{fontspec}
\setmainfont{Times New Roman}

\usepackage{polyglossia}  % Sprachumschaltung
\setdefaultlanguage{german}% Voreingestellte Dokumentsprache: Deutsch


%%% Seitenränder etc.
\usepackage[footnotesep=1.5\baselineskip]{geometry}
\geometry{includehead=true, textwidth=117mm, textheight=186mm, headsep=6mm, left=18mm, bottom=24mm}


%% Design der Überschriften %%%%%%%%%%
\setkomafont{sectioning}{\normalfont\large\scshape\centering}
\setkomafont{part}{\normalfont\Huge\scshape\centering}
\setkomafont{chapter}{\normalfont\LARGE\scshape\centering}
\setkomafont{section}{\normalfont\Large\centering}
\setkomafont{subsection}{\normalfont\large\centering}
\setkomafont{subsubsection}{\normalfont\normalsize\centering}
\setkomafont{minisec}{\normalfont\normalsize\itshape\centering}
\setkomafont{paragraph}{\normalfont\normalsize\itshape}
\setkomafont{subparagraph}{\normalfont\normalsize\itshape}


%% Inhaltsverzeichnis formatieren %%%%%%%%%%%%%%%%%%%
\usepackage{tocloft}

\tocloftpagestyle{empty} %keine Seitenzahl auf erster Seite des Inhaltsverzeichnisses

\renewcommand{\cftchappagefont}{} % Seitenzahlen auch bei Chapter nicht fett
\renewcommand{\cftpartpagefont}{} % Seitenzahlen auch bei Chapter nicht fett

\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}} % Punkte nicht fett
\renewcommand{\cftchapdotsep}{\cftdotsep}

\renewcommand{\cfttoctitlefont}{\hfill\normalfont\LARGE\scshape} % Größe der Überschrift wie \chapter
\renewcommand{\cftaftertoctitle}{\hfill}

\renewcommand{\cftchapfont}{\normalfont}  %% Im Inhaltsverzeichnis Titel normal. alternativ fett. \bfseries
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\renewcommand\thesection{\thechapter.\arabic{section}}   %% Nummerierung Section Römisch

\renewcommand\thesubsection{\thesection.\arabic{subsection}} %% Nummerierung Subsection arabisch usw.

\renewcommand\thesubsubsection{\thesubsection.\arabic{subsubsection}}

\usepackage{blindtext}

\begin{document}

\tableofcontents

\chapter{A chapter with a title much too long as there aren't any dots any more}
\blindtext

\section{A section that is ok as there are at least two dots left}
\blindtext

\subsection{A subsection again not fitting as it is only ONE dot left}
\blindtext

\end{document}
dom
  • 169

2 Answers2

4

One way is to directly patch the \@dottedtocline internal command so that it produces leaders that are at least two em long (say):

\documentclass{scrbook}

\makeatletter

\renewcommand*\@dottedtocline[5]{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hskip 2em \@plus 1fill % <<< PATCH
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
     \par}%
  \fi}

\makeatother

\begin{document}

\tableofcontents

\chapter{One}
Lorem ipsum.

\section{Some words to fill the line, words to fill the line,
    words to fill the line}
Dolor sit amet.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, w}
Consectetur adipisci elit.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, wo}
Numquam faucibus,.

\end{document}

Both the book and the scrbook classes ultimately invoke \@dottedtocline to produce toc entries with leaders, so the patch should work.

Output:

Output of sample code

Addition

I forgot to mention that, as always, the \patchcmd facility provided by the etoolbox package can be used instead of \renewcommand (which require to repeat the full definition):

\documentclass{scrbook}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\@dottedtocline}{\hfill}{\hskip 2em \@plus 1fill }{}{}
\makeatother

\begin{document}

\tableofcontents

\chapter{One}
Lorem ipsum.

\section{Some words to fill the line, words to fill the line,
    words to fill the line}
Dolor sit amet.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, w}
Consectetur adipisci elit.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, wo}
Numquam faucibus,.

\end{document}

Second addition

A comment requires a similar patch tailored for the tocloft package. A quick inspection reveals that this package doesn’t rely on \@dottedtocline to produce the leaders, but defines its own command, namely \cftdotfill, for the purpose. Therefore, this is the command that has to be patched in this case:

\documentclass{scrbook}
\usepackage{tocloft}

\makeatletter

\renewcommand{\cftdotfill}[1]{%
  \def\@tempa{#1}%
  \def\@tempb{\cftnodots}%
  \ifx\@tempa\@tempb
    \hfill % <<< this has not been changed
  \else
    \leaders\hbox{$\m@th\mkern #1 mu\hbox{\cftdot}\mkern #1 mu$}%
      \hskip 2em \@plus 1fill \relax % <<< PATCH
  \fi
}

\makeatother

\begin{document}

\tableofcontents

\chapter{One}
Lorem ipsum.

\section{Some words to fill the line, words to fill the line,
    words to fill the line}
Dolor sit amet.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, w}
Consectetur adipisci elit.

\section{Some words to fill the line, words to fill the line,
    words to fill the line, wo}
Numquam faucibus,.

\end{document}
GuM
  • 21,558
  • This works fine if you don't use the tocloft package which seems to suspend all the efforts... to achieve a proper formatting I do need it... – dom Aug 04 '15 at 07:34
  • @dom: Please see edit. – GuM Aug 04 '15 at 09:07
4

Knuth & Plass's 1981 paper "Breaking Paragraphs into lines" explains the box and glue model underlying TeX's line-breaking algorithm and shows off some pyrotechnics enabled by it.

Using one of those tricks, we can set a more ambitious goal: when the leaders and pagenum are place in their own line for lack of room, fill up the preceding line's with leaders to within a fixed margin from the right.

Note the way the leaders' right edge match up when we force an extra line.

You can control how much The extra leaders protrude into the right margin above the page number appearing the next line by adjusting \rmargin.

enter image description here

\documentclass[a5paper]{article}     

\usepackage{fontspec}

\setmainfont{Times New Roman}

\parindent=0pt % optional, mimick TOC environment

\newdimen\pagenumwidth \pagenumwidth=0.5cm  % reserved right margin space for page numbers
                                                                                  % leaders will reach flush with it 
\newdimen\rmargin \rmargin=0.5cm  % reserved right margin for leaders followed by linbreal
\newdimen\minspc \minspc=1.6em    % if this much hspace isn't available for leaders, break                                                

\def\fillwith{\hbox to 1ex {\hss.\hss}} 

\def\DotsAndPage#1{%
    \leaders\fillwith\hfil
    \hskip \rmargin 
    \penalty50  
    \hskip - \rmargin plus -1fil  % Thanks Gustavo
    \hbox{}      
    \unskip\leaders\fillwith\hskip \minspc plus 1fill
    \nobreak
    {\bfseries\normalsize \hbox to \pagenumwidth{\hfil#1}}%
    \break}

\begin{document}  

{
\hsize=2.4in
A chapter with a title any \DotsAndPage{1}%\par
A chapter with a title A chapter with a title A chapter with a title\DotsAndPage{13}%\par
A chapter with a title A chapter with a title A chapter with a title with a title so t\DotsAndPage{132}
A chapter with a title A chapter with a title A chapter with a title with a title so th\DotsAndPage{132}
\rmargin=0.25cm % leaders tail bite into right margin
A chapter with a title with a title so th\DotsAndPage{132}
\rmargin=-0.1cm  
A chapter with a title with a title so th\DotsAndPage{132}\par      
}


\end{document}
  • 1
    May I suggest the following improvement: \def\DotsAndPage#1{\leaders\fillwith\hfil\hskip\rightdotmarg \penalty50 \hskip -\rightdotmarg plus -1fil \vadjust{}\leaders\fillwith\hskip\minspc plus 1fill \nobreak {\bfseries\normalsize \hbox to \rightdotmarg{\hfil#1}}}? – GuM Aug 04 '15 at 00:27
  • Thanks! so negative stretch was what I was looking for... I'm disinclined to use \vadjust{} instead, to stick with the box+glue+panelty approach. Inserting into the vertical list achieves the same goal but breaks away from the examples I cited. – Jared Kulik Aug 04 '15 at 00:56
  • Actually I made two errors, the correction should be \def\DotsAndPage#1{\leaders\fillwith\hfil\hskip\rightdotmarg\penalty50 \hskip -\rightdotmarg\vadjust{}\nobreak\leaders\fillwith\hskip\minspc plus 1fill {\bfseries\normalsize \hbox to \rightdotmarg{\hfil#1}}}; the negative infinite stretch serves no purpose, the key point is that the second \leaders have fill stretchability, which overpowers the fil of the first ones; \vadjust{} is only to insert a non-discardable item, it is more efficient than an empty box. Also, you should say \def\fillwith{\hbox to 1ex {\hss.\hss}} . – GuM Aug 04 '15 at 01:21
  • The -1fil closes the gap 'tween leaders in the first example. Using fil + fill has one take over the other which is cleaner I suppose, but the result is visually identical. – Jared Kulik Aug 04 '15 at 01:38
  • The presence or absence of -1fil makes no difference: if a break does not occur at the \penalty50, the glue of the last line will be set at the fill order, so whatever value the fil stretchability adds up to, it will be ignored. Also, we are both forgetting a \nobreak before the first \leaders, aren’t we? – GuM Aug 04 '15 at 01:44
  • It matters when the 2nd leaders employs fil (as in my version), it doesn't if you use fill instead. As I said, interchangable. I think example #2 shows that '\nobreak` is redundant. – Jared Kulik Aug 04 '15 at 02:02
  • This seems to work only with the lmodern package – for my purpose Times New Roman is required. txfonts (PdfLatex) or the fontspec (XeTex) don't help... – dom Aug 04 '15 at 08:00
  • The \parfillskip glue has fil stretchability too (unless you change it), and for this reason you need \leaders … plus 1fill. Moreover, with your approach you are actually typesetting dots over dots (first set of leaders, backspace, second set of leaders overprinting the first one), which I prefer to avoid. – GuM Aug 04 '15 at 08:34
  • 1
    @dom, it works fine with Times afaict, though the widths needed adjusting to demonstrate all cases. See corrected example. – Jared Kulik Aug 04 '15 at 14:16
  • @Gusavto, again, there's no visual difference I can detect. I accept (again) that the overprinting is ungentleman-like and so have amened the code accordingly because of it. Also, to shut you up! ;-). Thanks for caring. – Jared Kulik Aug 04 '15 at 14:20