Fix the table of contents line breaking headache in LaTeX

[From the help-a-future-Googler files…]

For all of LaTeX’s obvious charm, longtime users know that you still run into formatting issues from time to time. One such issue is how LaTeX has trouble handling line breaks (or line wrapping) in table of contents pages, which also applies to list of figures and list of tables pages. Here is what you might see:

Toc before

Note Figure 3.1 (The Drucker-Prager hyperbolic…), which violates the right margin. It’s not just that it’s improperly aligned—sometimes entries like this will march right off the page. You don’t have to stand for this.

It turns out this arises from a problem with the hyperref package’s ability to insert a line break inside a link. Since I am using hyperref to make all TOC entries clickable (which is why the list is blue in the image above), if the caption is too long, it doesn’t know what to do.

You can fix this issue by adding the option linktocpage to your hyperref stack. The tradeoff is that the list text is no longer linked; just the page number. Not ideal, but worth it to close this ticket without significantly altering the functionality of the TOC. Here’s the new output:

Toc after

There, much better.

A few final notes: