15

I'd like to have a horizontal line extending from the end of the text on a line to the right margin. So far I've found \hrulefill and \leaders\hrule\hfill, but these draw a line that's flush with the baseline of my text — how can I get it to be vertically centered?

7
\documentclass{article}
\usepackage{xhfill}
\begin{document}
Here is some text\xfill{1pt} \par
Here is some text\xfill{8pt} \par
Here is some text\xhrulefill{blue}{2pt}\par
Here is some text\xhrulefill{cyan}{1ex}\par
Here is some text\xrfill[0ex]{1ex}[red] \par
\end{document}​

enter image description here

  • 6
    Is the command \xfill defined in hxfill? It seems not. – egreg Oct 24 '11 at 21:51
12

You can define a command similar to \hrulefill, but changing the height and depth of the rule used:

\documentclass{article}

\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}

\begin{document}

\noindent text\hrulefill

\noindent text\Vhrulefill

\end{document}

enter image description here

10
\documentclass[a4paper]{article}

\newcommand\filltoend{\leavevmode{\unskip
  \leaders\hrule height.5ex depth\dimexpr-.5ex+0.4pt\hfill\hbox{}%
  \parfillskip=0pt\endgraf}}
\newenvironment{filllines}
  {\par\let\par\filltoend}
  {\par}

\usepackage{lipsum}

\begin{document}

A paragraph outside the environment can be ended with the
command \verb|\filltoend| like this one.\filltoend

\begin{filllines}
In this environment all paragraphs will be ended with a line.

As it's shown here.
\end{filllines}
\end{document}

enter image description here

The advantage of this approach with respect to the other proposed solutions is that it has no issues when the paragraph happens to end exactly at the right margin. However it won't work, for TeXnical reasons, with list environments inside filllines (list environments redefine \par in terms of the primitive commands, not taking into account its present meaning), but \filltoend will work.

If a rule is needed no matter what, change the specification

  \leaders\hrule height.5ex depth\dimexpr-.5ex+0.4pt\hfill

into

  \leaders\hrule height.5ex depth\dimexpr-.5ex+0.4pt\hskip1em plus 1fill

which will draw a rule at least 1em wide.

  • Did you opt for \endgraf to deal with redefinitions of \par within list environments? – Jonathan Komar Feb 8 '17 at 20:19
  • @macmadness86 Most likely so – egreg Feb 8 '17 at 20:25
6

The following \centerline[<height>][<thickness>] produces a rule at height <height> (default is 0.5ex) and thickness <thickness> (default is 0.4pt). The horizontal alignment is not entirely optimal compared to a traditional \hrulefill, but this is perhaps not an issue.

enter image description here

\documentclass{article}
\usepackage{linegoal}% http://ctan.org/pkg/linegoal
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\centerrule}{O{0.5ex} O{0.4pt}}{%
  \leavevmode\rule[#1]{\linegoal}{#2}}%
}
\begin{document}
Here is some text\centerrule \par
Here is some text\centerrule[0pt] \par
Here is some text\centerrule[0.5ex][1pt] \par
Here is some text\centerrule[0pt][2pt]
\end{document}​

The interface is provided by the xparse package, while the end-of-line estimation is done using \linegoal (from the linegoal package). It requires at least 2 compiles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.