How can I stop LaTeX from leaving too little room between the contents of a contents line and the page number?

If the length of a contents line is a bit too much, but not much too much, LaTeX squeezes it in before the page number. This looks pretty awful to me.

How can I persuade it to break the line in this case, as it would if the line were just a bit longer and couldn't be squeezed in?

Please note that my real document uses etoc and I need a solution which is consistent with that usage. (I'm using it both to generate multiple tables of contents and to flexibly fade parts of them.)

MWE:

\documentclass[a4paper]{book}
\usepackage{geometry}
\begin{document}
\tableofcontents
\chapter{In the beginning}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the middle}
\setcounter{page}{50}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the end}
\setcounter{page}{100}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\end{document}


I can't imagine this hasn't been asked before, but I'm not finding a good solution. How can I force long chapter names (including page numbers) to stay in the margins on the table of contents? concerns the same problem, but I'm already loading hyphenation patterns for UK English and Welsh, I'm using pdfLaTeX, but I'm not using hyperref. So that leaves the manual solution of adding in hyphenation points, which would be very far from an optimal solution. The other suggestion in the comments there is to shorten titles, but that's not an option here: I didn't name the thing I'm including and I can't re-title it.

Is there a better solution? 'Better' means the solution is automatic (does not require inserting hyphenation points in words LaTeX knows how to hyphenate elsewhere) and does not require rewriting things to be shorter (or longer).

• Nice observation! If you set the page to 1000 instead of 100, it becomes even worse, the page number overshoots to the right. – user121799 Jan 28 '18 at 0:29
• Your titles are too long! Please make them as short as possible! – Mogic Jan 28 '18 at 4:12
• @Kuttens It isn't my title, as I explained. Well, the one in the MWE is. But the real one isn't. – cfr Jan 28 '18 at 4:14
• I don't know the etoc package but have you tried the tocloft package? However the 3 answers below are all good, especially by @Kuttens. – Peter Wilson Jan 28 '18 at 18:55

First I'd like to give you a structure of the CONTENTS which comes from layouts package readme file.

Then you can adjust any parameter as you like. And for your question, we can adjust \@pnumwidth and \@tocrmarg values as follows.

\documentclass[a4paper]{book}
\usepackage{geometry}
\makeatletter
\renewcommand\@pnumwidth{2.4em}
\renewcommand{\@tocrmarg}{2.4em}
%make sure that these two values are the same and then the titles
%on the right would break lines at one vertical lines.
\makeatother
\begin{document}
\tableofcontents
\chapter{In the beginning}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the middle}
\setcounter{page}{50}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the end}
\setcounter{page}{100}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\end{document}


And the PDF is

• +1, I think this the right approach: understand what's going on and change what's necessary, instead of adding more and more packages on top of it. – ShreevatsaR Jan 28 '18 at 5:57
• BTW the \@pnumwidth and \@tocrmarg parameters are documented in texdoc source2e, as “Width of box in which page number is set” and “Right margin indentation for all but last line of multiple-line entries” respectively. – ShreevatsaR Jan 28 '18 at 5:59
• Nice. Are there any specific ways to have the dots align on the right? Should a "magic number" be computed based on \@dotsep? – Andrea Lazzarotto Jan 28 '18 at 19:21
• @AndreaLazzarotto You mean with the text of the lines above/below? I was hoping to avoid the raggedness which LaTeX seems to prefer here. This is indeed an extremely useful answer, though. – cfr Jan 28 '18 at 22:40
• @cfr yes. I mean, the text of every line now seems overfull because it goes past the dots. OK maybe it's just an illusion but that's what it looks like to me. – Andrea Lazzarotto Jan 28 '18 at 23:30

the standard classes leave \@pnumwidth for the width of the page number, so you just need to increase it if you have lots of pages,

\documentclass[a4paper]{book}
\usepackage{geometry}
\makeatletter
\renewcommand\@pnumwidth{2.5em}
\renewcommand\@tocrmarg{2.5em}

\makeatother
\begin{document}
\tableofcontents
\chapter{In the beginning}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the middle}
\setcounter{page}{50}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the end}
\setcounter{page}{100}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\end{document}

• Thanks. That's helpful. But it doesn't cut the lines cleanly. That is, it is still raggedy on the right because it doesn't tidy it to the 'margin' of the lines of content. Is this intentional? – cfr Jan 28 '18 at 1:17
• yes \dottedcontentsline is essentially raggedright with at least pnumwidth space on the right, and the last line filled with dots rather than spaces, would be possible to do a version that justified, I suppose... – David Carlisle Jan 28 '18 at 12:11
• I just think it looks messy when the edges are ragged like that. Maybe it is just me, but it looks untidy: as if I didn't finish tucking the ends in properly. – cfr Jan 28 '18 at 22:42
• @cfr sorry I misled you. I have updated the answer with the sheets tucked in. ( note relative sizes of \@tocrmarg and \@pnumwidth) – David Carlisle Jan 28 '18 at 22:55

You have the \contentsmargin command of titletoc:

\documentclass[a4paper]{book}
\usepackage{titletoc, calc}
\usepackage[showframe]{geometry}
\contentsmargin{2pc}

\begin{document}

\tableofcontents
\chapter{In the beginning}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the middle}
\setcounter{page}{50}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}
\chapter{In the end}
\setcounter{page}{100}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos}
\section{The emptiness of nothingness and the blue ovoidness of avoiding voiding kangaroos and jumping counts}
\section{Counting kangaroos and jumping counts}

\end{document}


• Thanks. Is titletoc compatible with etoc and not using titlesec? – cfr Jan 28 '18 at 1:14
• I don't know, but I wouldn't bet on it. As you see , I didn't load titlesec, but you'll get the most of titletoc in combination with titlesec. If you use etoc (which I don't knwo well), I'm sure it has an equivalent tool. – Bernard Jan 28 '18 at 1:20
• Thanks. As I say, I'm already using etoc. Also, I'm allergic to titlesec. (Possibly to titletoc`, too - I'm not sure how specific my allergy is exactly.) ;) – cfr Jan 28 '18 at 1:24
• If I'm not too inquisitive, could you explain why this allergy, in a few words? – Bernard Jan 28 '18 at 1:26
• It doesn't work for me. Any time I try to use it in my own code (not just for an answer here), it means trouble. It fails mysteriously in ways I don't understand and cannot disentangle. I cannot find my way through the code and I can never find quick workarounds in emergencies. I know it works well for other people. It just doesn't work for me. I have, therefore, eradicated all uses of it from my personal tree. I only had one left and then that caused problems and, as usual, the easiest solution in the end was to code something from scratch. I breathe; I break it. I know not why. – cfr Jan 28 '18 at 1:43