TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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

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

This is an old question but the answers proved unsatisfactory for me and I spent countless hours tearing my hair out. What I ended up arriving at was \uline from the ulem package. This allows the inclusion of a \hfill to stretch the line to the end of the page, which allows the underline to start from somewhere other than the left edge.

\documentclass{article}
\usepackage[normalem]{ulem}

\hspace{10em}\uline{\textsc{HOMEWORK}\hfill}

The \hspace is just there to show it doesn't have to start at the left margin.

share|improve this answer

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.