LaTeX \rule filling the line
Asked Answered
K

5

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

Knell answered 31/3, 2010 at 13:42 Comment(0)
P
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}
Photoreceptor answered 31/3, 2010 at 14:53 Comment(3)
Unfortunately, I don’t understand how this works since the \hfill is outside the \hbox but it works like a charm, thanks.Knell
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.Photoreceptor
With some code from 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}.Puppy
S
7

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

Hello \leaders\vrule height 2.5pt depth -1.5pt \hfill \null
Snob answered 31/3, 2010 at 14:58 Comment(0)
P
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.

Pattypatulous answered 20/12, 2019 at 20:36 Comment(0)
E
-1

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

Epigenous answered 13/4, 2015 at 4:23 Comment(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.Manado
P
-1

% I did it! %

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

\begin{document}
\uline{Some text \hfill\phantom{.}}
\end{document}
Pergolesi answered 26/4, 2022 at 9:15 Comment(1)
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.Tugboat

© 2022 - 2024 — McMap. All rights reserved.