15

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

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

5 Answers5

17

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
  • 4,374
  • 27
  • 27
  • Unfortunately, I don’t understand how this works since the `\hfill` is *outside* the `\hbox` but it works like a charm, thanks. – Konrad Rudolph Mar 31 '10 at 15:36
  • 4
    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 Mar 31 '10 at 15:46
  • With some code from http://tex.stackexchange.com/a/17130/13450 this also works to surround text to both sides: `\newcommand{\raisedrule}[2][0em]{\leavevmode\leaders\hbox{\rule[#1]{1pt}{#2}}\hfill\kern0pt}`. – Christian Jul 08 '12 at 21:08
7

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

Hello \leaders\vrule height 2.5pt depth -1.5pt \hfill \null
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
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.

zyy
  • 1,271
  • 15
  • 25
-1

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

Rob
  • 23
  • 1
  • `\hrulefill` will indeed produce a rule filling the rest of the line. The question asked for a raised line, but I think a solution using `\hrulefill` would better a better starting point than using glue as suggested in the accepted answer. – Andrew Bate Feb 11 '21 at 02:42
-1

% I did it! %

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

\begin{document}
\uline{Some text \hfill\phantom{.}}
\end{document}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 12:16