[Tex/LaTex] Center text between horizontal lines extending to the margin on either side

rules

I'm wondering how to make something like this:

----------------------------------   Text   -------------------------------------

In words: the text is centered. There is some space between the lines and the text. The lines are aligned vertically with the center of the text and extend to the margin on either side, so that the total length is \textwidth. Here is the idea:

\documentclass{article}
\begin{document}
\centering{\rule[0.5ex]{4.5cm}{2pt} \hspace{0.6cm} Text \hspace{0.6cm} \rule[0.5ex]{4.5cm}{2pt}}
\end{document}

Edit: The length of the lines should change, not of the space.

Best Answer

Extensible rules and customizable height:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter
  \newcommand*\variableheghtrulefill[1][.4\p@]{%
    \leavevmode
    \leaders \hrule \@height #1\relax \hfill
    \null
  }
\makeatother

\begin{document}

\lipsum[1]

\noindent
\variableheghtrulefill[.5ex]\quad Text\quad\variableheghtrulefill[.5ex]

\lipsum[2]

\noindent
\variableheghtrulefill[.5ex]\quad Wider text\quad\variableheghtrulefill[.5ex]

\lipsum[3]

\noindent
\variableheghtrulefill\quad
    No argument gives default height\quad\variableheghtrulefill

\lipsum[4]

\end{document}

Edit: This is the output produced:

Output of the above code

Edit 2: Sorry, I had completely overlooked the requirement concerning the vertical alignment of the rules; as a sort of over-compensation, the following code tries to thoroughly address this issue:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter
  % User command: \genhrulefill
  % This time we make it full-fledged! (:-)
  %
  % SYNOPSIS
  %   - \genhrulefill[<base_height>][<thickness>]
  %       A rule of thickness <thickness> whose lower edge runs
  %       at a height of <base_height> above the baseline of the text.
  %   - \genhrulefill[<base_height>]
  %       A rule of default thickness (0.4pt) whose lower edge runs
  %       at a height of <base_height> above the baseline of the text.
  %   - \genhrulefill
  %       A rule of default thickness (0.4pt) whose lower edge runs
  %       along the baseline of the text.
  %   - \genhrulefill*[<thickness>]
  %       A rule of thickness <thickness> whose axis is aligned along
  %       the math axis.
  %   - \genhrulefill*
  %       A rule of default thickness (0.4pt) whose axis is aligned along
  %       the math axis.
  \newcommand*\genhrulefill{%
    \begingroup % paranoid -- it's because we use \@tempdima, etc.
    \@ifstar \@s@genhrulefill \@n@genhrulefill
  }

  \newcommand*\@n@genhrulefill[1][\z@]{%
    \setlength\@tempdimb{#1}%
    \@x@genhrulefill
  }
  \newcommand*\@x@genhrulefill[1][.4\p@]{%
    \@tempdima \@tempdimb
    \addtolength\@tempdima{#1}%
    \@genhrulefill
  }

  \newcommand*\@s@genhrulefill[1][.4\p@]{%
    \setbox\z@ \hbox{$\global \dimen@i \fontdimen22\textfont\tw@$}%
    \setlength\@tempdima{#1}%
    \@tempdimb \dimexpr \dimen@i-\@tempdima/\tw@
    \advance \@tempdima \@tempdimb
    \@genhrulefill
  }

  \newcommand*\@genhrulefill{%
    \leavevmode
    \leaders \hrule \@height \@tempdima \@depth -\@tempdimb \hfill
    \null
    \endgroup
  }
\makeatother

\begin{document}

\lipsum[2]

\noindent
\genhrulefill[.5ex][4pt]\quad
    All arguments are given\quad\genhrulefill[.5ex][4pt]

\lipsum[2]

\noindent
\genhrulefill[.5ex]\quad Default thickness\quad\genhrulefill[.5ex]

\lipsum[2]

\noindent
\genhrulefill\quad No arguments\quad\genhrulefill

\lipsum[2]

\noindent
\genhrulefill*[2pt]\quad $*$-form, explicit thickness\quad\genhrulefill*[2pt]

\lipsum[2]

\noindent
\genhrulefill*\quad $*$-form, default thickness\quad\genhrulefill*

\lipsum[2]

\end{document}

I think the starred form of the \genhrulefill command should be the preferred one.

Output: enter image description here