[Tex/LaTex] Moving the page number in a table of contents to the left of the titles with tocloft

horizontal alignmenttable of contentstocloft

I would like to move the page number from the right-hand side of a table of contents to the left, using tocloft. The page number should still be right-aligned.

The question has already been answered here for titletoc, but I would like to know if there is a way to achieve the same using tocloft. The question referred to suggests not, but I would like to check anyway.

I start with a MWE:

\documentclass[a4paper]{article}

\usepackage{lipsum}
\RequirePackage[titles]{tocloft}

\makeatletter

\renewcommand{\cftsecpresnum}{\begin{lrbox}{\@tempboxa}}
\renewcommand{\cftsecaftersnum}{\end{lrbox}}
\setlength{\cftsecnumwidth}{0pt}
\makeatother

\begin{document}
\tableofcontents
\section{Section 1 title}
\lipsum[1-2]
\section{Section 2 title}
\lipsum[3-4]
\end{document}

That produces this:

page numbers on right

I am trying to move the page numbers to the other side:

page numbers on left

And in case you're wondering, this is what I'm actually trying to achieve (without the blue lines). I've done the rest of the formatting successfully using tocloft, which is why I would prefer to continue using that package rather than switching to titletoc.

picture of final aim

Best Answer

This update achieves right alignment, as explained in my added comment. The defect is that it will not work if \thepage is not the standard representation with digits. Also the placement of the page number is not so good in the case of multiline section titles.

page on left

\documentclass[a4paper]{article}

\usepackage{lipsum}
\RequirePackage[titles]{tocloft}

\newcount\cnta
\cftsetpnumwidth{0pt}
\renewcommand\cftsecpagefont {\global\cnta}
\renewcommand\cftsecafterpnum {%
  \makebox[0pt][l]%
     {\kern-\dimexpr\linewidth+2em\relax\makebox[1em][r]{\the\cnta}}%
}

\begin{document}
\tableofcontents
\section{First section}
\lipsum[1]
\clearpage
\setcounter{page}{13}
\section{Second section}
\lipsum[2]
\clearpage
\setcounter{page}{315}
\section{A very very very very very veryvery very veryvery very veryvery very
  veryvery very very long section title} 
\end{document}

Without hacking tocloft internal redefinition of \l@section I don't think there is a way. Here is an attempt though. Defects

  1. the section titles should not extend on multiple lines
  2. the page numbers will be left aligned, not right aligned

page on left

\documentclass[a4paper]{article}

\usepackage{lipsum}
\RequirePackage[titles]{tocloft}

\cftsetpnumwidth{0pt}
\renewcommand{\cftpnumalign}{l}
\toks0 \expandafter{\cftsecpagefont}
\edef\cftsecpagefont {\kern-\dimexpr\linewidth+2em\relax\the\toks0 }

\begin{document}
\tableofcontents
\section{First section}
\lipsum[1]
\section{Second section}
\lipsum[2]
\section{A very very very very very veryvery very veryvery very veryvery very
  veryvery very very long section title} 
\end{document}

With package etoc one may obtain all of your layout.