Page LayoutProblem with page number spacing in table of contents

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
doragasu
Posts: 4
Joined: Mon Oct 06, 2014 11:46 am

Problem with page number spacing in table of contents

Post by doragasu »

I'm having a problem with separation between section name and page number in the table of contents. Some entries almost overlap page number, as you can see in attached screenshot:
toc.png
toc.png (46.47 KiB) Viewed 6798 times
I'm using 12pt, twoside, a4paper book class and I'm *not* using any ToC customization package (such as tocloft or similar). Strange enough, I have this problem only with the ToC. List of figures and list of tables both have correct spacing between the text and the page number.

I have googled a bit, and found topics dealing with spacing between text and section number, but none dealing with text and page number. How can I fix this? Is it possible to add additional spacing between section text and page number? Or if not possible (or troublesome) is it possible to manually force line breaks in conflicting entries?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problem with page number spacing in table of contents

Post by Johannes_B »

Hi and welcome, it's hard to tell what causes the behaviour. But there is a nice method to find it. Make a backup of your files and start to uncomment out pieces, compile, compare, uncomment out more ppieces, compile, compare.

Might take a while, but sooner or later your document will have about ten lines and the cause will be obvious. This method is called preparing a minimal working example.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
doragasu
Posts: 4
Joined: Mon Oct 06, 2014 11:46 am

Re: Problem with page number spacing in table of contents

Post by doragasu »

OK, I'll try preparing a MWE.

EDIT: Preparing a MWE for this problem is tricky, because it must have at least 100 pages for page numbers to start almost overlapping text...

Any suggestions?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problem with page number spacing in table of contents

Post by Johannes_B »

I am a bit cofused as this behaviour is really the default one. My apologies. I thought LaTeX would be a bit more careful at this point and prevent that behaviour, but i was mistaken. It even happens with the more advanced KOMA classes.

But, fortunately, there is a package that allows you to style your toc. The documentation lists different styles and their visual appearance, please have a look and pick the one that you like best.

Please be aware, that you need to run latex multiple times (3 in my case) so that the spaces can be calculated correctly. Hint, you can click on »Open in WriteLaTeX« directly above the code to see the compiled output.

Code: Select all

\documentclass{book}
\usepackage{blindtext}
\usepackage{pgffor}
\usepackage{tocstyle}
\begin{document}
\tableofcontents
\foreach \n in {1,...,37}{\chapter{chapter numero \n}
\blindtext[10]
}
\chapter{a chapter with a very very long and exhausting title,
do people want to read this far?}
\blindtext[2]
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
doragasu
Posts: 4
Joined: Mon Oct 06, 2014 11:46 am

Problem with page number spacing in table of contents

Post by doragasu »

Thanks a lot for help!

BTW I already got a MWE and was going to post it. Just because I took the trouble of writing it, I'll post it :D

Code: Select all

\documentclass[12pt, twoside, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[spanish,english]{babel}
\usepackage{blindtext}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\selectlanguage{spanish}
\chapter{Implementación práctica del detector de asfalto seco/mojado}
\section{Máquina de Vectores Soporte}
\subsection{Construcción de los conjuntos de entrenamiento y prueba}

\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Problem with page number spacing in table of contents

Post by Johannes_B »

Hm, looking at your real-life example, package tocstyle isn't doing any magic here. Your titles just fit, if they were a tiny bit longer, they would broken to different lines. If they were a tiny bit shorter, it wouldn't look so squeezed.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
doragasu
Posts: 4
Joined: Mon Oct 06, 2014 11:46 am

Re: Problem with page number spacing in table of contents

Post by doragasu »

I'm also playing with tocstyle on my complete document (I'm not playing with the MWE) and the chapter ending with "seco/mojado" still has the problem, but the other conflicting one has been broken down to two lines... Maybe the one that still doesn't get properly formatted has a problem with hyphenation due to the slahs '/' character...

Anyway, changing the style to KOMAlike luckily fixes the problem. I'll maybe just set that style and do not look back...
Post Reply