[Tex/LaTex] How to set Table of Contents page numbers ragged right

horizontal alignmentmemoirtable of contents

First post and certainly a tyro at LaTeX. Thanks to all those who have made this site such a helpful resource.

I'm using the memoir class to typeset a novel, and would like to have the Table of Contents page numbers appear ragged right, each page number set a fixed horizontal distance from the end of each chapter title, regardless of chapter title length. I would like to be able to try different spacings to find the most pleasing horizontal gap between the end of chapter title lines and the page numbers.

Any solutions or pointers greatly appreciated.

Best Answer

Change the family of commands \cftXleader \cftXafterpnum as shown below:

\documentclass{memoir}

\renewcommand{\cftchapterleader}{}
\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\renewcommand{\cftsectionleader}{}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}

\begin{document}

\tableofcontents
\chapter{Test Chapter}
\section{Test Section}

\end{document}

enter image description here

To control the distance you could define auxiliary lengths to be used as the box width used by \cftXformatpnum to format the page number:

\documentclass{memoir}

\newlength\chaplength
\newlength\seclength
\setlength\chaplength{7em}
\setlength\seclength{2em}

\renewcommand{\cftchapterleader}{}
\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\renewcommand*{\cftchapterformatpnum}[1]{%
  \hbox to \chaplength{\hfill{\cftchapterpagefont #1}}}

\renewcommand{\cftsectionleader}{}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}
\renewcommand*{\cftsectionformatpnum}[1]{%
  \hbox to \seclength{\hfill{\cftsectionpagefont #1}}}

\begin{document}

\tableofcontents
\chapter{Test Chapter}
\section{Test Section}

\end{document}

enter image description here