TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a simple way to have text that looks like this?

--- text ---

where the --- fills a certain width? I've tried


but here if "text" is "text_1" in one case and "text_123" in another, then the total width of the text plus rule varies in each case.

share|improve this question

You can use \leaders to specify the style of your rules:

enter image description here

\usepackage{xcolor}% http://ctan.org/pkg/xcolor
    \leaders\hrule height \dimexpr.5ex+.2pt\relax depth \dimexpr -.5ex+.2pt\relax \hfill% Left rule
    \enskip{#3}\enskip% Text (and surrounding spaces)
    \leaders\hrule height \dimexpr.5ex+.2pt\relax depth \dimexpr -.5ex+.2pt\relax \hfill\kern0pt}% Right rule
\myrulefill{10em}{text} \par
\myrulefill{.5\linewidth}{Here is some more text} \par
\myrulefill[\color{olive}]{150pt}{\color{black}SoMe MoRe TeXT}

The provided \myrulefill[<stuff>]{<len>}{<text>} inserts <text> in the middle of a box of length <len> with rules added on either side. <stuff> is an optional argument that can be specified if you want to (say) change the colour.

share|improve this answer
Leaders built with \leaders\hrule will use a single rule, rather than repeating small boxes containing a rule. You can use \leaders\hrule height \dimexpr.5ex+.2pt\relax depth \dimexpr -.5ex+.2pt\relax \hfill to have it centered at mid x-height. – egreg Oct 16 '12 at 8:03

You could just use \noindent\hrulefill text\hrulefill.

Here is a comparison of the outputs:

enter image description here






\noindent\hrulefill text\hrulefill
share|improve this answer
Excellent. This also works too in that if you enclose the hrulefill part in a minipage environment to get the width you want. – crippledlambda Oct 19 '12 at 19:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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