2

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

3

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.

  • I thought later about a hacky way, but only if \thepage prints out arabic numbers. The idea is to redefine \cftsecpagefont to be something like \global\acount with \acount a count registers. Then \cftsecafterpnum will do the page number placement: the advantage is that we can get it right aligned then, however it will still be difficult to position it higher up in case of multiline section name. – user4686 Apr 4 '14 at 18:06
  • i've never done this with tocloft, only with plain tex (see the tugboat toc for the relevant example). see whether it's possible to reorder the pieces that go into a toc line, setting the page number as the first element, followed by the rest, with the text hanging indented to the appropriate distance -- in other words, treating the whole thing as a hanging indented (ragged right) paragraph. – barbara beeton Apr 4 '14 at 18:24
  • @barbarabeeton using etoc and LaTeX2e all of this re-shuffling is easy to achieve. The page number is available as \etocpage, the section number as \etocnumber, the section title as \etocname. – user4686 Apr 4 '14 at 18:27
  • @jfbu I think it would be a nice addition to your answer if you could provide the actual code with etoc; it would show the power of the package. On a second thought, the question asks specifically for tocloft, so perhaps this is not the place to show the power of etoc? – Gonzalo Medina Apr 4 '14 at 18:33
  • @GonzaloMedina yes, I think this might not be the place... besides, power comes at the prize of having to define the line styles... and once one starts one has to do it for part, section, subsection... this is why I tried to say here in the tocloft theme. – user4686 Apr 4 '14 at 18:45

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.