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 would like to underline a whole line with a single word in the beginning of the line. I tried

\underline{\textsc{HOMEWORK}\hrulefill}}

but this underlines only the word ``HOMEWORK'', leaving the rest of line not underlined.

Any other suggestion?

share|improve this question
    
Please go over your list of questions and accept an answer for them if they got fully answered. At the moment you have a "0% accept rate" which doesn't look good to other users. –  Martin Scharrer May 8 '12 at 10:24
add comment

2 Answers

up vote 3 down vote accepted

It might be easiest to create a macro to do this which measures the length of the text and then produces an \hspace for the remaining space:

\documentclass{article}
\usepackage{calc}

\newlength{\remaining}
\newcommand{\titleline}[1]{%
\setlength{\remaining}{\textwidth-\widthof{\textsc{#1}}}
\noindent\underline{\textsc{#1}\hspace*{\remaining}}\par}
\begin{document}

\titleline{homework}

\end{document}
share|improve this answer
    
Thanks! this looks good but an anomaly occurs: for some reason the underline is one character longer then the width of the page. Can you figure how to cure this? –  user1999 May 21 '11 at 21:35
    
It's not for me. Are you using this exact document? (Try adding \usepackage[showframe]{geometry} and this will show you that the line is the same length as the textwidth.) –  Alan Munn May 21 '11 at 21:48
add comment

The macro \hrulefill tells TeX to fill with a rule the available space, but in your case there's none: \underline{...} creates a box as wide as the text inside.

What you probably want is "HOMEWORK" on a line by itself followed by a horizontal rule across the whole page. Then

\par\hbox{HOMEWORK\strut}\hrule

should do what you need.

If you think that the spacing is excessive, don't. :) Underlining is frowned upon in typography. However, you can play with spacing by trying

\par\hbox{\scshape homework}\kern1pt\hrule\kern3pt

giving different values until you're satisfied.

share|improve this answer
    
Thank you for your suggestion! This gives the underline the correct length, but it puts it too much beneath the word. The solution of Alan seems to be more appropriate from the aesthetical aspect. –  user1999 May 21 '11 at 21:30
    
@user1999: see edited message –  egreg May 21 '11 at 21:45
    
You could raise the line if you like by adding a \vspace{-2pt} before the \hrule. –  Alan Munn May 21 '11 at 21:49
    
@egreg Is there any substantive difference between adding \vspace and using \kern? –  Alan Munn May 21 '11 at 21:51
    
@Alan: TeX doesn't break pages at kerns, if they are not immediately followed by glue. So with the (edited) code, there won't be a legal breakpoint between the header, the rule and the text following them, since TeX doesn't add interline glue before and after horizontal rules. The "excess" space in the original code is produced by the \strut in the \hbox. –  egreg May 21 '11 at 22:11
show 1 more 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.