# Right-align numbers in ToC

How do I right-align the section/subsection/.. number in the ToC? The distance between the number and the heading should be fixed.

|  1     Section
|        1.1      Subsection


I'd like to have something like

|      1 Section
|             1.1 Subsection


(|= left margin)

There must be a package for that...

(And I'd like to play with the indentation as well)

-
you might want to think about this a bit more. while i agree with the concept in general, when section numbers are formatted as "chapter.section" and section goes to two digits, to me it looks a bit nicer with the compound number aligned on the decimal point. (of course, there's really no hope for a three-part subsection number with segments numbered 10 or greater.) –  barbara beeton Aug 17 '12 at 12:54
@barbarabeeton I have thought about it for quite some time and for my case the best solution is to have the numbers right aligned and only a little bit of indentation to the next sublevel :) I have very, very, very long section numbers. The default is just not feasible. –  topskip Aug 17 '12 at 13:10

\numberline could be redefined:

\documentclass{article}

\makeatletter
\renewcommand*{\numberline}[1]{%
\hb@xt@\@tempdima{\hfil#1 }%
}
\makeatother

\begin{document}
\tableofcontents

\section{First}
\subsection{Sub}

See also tocloft package, page 11. It offers facilities to play with the indentation. –  Guido Aug 17 '12 at 12:54
Thanks Heiko for the perfect solution. I should read latex.ltx more often. –  topskip Aug 17 '12 at 13:12