[Tex/LaTex] TOC Page Numbers, A-pagenum left aligned, S-pagenum centered

horizontal alignmentmemoirtable of contents

I have customized some page numberings. My adviser wanted appendix pages labeled as A-pagenum. So that was accomplished with \renewcommand{\thepage}{A-\arabic{page}} while my supporting info labeled with S-pagenum, so \renewcommand{\thepage}{S-\arabic{page}}.

However, looking at my TOC, it appears that my A pages are left aligned when the pagenum switches from single to double digits. But my S pages are maybe…centered? It looks off, and I have no clue why the S centers, and the A does not.

Seems like one of those gotcha things that maybe I'm not aware of, or overlooking. But I would like the S pages to also be left-aligned like the A ones.

Here is a MWE (which is a bit longer, because it is a MWE for a related post)

\documentclass{memoir}
\setlength{\parskip}{12pt plus2pt}
\def\baselinestretch{1.6}
\linespread{1.3}
\usepackage{titletoc}

\begin{document}
\tableofcontents*
\setsecnumdepth{subsection}
\maxsecnumdepth{subsection}
\settocdepth{subsection}    
\chapter{Chap1}
\section{Chap1Sec1}
\section{Chap1Sec2}
\chapter{Chap2}
\section{Chap2Sec1}
\section{Chap2Sec2}
\subsection{Chap2Sec2Subsec1}
\chapter{Chap3}
\settocdepth{section}
\renewcommand{\thesection}{\thechapter-\Roman{section}}
\renewcommand{\thesubsection}{\thechapter-\Roman{section}.\arabic{subsection}}
\renewcommand{\thepage}{S-\arabic{page}}
\section{Chap3Sec1}
\clearpage
\section{Chap3Sec2}

\startcontents
\printcontents{}{2}{\addtocontents{ptc}{\setcounter{tocdepth}{2}}}
\subsection{Chap3Sec2Subsec1}
\subsection{Chap3Sec2Subsec2}
\subsection{Chap3Sec2Subsec3}
\subsection{Chap3Sec2Subsec4}
\stopcontents
\section{Chap3Sec3}\clearpage
\section{Chap3Sec4}\clearpage
\section{Chap3Sec5}\clearpage
\section{Chap3Sec6}\clearpage
\section{Chap3Sec7}\clearpage
\renewcommand{\thepage}{A-\arabic{page}}
\setcounter{page}{9}
\chapter{Chap4}
\settocdepth{subsection}
\section{Chap4Sec1}\clearpage
\section{Chap4Sec2}\clearpage
\subsection{Chap4Sec2Subsec1}
\end{document}

Best Answer

It's not left-aligned vs. centered, but considerably vs. slightly exceeding the width of a box. Quoting section 9.2.2. of the memoir manual:

The page numbers [in the ToC] are typeset in a fixed width box. The command \setpnumwidth can be used to change the width of the box (LaTeX ’s internal \@pnumwidth). The title texts will end before reaching the righthand margin. \setrmarg can be used to set this distance (LaTeX ’s internal \@tocrmarg). Note that the length used in \setrmarg should be greater than the length set in \setpnumwidth. These values should remain constant in any given document.

Enlarging the values of \@pnumwidth and \@tocrmarg will make the page numbers (IMO, correctly) right-aligned. To make them left-aligned, you also have to fiddle with the various \cftKformatpnum macros.

\documentclass{memoir}
\setlength{\parskip}{12pt plus2pt}
\def\baselinestretch{1.6}
\linespread{1.3}
\usepackage{titletoc}

\setpnumwidth{2.5em}
\setrmarg{3.5em}

\makeatletter
\renewcommand*{\cftchapterformatpnum}[1]{%
    \hbox to \@pnumwidth{{\cftchapterpagefont #1}}}
\renewcommand*{\cftsectionformatpnum}[1]{%
    \hbox to \@pnumwidth{{\cftsectionpagefont #1}}}
\renewcommand*{\cftsubsectionformatpnum}[1]{%
    \hbox to \@pnumwidth{{\cftsubsectionpagefont #1}}}
\makeatother

\begin{document}
\tableofcontents*
\setsecnumdepth{subsection}
\maxsecnumdepth{subsection}
\settocdepth{subsection}    
\chapter{Chap1}
\section{Chap1Sec1}
\section{Chap1Sec2}
\chapter{Chap2}
\section{Chap2Sec1}
\section{Chap2Sec2}
\subsection{Chap2Sec2Subsec1}
\chapter{Chap3}
\settocdepth{section}
\renewcommand{\thesection}{\thechapter-\Roman{section}}
\renewcommand{\thesubsection}{\thechapter-\Roman{section}.\arabic{subsection}}
\renewcommand{\thepage}{S-\arabic{page}}
\section{Chap3Sec1}
\clearpage
\section{Chap3Sec2}

\startcontents
\printcontents{}{2}{\addtocontents{ptc}{\setcounter{tocdepth}{2}}}
\subsection{Chap3Sec2Subsec1}
\subsection{Chap3Sec2Subsec2}
\subsection{Chap3Sec2Subsec3}
\subsection{Chap3Sec2Subsec4}
\stopcontents
\section{Chap3Sec3}\clearpage
\section{Chap3Sec4}\clearpage
\section{Chap3Sec5}\clearpage
\section{Chap3Sec6}\clearpage
\section{Chap3Sec7}\clearpage
\renewcommand{\thepage}{A-\arabic{page}}
\setcounter{page}{9}
\chapter{Chap4}
\settocdepth{subsection}
\section{Chap4Sec1}\clearpage
\section{Chap4Sec2}\clearpage
\subsection{Chap4Sec2Subsec1}
\end{document}