2

I'm writing my thesis, but unfortunately I'm having a problem. In our thesis template, the page numbers in table of contents should be aligned left (default=right) as shown in the figure. When I look for the solution of it, I learned that tocloft package allows that by using \renewcommand{\cftpnumalign}{l}.

But it gives an error as

"! LaTeX Error: \cftpnumalign undefined."  

I don't know how can I fix. Is there an easy solution of the problem?

Thank you in advance. !!

enter image description here

Here is an example.

\documentclass{article}

\usepackage{tocloft}
\usepackage{titletoc}

\titlecontents{section}
[0.0em]
{}
{\thecontentslabel.\enspace{\hspace*{-0.2em}}}
{}
{\titlerule*[0.25pc]{.}\contentspage}


\titlecontents{subsection}
[1.6em]
{}
{\thecontentslabel.\enspace{\hspace*{-0.2em}}}
{}
{\titlerule*[0.25pc]{.}\contentspage}


\titlecontents{subsubsection}
[1.6em]
{}
{\thecontentslabel.\enspace{\hspace*{-0.2em}}}
{}
{\titlerule*[0.25pc]{.}\contentspage}


\setcounter{tocdepth}{3}
\setlength{\cftsecnumwidth}{1ex}
\setlength{\cftsubsecnumwidth}{3ex}
\setlength{\cftbeforesecskip}{1pt}

\renewcommand{\cftpnumalign}{l} 

\begin{document}
\pagenumbering{roman}

\tableofcontents
\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{9}
\section{three}


\newpage
\addtocounter{page}{9}
\section{four}


\newpage
\addtocounter{page}{1}
\subsection{five}


\clearpage
\pagenumbering{arabic}

\tableofcontents
\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{259}
\section{three}


\newpage
\addtocounter{page}{8699}
\section{four}


\newpage
\addtocounter{page}{1}
\subsection{five}


\end{document}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. As given, your MWE doesn't show the error you're describing. – Mike Renfro May 15 '16 at 0:41
  • 1
    Also, an alternative preamble with only \usepackage{tocloft} \renewcommand{\cftpnumalign}{l} in a simple document with a single table of contents and both roman and arabic page numbering does show left-aligned numbers. – Mike Renfro May 15 '16 at 0:50
  • titletoc and tocloft aren't compatible, in my point of view. Removing titletoc will provide the left aligned page numbers – user31729 May 15 '16 at 6:39
  • thank you for the update. I tried what you said, but unfortunately it again gave the same error. – zkrdnz May 15 '16 at 13:38
  • yes, it is latest version. Did you mean adding the error message in logfile. That is like in the following. { "File List article.cls 2007/10/19 v1.4h Standard LaTeX document class size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option) tocloft.sty 2010/10/13 v2.3e parameterised ToC, etc., typesetting titletoc.sty 2011/12/15 v1.6 TOC entries"} is it correct ? or else. – zkrdnz May 15 '16 at 20:11
2

Don't mix the use of two ToC-related packages.

That's why any adjustment to \cftpnumalign doesn't show in the ToC, since titletoc has completely rewritten the way ToC-related entries are handled.

The following is all that's needed to change the alignment (and width):

\makeatletter
\renewcommand{\contentspage}[1][\thecontentspage]{\hb@xt@\@pnumwidth{#1\hfil}\hspace*{-\@pnumwidth}}
\renewcommand{\@pnumwidth}{3em}
\makeatother

The first adjustment is made to \contentspage in order to change the alignment from right (default) to left. The second changes the width of the page setting to 3em (default is 15pt).

enter image description here

\documentclass{article}

\usepackage{titletoc}

\titlecontents{section}
  [0.0em]
  {}
  {\thecontentslabel.\enspace{\hspace*{-0.2em}}}
  {}
  {\titlerule*[0.25pc]{.}\contentspage}

\titlecontents{subsection}
  [1.6em]
  {}
  {\thecontentslabel.\enspace{\hspace*{-0.2em}}}
  {}
  {\titlerule*[0.25pc]{.}\contentspage}

\titlecontents{subsubsection}
  [1.6em]
  {}
  {\thecontentslabel.\enspace{\hspace*{-0.2em}}}
  {}
  {\titlerule*[0.25pc]{.}\contentspage}

\setcounter{tocdepth}{3}

\makeatletter
\renewcommand{\contentspage}[1][\thecontentspage]{\hb@xt@\@pnumwidth{#1\hfil}\hspace*{-\@pnumwidth}}
\renewcommand{\@pnumwidth}{3em}
\makeatother

\begin{document}
\pagenumbering{roman}

\tableofcontents
\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{9}
\section{three}


\newpage
\addtocounter{page}{9}
\section{four}


\newpage
\addtocounter{page}{1}
\subsection{five}

\clearpage
\pagenumbering{arabic}

\tableofcontents
\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{259}
\section{three}

\newpage
\addtocounter{page}{8699}
\section{four}

\newpage
\addtocounter{page}{1}
\subsection{five}

\end{document}

The frame is provided by showframe.

  • It is working, great thanks. I spent too much time to solve it. That is nice to see. – zkrdnz May 16 '16 at 12:07
1

I don't know about tocloft or titletoc, but for plain old article class...

\documentclass{article}
\usepackage{showframe}

\makeatletter
\def\@pnumwidth{.5in}% maximum width for number on right

\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty\@secpenalty
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries #1\hfil \hb@xt@\@pnumwidth{#2\hss}}\par% left align
       \nobreak
       \if@compatibility
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
      \fi
    \endgroup
  \fi}

\renewcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{#2\hss}\par% left align
    \endgroup
  \fi}

\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{\normalfont \normalcolor #5\hfil}% left align
 \par}%
 \fi}
\makeatother

\begin{document}
\pagenumbering{roman}

\tableofcontents
\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{9}
\section{three}


\newpage
\addtocounter{page}{9}
\section{four}


\newpage
\addtocounter{page}{1}
\subsection{five}


\clearpage
\pagenumbering{arabic}

\newpage
\section{one}

\newpage
\addtocounter{page}{25}
\subsection{two}

\newpage
\addtocounter{page}{259}
\section{three}


\newpage
\addtocounter{page}{8699}
\section{four}


\newpage
\addtocounter{page}{1}
\subsection{five}


\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.