How to align underlined text and a horizontal line created with \rule

We are putting a horizontal line under the whole section text up to the page number. Therefore we cannot just use \underline for the section text (because this would only affect the text) but have to use \rule to put the underlining.

However, the underlining for the chapter numbers can just be placed with \underline because we want to have a gap btw. the underlining of the chapter number and the underlining of the chapter title+page.

As you can see in the following snippet, the line is currently positioned with [-0.5mm] ...

\usepackage{tocloft}
\renewcommand{\cftchappresnum}{\underline} % chapter numbers are underlined
\renewcommand{\cftchapaftersnumb}{\rlap{\rule[-0.5mm]{0.96\textwidth}{.4pt}\hss}} % chapter entries are underlined as well


... however this does not correctly align with the previous underline.

I think the question is now: What would be the correct length (in ex?) to move the line so that it aligns with an underline on the same line?

Also: What would be the correct thickness so it's the same as for the underline?

-

\usepackage{calc}
\usepackage{tocloft}
\newlength\tocchaplength
\setlength\tocchaplength{\textwidth-\cftchapnumwidth}
\renewcommand\cftchapaftersnumb{\rlap{\underline{\hbox
to\the\tocchaplength{\hfil}}}}
\renewcommand\cftchappresnum{\underlinenumber}
\makeatletter
\def\underlinenumber#1\@cftasnum{\underline{#1}}
\makeatother


Edit: The first version didn't handle multiple token chapter numbers (so 10 and higher were wrong). The second version didn't handle noninteger chapter numbers. This version seems to work for any chapter numbers (as long as the numbers do not descend below the baseline).

Edit 2: Here's a version that adds more spacing so that the underline doesn't go through descenders. (The \smash isn't really necessary unless you're doing something that makes the chapter numbers descend too far down. In that case, the underline will go through the number, but it'll still be aligned with the chapter name.)

\usepackage{calc}
\usepackage{tocloft}
\newlength\tocchaplength
\setlength\tocchaplength{\textwidth-\cftchapnumwidth}
\renewcommand\cftchapaftersnumb{\rlap{\underline{\hbox
to\the\tocchaplength{\strut\hfil}}}}
\renewcommand\cftchappresnum{\underlinenumber}
\makeatletter
\def\underlinenumber#1\@cftasnum{\underline{\strut\smash{#1}}}
\makeatother

Oh, that's just where my editor decided to break the line. \hbox to <dimen>{...} creates an hbox with the width set to dimen. In this case, that dimension is \the\tocchaplength. \the expands an internal quantity into a list of tokens. It's described on page 214 of the TeXbook. In this case, it just expands to the value of \tocchaplength. –  TH. Sep 8 '10 at 13:33