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.

As described in this question I'm using the tocloft package to customize the table of contents.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

How about something like this?

\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
\renewcommand\cftchapafterpnum{\vadjust{\vskip1ex}}
share|improve this answer
    
The first version does work :-) What does the to\the command do? Where is it documented? (Note I've not tried the 2nd one.) –  Martin Sep 8 '10 at 13:08
    
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
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.