[Tex/LaTex] How to force long chapter names (including page numbers) to stay in the margins on the table of contents

line-breakingtable of contents

I'm somewhat new to LaTeX and have been tasked with satisfying some strict formatting requirements for this LaTeX document using a custom class.

Currently, if there is a long chapter name (within a certain window around 1 line in length), it will show up on one line with the page number "pushed" outside the right margin. This isn't allowed, so how can I wrap these slightly-too-long chapter headings and avoid printing the page number outside the right margin?

I've already tried \setlength{\hfuzz}{0.0pt} which I read about here but it sounds like that only applies to TeX, not LaTeX. In any case, it didn't work.

EDIT: Here is an example that illustrates the problem. The class I'm using/writing is based on report, and report has exactly the same problem, viewable to all in the following document:

\documentclass{report}

\begin{document}

\tableofcontents

\chapter{xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx 1234567890}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\chapter{xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx 1234567890}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\chapter{xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx12345}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\end{document}

When generated (with pdflatex in my case), the table of contents looks like this:

heading omitted, table of contents shown

As you can see, the first title is short enough that it is on one line. The second is long enough that it is broken into two lines. The third, however, is somewhere in between and only pushes the page number (4) outside the margins. The first and second page numbers are correctly within the margins.

Given the strict margin requirements for these documents, I need the third title to break, probably with a hyphen, within the margin. I've seen this done, and in that case it broke right before where the page numbers are printed normall.

Best Answer

It seems like (La)TeX has problems hyphenating the word at the end of the line (like with xxxxx12345) and decided that breaking the line before the word would look worse. Maybe you need to load hypenation patterns for your language (via babel) or it is just some word with “special characters” (i.e. not in the a-z, A-Z range, like 1 or ü). In this case it is probably easiest to explicitly tell TeX where it is possible to break the word by marking the positions with \-. For example, the above word might be marked as xxx\-xx\-123\-45, which gives

example