\def\doublerule#1{%
\vbox{
\setbox0=\hbox{ #1 }%
\baselineskip=3pt \lineskiplimit=-\maxdimen
\hbox to\hsize{\ruleheight=1.2pt\doubleruleA\kern\wd0\doubleruleA}%
\hbox to\hsize{\doubleruleA\box0 \doubleruleA}%
}%
}
\def\doubleruleA{{
\advance\ruleheight by2pt
\leaders\vrule height\ruleheight depth-1pt\hfil}}
Let's look at the code line by line.
First a \vbox
is started; in it a temporary box is set with the argument surrounded by spaces.
Then the \baselineskip
is set to 3pt and the setting to \lineskiplimit
means that no two lines will be considered too near to each other, as far as interline computations are concerned.
Then a horizontal box containing \doubleruleA
, a blank space as wide as the text (the chapter title) plus the spaces and then again \doubleruleA
; but the width of the rule is set to 1.2pt (for a heavy rule).
After this a second row is set, with the standard rule width; it's the same as before, but in the middle the text is set using \box0
.
The working of \doubleruleA
is to (locally) increase the rule thickness by 2pt, which will, however, be reduced by 1pt because of the negative depth. This rule will extend as much as possible, so it will fill the boxes used by \doublerule
.
In my opinion this makes quite hard to control the relative placement of the rules. If you want to increase the space between the rules, act on \baselineskip=3pt
.
Here's how I'd do it, with clearer setting of the distances and thicknesses.
\documentclass{memoir}
\usepackage{lipsum}
\newcommand{\upperchapterrule}{1.6pt} % upper rule
\newcommand{\lowerchapterrule}{0.8pt} % lower rule
\newcommand{\chapterrulesep}{2pt} % space between the rules
\newcommand{\chapterruleoffset}{1pt} % distance of the bottom rule from the baseline
\newcommand{\doublerule}[1]{%
\vbox{
\sbox0{ #1 }%
\dimen0=\textwidth
\advance\dimen0 by -\wd0
\divide\dimen0 by 2 % width of the rules
\noindent
\makedoublerule
\usebox{0}%
\makedoublerule
}
}
\newcommand{\makedoublerule}{%
\vbox{
\hrule width \dimen0 height \upperchapterrule
\vskip\chapterrulesep
\hrule height \lowerchapterrule
\vskip\chapterruleoffset
}%
}
\makechapterstyle{mychapter}{%
\renewcommand{\printchaptername}{}% suppress "Chapter" from heading
\renewcommand*{\printchapternum}{}% suppress numbering from heading
\renewcommand*{\chaptitlefont}{\centering}% title formatting
\renewcommand\afterchapternum{%
\vskip1em
\cleaders\doublerule{Hello world}\vskip\baselineskip
\vskip2em
}%
\setlength\beforechapskip{10pt}%
\setlength\afterchapskip{30pt}% adjust vertical space after the title
}
\chapterstyle{mychapter}
\begin{document}
\chapter{Sample Chapter}
\lipsum[1]
\end{document}

The \makedoublerule
command is the main part: after having computed the required width, two rules are drawn. TeX never adds interline glue between rules, so we can be certain that the space between them is what we specify by \vskip\chapterrulesep
. Similarly, the offset from the baseline is how specified in the parameter.
The command \doublerule
just computes the required width and then calls \makedoublerule
, typesets the text (surrounded by spaces) and call \makedoublerule
again.
Here's what I get if I change the parameters into
\newcommand{\upperchapterrule}{0.5pt} % upper rule
\newcommand{\lowerchapterrule}{1pt} % lower rule
\newcommand{\chapterrulesep}{2.5pt} % space between the rules
\newcommand{\chapterruleoffset}{0pt} % distance of the bottom rule from the baseline
