# Customising the page number in ToC using titletoc

I am using titletoc to customise the ToC. Right now there are three things I cannot solve (I guess, due to the fact that page number is typeset in a fixed-width box):

1. The titlerule doesn't fill till the page number.
2. If page number is too large, it exceeds into the right margin.
3. If the title is too long, it bumps into the page number.

Here is an illustration:

I have fixed the first issue with

\makeatletter
\renewcommand{\@pnumwidth}{0pt{}}
\makeatother


but the new problem appears

Full MWE:

\documentclass{book}

\usepackage[showframe]{geometry}
\usepackage{titletoc}
\makeatletter
\renewcommand{\@pnumwidth}{0pt{}}
\makeatother
\titlecontents{chapter}
[1cm]
{\vspace{1cm}\filright}
{\large\sffamily\bfseries\contentslabel[\thecontentslabel.]{1cm}}
{\hspace{-1cm}}
{\titlerule*[3pt]{.}\sffamily\bfseries\large\contentspage}

\begin{document}

\tableofcontents

\chapter{one}

Sample text

\cleardoublepage

\addtocounter{page}{1000}

\chapter{two}

\chapter{a long long long long long long long long long long long long long  title}

\end{document}

• \renewcommand{\p@numwidth}{0pt} Really? Leaving no space for the page number box? – user31729 Nov 3 '17 at 19:44
• @ChristianHupfer David Carlisle suggested this as a solution to my first issue in one question on this site – Michael Fraiman Nov 3 '17 at 20:19

## 1 Answer

This is the best I could obtain with the tools from titletoc. It solves points 2 and 3.

\documentclass{book}

\usepackage[showframe]{geometry}
\usepackage{titletoc}
\contentsmargin{1cm}
\titlecontents{chapter}
[0.6cm]
{\vspace{1cm}\filright\large\sffamily\bfseries}
{\contentslabel[\thecontentslabel.]{1.3em}}
{\hspace{-1cm}}
{{\mdseries\hspace{0.4em}\titlerule*[6pt]{.}}\contentspage}

\begin{document}

\tableofcontents

\chapter{one}

Sample text

\cleardoublepage

\addtocounter{page}{1000}

\chapter{two}

\chapter{a long long long long long long long long long long long long long long title}

\end{document}


• Looks like this works. Can everything be achieved with another package? – Michael Fraiman Nov 4 '17 at 15:28
• I guess tocloft can do it, but I don't know it well enough to be sure. As I use titlesec, I prefer to stick to the companion packages titletoc and titleps. – Bernard Nov 4 '17 at 15:49
• to my knowledge, tocloft is less powerfull. And I also use titlesec, so tocloft is the choice – Michael Fraiman Nov 4 '17 at 15:51
• @MichaelFraiman: tocloft is less powerful? That's a matter of choice. tocloft and titletoc however should not be used together. – user31729 Nov 4 '17 at 19:09
• @ChristianHupfer can this be made with tocloft? – Michael Fraiman Nov 5 '17 at 16:28