0

My page numbering isn't aligned properly starting with page VIII. It seems to be somehow focused on the X, but also the second chapter isn't aligned properly, where the first, which has arabic numbers, is properly aligned.
Do you have a clue how to solve this?

MWE.tex

\documentclass[a4paper,12pt]{report}
\usepackage{lipsum}
\begin{document}
        \tableofcontents
        \pagenumbering{arabic}
        \chapter{Chapter}
                \section{Lorem}
                        \lipsum[1]\newpage
                \section{ipsum}
                        \lipsum[1]\newpage
                        \pagenumbering{Roman}
                \section{dolor}
                        \lipsum[1]\newpage
                \section{sit}
                        \lipsum[1]\newpage
                \section{amet}
                        \lipsum[1]\newpage
                \section{consectetuer}
                        \lipsum[1]\newpage
                \section{adipiscing}
                        \lipsum[1]\newpage
                \section{elit}
                        \lipsum[1]\newpage
                \section{Ut}
                        \lipsum[1]\newpage
        \chapter{Chapter}
                \section{purus}
                        \lipsum[1]\newpage
                \section{elit}
                        \lipsum[1]\newpage
                \section{vestibulum}
                        \lipsum[1]\newpage
                \section{ut}
                        \lipsum[1]\newpage
                \section{placerat}
                        \lipsum[1]\newpage
\end{document}
Nepumuk
  • 483

2 Answers2

1

The usual solution is to use the tocloft package and it's \cftsecnumwidth{<length>} to set the space for page numbers. You have to specify the <length> argument to suit your document. Below 2em works for your MWE.

\documentclass[a4paper,12pt]{report}
\usepackage{lipsum}
\usepackage{tocloft}
\cftsetpnumwidth{2em}  % space for page numbers, default is 1.55em
\begin{document}
% all the rest of your MWE
Peter Wilson
  • 28,066
  • But then all dots end at a same fixed place, independent of the number width. The behavior is different on the other side. Why having a fix width box is the best design solution? – Simon Dispa Dec 15 '20 at 19:31
0

It can be solved with a small modification of \@dottedtocline (file latex.ltx). I added more pages to the MWE to see more Roman letters.

\documentclass[a4paper,12pt]{report}
\usepackage{lipsum}

\makeatletter \def@dottedtocline#1#2#3#4#5{% \ifnum #1>\c@tocdepth \else \vskip \z@ @plus.2\p@ {\leftskip #2\relax \rightskip @tocrmarg \parfillskip -\rightskip \parindent #2\relax@afterindenttrue \interlinepenalty@M \leavevmode @tempdima #3\relax
\advance\leftskip @tempdima \null\nobreak\hskip -\leftskip {#4}\nobreak \leaders\hbox{$\m@th \mkern @dotsep mu\hbox{.}\mkern @dotsep mu$}\hfill \nobreak %\hb@xt@@pnumwidth{\hfil\normalfont \normalcolor #5% {\hfil\normalfont \normalcolor #5%
\kern-\p@\kern\p@}% \par}%
\fi}
\makeatother

\begin{document} \tableofcontents
\pagenumbering{arabic} \chapter{Chapter 1} \section{Lorem} \lipsum[1-18]\newpage \section{ipsum} \lipsum[1]\newpage \pagenumbering{Roman} \section{dolor} \lipsum[1-16]\newpage \section{sit} \lipsum[1-16]\newpage \section{amet} \lipsum[1]\newpage \section{consectetuer} \lipsum[1-8]\newpage \section{adipiscing} \lipsum[1]\newpage \section{elit} \lipsum[1-15]\newpage \section{Ut} \lipsum[1]\newpage \chapter{Chapter 2} \section{purus} \lipsum[1-14]\newpage \section{elit} \lipsum[1]\newpage \section{vestibulum} \lipsum[1-16]\newpage \section{ut} \lipsum[1-16]\newpage \section{placerat} \lipsum[1]\newpage \end{document}

x2

when before it was

x1

the change replacing

 \nobreak
 \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5%
                    \kern-\p@\kern\p@}%
 \par}%
\fi}

by

 \nobreak
 {\hfil\normalfont \normalcolor #5%
                    \kern-\p@\kern\p@}%
 \par}%
\fi}

A fixed box does not work. The alignment also works with

\hb@xt@35\p@{\hfil\normalfont \normalcolor #5%

but obviously the dots do not look so nice with a fixed width, which is the problem with the kind of answers in

In table of contents, long page numbers intrude on right margin despite plenty of available space

x3

versus

x4

My TeX is not good enough to go any further.

Simon Dispa
  • 39,141