0

When using the \titlecontents command of titletoc to create different text formats for the different section levels, the page numbers become unaligned. For example

\documentclass{book} \usepackage{titletoc} \contentsmargin{2.55em} \titlecontents{chapter}[0pt]{\scshape}{\thecontentslabel}{}{\hfill \contentspage[\bfseries \thecontentspage]} \titlecontents{section}[2cm]{\bfseries}{\thecontentslabel}{}{\hfill \contentspage} \begin{document} \tableofcontents{} \chapter{Lorem} \section{Ipsum} \end{document} 

Results in (red line added by me for demonstration) unaligned page numbers

3 Answers 3

1

Answering my own question:

The problem seems to be specifically with using formatting commands such as \bfseries in the before/above part of the command. Somehow they end up influencing how the filler part works. One solution I found was using \normalfont first thing in the filler part and reapplying any formatting to the page number afterwards (if desired). Example:

\documentclass{book} \usepackage{titletoc} \contentsmargin{2.55em} \titlecontents{chapter}[0pt]{\scshape}{\thecontentslabel}{}{\normalfont\hfill \contentspage[\bfseries \thecontentspage]} \titlecontents{section}[2cm]{\bfseries}{\thecontentslabel}{}{\normalfont\hfill \contentspage[\bfseries \thecontentspage]} \begin{document} \tableofcontents{} \chapter{Lorem} \section{Ipsum} \end{document} 

This now results in enter image description here However it should be noted that \contentsmargin seems to also play some part here, since I needed to add it to the first example in order to get at least strongly noticeable misalignment. I very much welcome thoughts and explainations from the experts why that is.

1

\contentsmargin is for the right margin of the table of contents. This code produces a right margin of 1cm:

\documentclass{book} \usepackage[showframe]{geometry}% \usepackage{titletoc} \contentsmargin{1cm} \titlecontents{chapter}[3em]{\scshape}{\contentslabel[\thecontentslabel]{2.8em}}{}{\hfill\bfseries\thecontentspage} \titlecontents{section}[2cm]{\bfseries}{\contentslabel[\thecontentslabel]{\dimexpr2cm-2.6em}}{}{\hfill\thecontentspage} \begin{document} \tableofcontents \chapter{Lorem} \section{Ipsum} \end{document} 

enter image description here

Changing to \contentsmargin{0cm} gives:

enter image description here

1
  • Yes I'm aware however how and why does that affect the alignment? try it yourself, remove the contentsmargin from the code in my "question" and it's pretty much aligned. I just did a bit more experimentation and for \contentsmargin{0em} alignment is also achieved. But already for a value of 2pc misalignment can be detected by drawing a line. It is very miniscule though. At 1em it is noticeable to the naked eye. Commented Sep 22, 2016 at 19:43
0

I just ran into the same issue. It only seems to occur if you change the font in \titlecontents and specify \contentsmargin in em units. Since the size of 1em depends on the font size, titletoc puts the page number in an incorrectly sized box. The easiest solution is to specify \contentsmargin in another unit, say

\contentsmargin{30pt} 
2
  • Welcome to TeX.SX! Did you saw the answer of user @bernhard? Commented Feb 28, 2020 at 11:43
  • Yes, I did, but it doesn't actually address the original question. My answer is actually a response to @Danieru's answer above, but I don't have enough reputation to comment yet. Commented Feb 29, 2020 at 18:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.