Underlining a whole line with a short text in its beginning

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.


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}

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.

Tags:

Spacing