Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the tocloft package to design my table of contents. I know how to set the width of the chapter numbers in the toc using

\setlength{\cftchapnumwidth}{2em}

However this sets a fixed width of 2em for all chapter numbers. Some chapter numbers need more space than others - .e.g. XVII vs V. Is there a way to set the width to dynamically include the width of the chapter number followed by one space?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

In this instance you're most likely interested in adjusting the number widths of all sectional units in the ToC, since they are usually set hierarchically. As such, a simple redefinition of the \numberline macro suffices:

\usepackage{tocloft}% http://ctan.org/pkg/tocloft
\makeatletter
\renewcommand{\numberline}[1]{%
  \@cftbsnum #1\@cftasnum~\@cftasnumb%
}
\makeatother

The above removes the former \hb@xt@\@tempdima{...} construction, which sets the number inside a fixed-width box, left-aligned. In fact, the original definition made by tocloft is:

\renewcommand{\numberline}[1]{%
  \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}

Here's a minimal working example showing the result:

enter image description here

\documentclass{report}
\usepackage{tocloft}% http://ctan.org/pkg/tocloft
\makeatletter
\renewcommand{\numberline}[1]{%
  \@cftbsnum #1\@cftasnum~\@cftasnumb%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{A chapter}\section{A section}
\setcounter{chapter}{11}
\chapter{A chapter}\section{A section}
\setcounter{chapter}{122}
\chapter{A chapter}\section{A section}
\setcounter{chapter}{1233}
\chapter{A chapter}\section{A section}
\setcounter{chapter}{12344}
\chapter{A chapter}\section{A section}
\end{document}

Since the original definition left a "dynamic" gap between the number and the title, I've forced it to be ~ in my example. You can modify that to whatever length you're interested in (\quad, \hspace*{2em}, ...).

Note that this modification has to be inserted before calling \tableofcontents.

share|improve this answer
    
Thanks, that worked great! –  COM Jul 24 '12 at 17:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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