Center text between double horizontal rules

I'd like to do something like this:

====================   Text   ====================


Where '===' represents two horizontal lines separated by 2pt. In other words, I'd like to center some text between double horizontal lines. I have found a similar question here and attempted to adapt the following solution by egreg:

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


And combine it with the solution given here for making double rules:

\hrule width \hsize \kern 1mm \hrule width \hsize


However, no matter what I try, the second \hrule doesn't seem to be recognized properly and I either get "height depth" printed on my document or nothing at all.

\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 \kern2pt \hrule height \dimen0 depth \dimen2\hfill
\leaders\hrule height \dimen0 depth \dimen2 \kern2pt \hrule height \dimen0 depth \dimen2\hfill\hfill
}\par\nopagebreak\vspace{\topsep}
}

\begin{document}

\lipsum[2]

\textbetweenrules{Text}

\lipsum[3]

\end{document}


You can use the same idea, but leaders don't work here, so I prepare a vbox with the double rules after computing their width.

Somewhat arbitrarily, the space between the rules is four times their thickness.

\documentclass{article}

\newcommand{\textbetweendoublerules}[2][.4pt]{%
\noindent\makebox[\textwidth]{%
\dimen0=.5\dimexpr\ht0+#1\relax
\dimen2=-.5\dimexpr\ht0-#1\relax
\dimen4=.5\dimexpr\textwidth-\wd0\relax
\setbox2=\vbox to \ht0{%
\vss
\hrule width \dimen4 height #1
\kern 4\dimexpr#1\relax
\hrule width \dimen4 height #1
\vss
}%
\copy2 \box0 \box2
}

\begin{document}

\textbetweendoublerules{abc}

\end{document}


• Worked like a charm. I only changed '4\dimexpr#1' to '2pt' to be consistent with other double rules in my document. Thanks for also helping me understand what all the dimension commands stand for. – Matheus Leão Jan 1 '18 at 23:12

I would go a different route and place the text in a coloured box over the lines. This way you don't have to worry about the centering.

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}

\newcommand{\textbetweenrules}[1]{%
\begin{center}
\setlength{\fboxsep}{0.4cm}
\rule{\textwidth}{1pt}\\[-0.3cm]
\rule{\textwidth}{1pt}\\[-0.65cm]
\colorbox{white}{#1}
\end{center}
}

\begin{document}

\lipsum[1]

\textbetweenrules{Text}

\lipsum[2]

\textbetweenrules{longer text text text}

\lipsum[3]

\end{document}


• Are you sure there are 2pt between the lines? – TeXnician Jan 1 '18 at 21:24
• @TeXnician No. I just used dummy numbers. I will mention this in the answer. – user36296 Jan 1 '18 at 21:25

It might be overkill to load TikZ for that, but if you load it anyway, you might consider this code:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}

\newcommand{\RuleText}[1]{%
\centerline{\tikz{\draw[double,line width=1pt,double distance between line
centers=3pt] (0,0)--(\linewidth,0) node[midway,fill=white]{#1};}}}

\begin{document}

\lipsum[1]

\RuleText{koala bear}

\lipsum[2]

\RuleText{marmot}

\lipsum[3]

\end{document}


Here's one version which is quite "hacky", but at least works for short text. You would have to play around with the vskips to get 2pt distance.

\documentclass{article}
\usepackage{lipsum}
\newcommand\textbetweenlines[1]{%
\par
\vskip-.2\baselineskip