[Tex/LaTex] How to fill a line from the left

horizontal alignmentrules

I am fiddling with various ways of filling a line with a vertically centered line and experimenting with the answers here: https://stackoverflow.com/questions/2553392/latex-rule-filling-the-line and here: Vertically centered horizontal rule filling the rest of a line?. Now, I would like the line to fill the beginning (left) of the line – not the rest. It seems "fill" commands do not do anything unless the line contains "something" before the fill part. How can I do this so that I can fill the line with a rule like this:

———————————————– Text

Where the text is flush right?

Best Answer

What you place before can be empty. The following example is taken from Vertically centered horizontal rule filling the rest of a line?:

enter image description here

\documentclass{article}
\usepackage{xhfill}% http://ctan.org/pkg/xhfill
\newcommand{\xfill}[2][1ex]{{%
  \dimen0=#2\advance\dimen0 by #1
  \leaders\hrule height \dimen0 depth -#1\hfill%
}}
\setlength{\parindent}{0pt}
\begin{document}
\mbox{}\xfill{1pt} Here is some text \par
\mbox{}\xfill{8pt} Here is some text \par
\mbox{}\xhrulefill{blue}{2pt} Here is some text \par
\mbox{}\xhrulefill{cyan}{1ex} Here is some text \par
\mbox{}\xrfill[0ex]{1ex}[red]{} Here is some text \par
\end{document}​

\mbox{} sets an empty box and initiates a paragraph (similar to what TeX's \leavevmode does). Also, to avoid paragraph indentation, I've set \parindent to 0pt. However, you can control this individually using \noindent.