3

In setting up a table of contents, by default the TOC spans the entire width of the page, regardless of the length of the section titles. I would like to have the entire TOC be only as wide as it needs to be, with the option of including a small additional space (1-4em) between the longest section title and the page number. I would also like to have all the page numbers on the right lined up (i.e. not ragged). Is there a way to do this?

I have to imagine this is possible with tocloft, but I can't seem to find the right settings. I am doing my best to avoid package bloat (which is getting hard to manage), so I would be extra appreciative of solutions using tocloft.

Edit: Here is a MWE

\documentclass[12pt]{article}
\usepackage{setspace, tocloft}

%Modifies line spacing of the ToC
\setlength\cftparskip{-1.2pt}
\setlength\cftbeforesecskip{1.3pt}
\setlength\cftaftertoctitleskip{2pt}

%Makes dots after sections/subsections: Sections 1., 2.1., etc
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname.\quad}
\makeatother

%Makes the dots (above) appear in ToC
\let \savenumberline \numberline
\def \numberline#1{\savenumberline{#1.}}

\begin{document}
\tableofcontents
\begin{section}{Section 1}
\begin{subsection}{Section 1.1}\end{subsection}
\begin{subsection}{Section 1.2}\end{subsection}
\end{section}
\begin{section}{Section 2}\end{section}
\end{document}

Which produces:enter image description here

As mentioned above, I would like to control the width between the end of the section titles and the beginning of the page numbers, keeping the page numbers vertically aligned.

6

Simply add these lines to your preamble

\renewcommand{\cftsecafterpnum}{\hspace*{7.5em}}
\renewcommand{\cftsubsecafterpnum}{\hspace*{7.5em}}

and adjust 7.5em to your needs.

MWE

\documentclass[12pt]{article}
\usepackage{setspace, tocloft}

%Modifies line spacing of the ToC
\setlength\cftparskip{-1.2pt}
\setlength\cftbeforesecskip{1.3pt}
\setlength\cftaftertoctitleskip{2pt}

\renewcommand{\cftsecafterpnum}{\hspace*{7.5em}}
\renewcommand{\cftsubsecafterpnum}{\hspace*{7.5em}}

%Makes dots after sections/subsections: Sections 1., 2.1., etc
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname.\quad}
\makeatother

%Makes the dots (above) appear in ToC
\let \savenumberline \numberline
\def \numberline#1{\savenumberline{#1.}}

\begin{document}
\tableofcontents
\begin{section}{Section 1}
\begin{subsection}{Section 1.1}\end{subsection}
\begin{subsection}{Section 1.2}\end{subsection}
\end{section}
\begin{section}{Section 2}\end{section}
\end{document} 

Output

enter image description here

A remark

If you want to add dots after sections and subsections numbers, you can use

\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}

instead of

\let \savenumberline \numberline
\def \numberline#1{\savenumberline{#1.}}
  • Thanks! I'm partially glad to see this command was buried in the "code" section of the documentation for toclof, and partially bewildered about where to go to find general information on doing things like this. – RghtHndSd Sep 7 '13 at 18:01
2

I don't have enough reputation points to add this as a comment, but going off of what @karlkoeller suggested, you can also add:

\setlength{\cftsecindent}{7.5em}
\setlength{\cftsubsecindent}{\cftsecindent}
\addtolength{\cftsubsecindent}{2.3em}

to your preamble, again adjusting 7.5em to your needs. This centers (I think exactly) your TOC by changing the left indent to match the right "indent" (pnum). The third line preserves the default spacing provided by the tocloft package, but you can set a custom indent by deleting the third line and assigning the width in the second set of braces currently occupied by \cftsecindent, eg.

\setlength{\cftsecindent}{7.5em}
\setlength{\cftsubsecindent}{10em}

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.