[Tex/LaTex] Underlining a whole line with a short text in its beginning

spacing

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?

Best Answer

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}