# How can I force long chapter names (including page numbers) to stay in the margins on the table 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}


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.

• I tried to reproduce the problem, but failed. Could you please post a example (do including your documentclass). – Caramdir Oct 19 '10 at 21:51
• LaTeX is a set of TeX macros. \hfuzz is a TeX primitive so it absolutely applies to LaTeX. It's just irrelevant here. It controls the amount a box may be overfull before TeX reports it as such. – TH. Oct 20 '10 at 4:47
• @Caramdir: I've added an example, but I didn't include the document class because it is based on report and doesn't change all that much. I really need to figure out what I need to change from the report documentclass, which has the same problem. – Michael Victor Zink Oct 22 '10 at 19:33
• A non-answer to your problem, but probably trying to have short concise titles for your chapters will help your readers to more easily grasp the contents of your document, as well as avoid this kind of issues. – Juan A. Navarro Nov 10 '10 at 17:29

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

• It's all alphanumeric (and probably alphabet-only), and using babel didn't work. While it's not optimal, your \- solution is better than nothing and will work. If anyone has anything better (i.e. I don't have to ask grad students to do something like that manually), I'd love to hear it. Thanks Caramdir! – Michael Victor Zink Oct 22 '10 at 20:29
• If you want to wait for another answer you should wait with accepting, because many people ignore questions with accepted answers. – Caramdir Oct 22 '10 at 21:29

I was searching the internet and reading documentations for a solution for hours. This research also led me to this page, unfortunately without providing me a satisfactory answer.

So this is my solution:

\addtocontents{toc}{\protect\sloppy}
\tableofcontents
\listoffigures
\listoftables


I hope this is useful to anybody who is experiencing the same problem with Captions in ToC/LoF/LoT passing the margin and moving the page number.

• This is great and works well – kongo09 Oct 20 '11 at 0:12
• I am unable to use \addtocontents{toc}{\protect\sloppy} \tableofcontents I tried using \usepackage[breaklinks=true]{hyperref} and it worked.. :) but the chapter name which was long, is not clickable anymore.. I can click on other chapters and sections, but not on this one.. – user15501 Jun 11 '12 at 6:14

You may use the tocstyle package which is included in KOMA-Script to set the ToC entries raggedright (and thereby avoid problematic line breaks). Add the following to your preamble:

\usepackage{tocstyle}
\usetocstyle{standard}
\settocfeature{raggedhook}{\raggedright}


Note that the package has "alpha" status.

• This is a great solution. Unfortunately, in my case it produces toc lines, that might have a page-break inbetween. As you said, alpha... – kongo09 Oct 20 '11 at 0:09
• Almost seven years later, the docs say: In the medium term tocbasic will replace the experimental package tocstyle that never became an official part of the KOMA-Script bundle. – takrl Sep 11 '17 at 13:10

(Polishes crystal ball) Let me guess: you are using the hyperref package and you are not using pdflatex. Try adding the linktocpage option to your \usepackage{hyperref} line. Failing that, better follow Caramdir's advice to post an example.

• I am using pdflatex, I am not using hyperref. I'll follow Caramdir's advice. Thanks though! – Michael Victor Zink Oct 22 '10 at 19:19
• This was also the solution for my toc. Thanks! – Wavemaster Mar 1 '17 at 23:25

I found a solution by editing the TOC style's paragraph settings. Use right indentation to stop your title text at defined point. Program a right tab, with or without leading dots, where the page number is to go.

For example, on 8.5" by 11" paper with 1" margins, you have a 6.5" of usable horizontal space. Therefore, program the TOC style tab settings with a 6.5" right tab with or without leading dots.

In the TOC style Paragraph settings, program a right indent of .5", and your titles will not go beyond the 6" mark, leaving .5" of space for the page number.

For a clean look, program the TOC style Paragraph special settings with a hanging indentation of .5", and the second line of your long title will be slightly indented.

• Welcome to TeX.SX! Could you please extend your answer by also showing with an example the code you used? This will be highly beneficial for future visitors. – Claudio Fiandrino May 10 '15 at 15:48
• yes it would be really helpful if you could show an example. thank you! – claudia May 12 '15 at 18:17