tags:

views:

599

answers:

2

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).

+4  A: 

I don't have a satisfying answer as to why the command you presented doesn't work, but I can offer an effective workaround. Put

% Raised Rule Command:
%  Arg 1 (Optional) - How high to raise the rule
%  Arg 2            - Thickness of the rule
\newcommand{\raisedrule}[2][0em]{\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill}

into your document's preface, and then you can accomplish what you were hoping to with:

Hello \raisedrule[0.5em]{1pt}
RTBarnard
Unfortunately, I don’t understand how this works since the `\hfill` is *outside* the `\hbox` but it works like a charm, thanks.
Konrad Rudolph
That part I can explain. Leaders are the general case of glue in TeX; usually, glue fills space with nothing. But leaders fill space with whatever you want. The general form of this is `\leaders{box or rule}\hskip{glue}`, Since `\hfill` is a special kind of infinitely strechable glue, the leaders command has the two things it need: an element to repeat and glue to tell it how far to extend the repetition.
RTBarnard
+3  A: 

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

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