9

I would like to make horizontal lines at each side of some text. I tried to use \rule, but I want the total lenght (text + line) to be the same (and user defined) for several line-text, so the line lenght depends on the text lenght. Example :

Tree text-lines

Is there a package or a native LaTeX command to do the trick ?

Dimitri
  • 91

3 Answers3

8
\documentclass{article}
\usepackage{xhfill}
\begin{document}        
\noindent
\xrfill[0.7ex]{1pt}Text\xrfill[0.7ex]{1pt}

\noindent
\xrfill[0.7ex]{1pt}This is a longer text\xrfill[0.7ex]{1pt}
\end{document}

enter image description here

  • It doesn't work for me "Undefined control sequence". I'm using ShareLaTeX, maybe it comes from that (unknown package or something) ? – Dimitri Jan 09 '17 at 20:59
  • Ah, I see. Use \xrfill instead of \xhfill. See edited answer –  Jan 09 '17 at 21:46
5

Here, I take the definition of \hrulefill, and add height/width/depth specification to the \hrule, allowing it to be customized (I call it \myrulefill). Also, by avoiding an earlier suggested \raisebox, the text sits on the baseline.

\documentclass{article}
\def\dotfill#1{\cleaders\hbox to #1{.}\hfill}
\makeatletter
\def\myrulefill{\leavevmode\leaders\hrule height .7ex width 1ex depth -0.6ex\hfill\kern\z@}
\makeatother
\begin{document}
\noindent\myrulefill Short Text\myrulefill\par

\noindent\myrulefill This is Longer Text\myrulefill

\noindent\myrulefill What you see is very long text\myrulefill

\makeatletter
\def\myrulefill{\leavevmode\leaders\hrule height .9ex width 1ex depth -0.2ex\hfill\kern\z@}
\makeatother

\noindent\myrulefill Short Text\myrulefill\par

\noindent\myrulefill This is Longer Text\myrulefill

\noindent\myrulefill What you see is very long text\myrulefill

\end{document}

enter image description here

2
\documentclass[a4paper,11pt]{article}
\usepackage{array}
\usepackage{ragged2e}

%% Define a new command as Steven B. Segletes suggested:
%% Use the length \rlength to have a default width.
\newlength{\rlength}\setlength{\rlength}{5cm}
\newcommand{\ruletext}[2][\rlength]{%
  \noindent%
  \parbox{#1}{%
    \noindent\hrulefill\raisebox{-.3\ht\strutbox}{#2}\hrulefill\par}%
}

\begin{document}

\ruletext{Foo}

\ruletext{Bar Baz}

%% Change the width of the box.
\ruletext[3cm]{Hello World}
\end{document}

Results in

enter image description here

Jan
  • 5,293