'LaTeX \rule filling the line

Why does the following command not produce a horizontal rule filling the space until the end of the line?

Hello \rule[0.5em]{\fill}{1pt}

It is my understanding that this should print the text “Hello” followed by a horizontal rule that extends until the end of the line, analogously to the macro \hfill which is effectively equivalent to \hspace\fill. – But in effect, this command just produces the text “Hello”, no rule.

I am aware that the effect can be produced by \hrulefill but it can’t be used here because I want a raised rule and \hrulefill doesn’t work together with \raisebox and I want my rule to hang above the baseline (at best in the middle of the line).



Solution 1:[1]

The horizontal rule of 1pt height and raised by 1.5pt.

Hello \leaders\vrule height 2.5pt depth -1.5pt \hfill \null

Solution 2:[2]

There is a package called ulem which does this

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass{article}
\usepackage[normalem]{ulem}

\begin{document}
normal text \uline{\textit{underline text}\hfill}
\end{document}

which will produce

underline

For your curiosity, the option normalem for package ulem prevents ulem to produce extra underline with \em or \emph.

Solution 3:[3]

You can do this with the command \hrulefill see http://en.wikibooks.org/wiki/LaTeX/Lengths#Fill_the_rest_of_the_line

Solution 4:[4]

% I did it! %

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

\begin{document}
\uline{Some text \hfill\phantom{.}}
\end{document}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey Malistov
Solution 2 zyy
Solution 3 Rob
Solution 4 Viacheslav Nasyrov