# Issue with TOC alignment when using fontspec and titletoc with font size >10pt

I am in the process of switching a large document to lualatex for various reasons, and I noticed an issue with the table of contents when the packages fontspec and titletoc are loaded together, AND the font size is larger than the default 10pt.

When the page number to display in the TOC is above 3 digits, it protudes over the right margin.

A quick search have yielded this similar issue (http://www.macfreek.nl/memory/LaTeX_package_conflicts#XeTeX_and_titletoc) but it is with XeTeX, so I am not sure it is related.

MWE:

\documentclass[12pt]{book}
\usepackage{lipsum}

\usepackage[showframe]{geometry}

\usepackage{fontspec}

\usepackage{titletoc}

\begin{document}
\tableofcontents

\chapter{Chapter 1}
\section{Section 1.1}
\lipsum[1-100]
\section{Section 1.2}
\lipsum[1-100]
\section{Section 1.3}
\lipsum[1-100]

\chapter{Chapter 2}
\section{Section 2.1}
\lipsum[1-100]
\section{Section 2.2}
\lipsum[1-100]
\section{Section 2.3}
\lipsum[1-100]

\chapter{Chapter 3}
\section{Section 3.1}
\lipsum[1-100]
\section{Section 3.2}
\lipsum[1-100]
\section{Section 3.3}
\lipsum[1-100]
\end{document}


This yields this result on the first page. Notice how the 3-digits page numbers extend outside of the text area (for sections lines only, not chapter lines though...)

Changing to 10pt, or commenting either \usepackage{fontspec} or \usepackage{titletoc} removes the issue.

With 10pt:

12pt, no fontspec:

12pt, no titletoc:

I guess your question is "What's going on here?"

Loading fontspec changes the current font from Computer Modern to Latin Modern. And the length governing the width of the page number in the ToC, given by \@pnumwidth, is defined in terms of the font using ems.

To isolate the problem, compile the following document with either XeLaTeX or LuaLaTeX:

\documentclass[12pt]{article}

\newlength{\nofontspecem}
\setlength{\nofontspecem}{1em}
\showthe\nofontspecem% Length of 1em in Computer Modern

\usepackage{fontspec}

\newlength{\fontspecem}
\setlength{\fontspecem}{1em}
\showthe\fontspecem% Length of 1em in Latin Modern

\begin{document}
abc
\end{document}


The .log shows

> 11.74988pt.
l.5 \showthe\nofontspecem
% Length of 1em in Computer Modern

...

> 10.0pt.
l.11 \showthe\fontspecem
% Length of 1em in Latin Modern


So, Computer Modern has a slightly wider em under 12pt font than that of Latin Modern. As such \@pnumwidth has a little more room to place the ToC page numbers, not making them spill into the right margin.

How can you fix this? Provide an adequate value for \@pnumwidth that is larger than the default of 1.55em, so 2em, or something non-font-related, like 25pt. With titletoc loaded, simply issue

\contentsmargin{2em}

• I see.. I would have thought this was detected somehow automatically.. Thanks for guessing the question and answering it ! I think I've managed to get rid of all warnings now =) – AElfwine Nov 4 '15 at 1:22