3

This is the code I'm currently using the customize the section headers in my table of contents:

\usepackage[dotinlabels]{titletoc}
\titlecontents{section}[1pc]
{\addvspace{0pc}}
{\thecontentslabel.\hspace{3mm}}
{}
{\normalsize\titlerule*[6pt]{.}\contentspage}
[\addvspace{0pt}]

And the result looks like this: enter image description here

Question: How can I modify this such that all the section titles will be properly "left-aligned", even the ones starting with section 10? I want the section number to appropriately "move to the left", as required to make this happen. How do I accomplish this?

1 Answer 1

6

You can use a \parbox and \hfill to push \thecontentslabel to the right; something like:

\parbox{1cm}{\hfill\thecontentslabel}

The width of the \parbox can be set using a length in which you specify the widest label. A complete example in which I set the widest label to the width of 99.99.:

\documentclass[fleqn,12pt,a4paper]{book}
\usepackage[dotinlabels]{titletoc}

\newlength\widest
\settowidth\widest{99.99.}

\titlecontents{section}[1pc]
{\addvspace{0pc}}
{\parbox[t]{\widest}{\hfill\thecontentslabel.}
\hspace{3mm}}
{}
{\normalsize\titlerule*[6pt]{.}\contentspage}
[\addvspace{0pt}]

\begin{document}

\tableofcontents

\chapter{Test}
\section{section-1}
\section{section-2}
\section{section-3}
\section{section-4}
\section{section-5}
\section{section-6}
\section{section-7}
\section{section-8}
\section{section-9}
\section{section-10}
\section{section-11}
\section{section-12}
\section{section-13}

\end{document}

enter image description here

3
  • This works, expect for one problem: For some reasons, this solution causes the section number (e.g. "1.1") to move down a bit, and it makes "1.1." a bit below "section", compared to the baseline. Why would this be happening?
    – meh
    Commented May 11, 2012 at 14:00
  • 1
    \parbox[t]{...}{...}
    – daleif
    Commented May 11, 2012 at 14:13
  • @daleif: I incorporated your suggestion to my answer, thanks. Commented May 11, 2012 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .