6

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.

1
  • \centering does not take an {} argument May 7, 2015 at 15:16

3 Answers 3

8

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

3
  • I appreciate the addition. Too bad I can't upvote again. May 8, 2015 at 16:52
  • I'm very impressed with how well this works and how easy it is to use. May 8, 2015 at 23:36
  • How to add color to the line?
    – Leon
    Jun 22, 2022 at 21:10
1

This has adjustable thickness and aligns vertically the rule taking into account only the ascenders in the text (not the descenders, because they are visually irrelevant as far as reading is concerned).

\documentclass{article}
\usepackage{lipsum}

\newcommand{\textbetweenrules}[2][.4pt]{%
  \par\vspace{\topsep}
  \noindent\makebox[\textwidth]{%
    \sbox0{#2}%
    \dimen0=.5\dimexpr\ht0+#1\relax
    \dimen2=-.5\dimexpr\ht0-#1\relax
    \leaders\hrule height \dimen0 depth \dimen2\hfill
    \quad #2\quad
    \leaders\hrule height \dimen0 depth \dimen2\hfill
  }\par\nopagebreak\vspace{\topsep}
}

\begin{document}

\lipsum[2]

\textbetweenrules{Text}

\lipsum[3]

\textbetweenrules[3pt]{\Large aapaa}

\lipsum[3]

\end{document}

Adjust the spacings. This won't allow a page break after the text with rules.

enter image description here

1
  • I still prefer alignment along the math axis. May I also suggest considering a \penalty\@beginparpenalty before the upper \vspace{\topsep}? Or may be not…
    – GuM
    May 8, 2015 at 23:46
0
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\noindent\lipsum[1]

\noindent\rule[0.5ex]{4.5cm}{2pt}\hfill{}Text\hfill \rule[0.5ex]{4.5cm}{2pt}

% fixed space around, but the width and depth of rule is not changeable
\noindent\hrulefill\hspace{.6cm}Text\hspace{.6cm}\hrulefill
\end{document}

enter image description here

1
  • @RandyRanderson, almost there.
    – Sigur
    May 7, 2015 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .