# Underlining a whole line with a short text in its beginning

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?

-

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}

-
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

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.

-
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