4

In another post I was given the code to create a header for my Memoir chapter. I wanted some text between a horizontal double-rule, like so:

========= Hello World======== Chapter Name

(see MWE below) Now I want to understand what is going on so I can modify the double rule with specific line. I've spent some frustrating time playing with the numbers. Nothing behaves as I would expect it to... I wonder if someone could comment the bits of code with their purpose. To start I would like to make the top rule 1 pt with a .5pt gap then the lower rule .5pt... but for future reference I would really like to understand what is going on.

\documentclass{memoir}
\usepackage{lipsum}

\newdimen\ruleheight \ruleheight=.4pt
\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}}    

\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}
2
\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}

enter image description here

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

enter image description here

5
  • That is very useful, thank you. I think I'll need to study it a bit to actually wrap my head around it. – theobear Jan 14 '15 at 23:58
  • one question, if I make the rules too thin I run into the problem where it will repeat the whole header twice... so 2 "=========hello world=========="s on top of each other... what causes this? – theobear Jan 15 '15 at 16:09
  • @theobear Can you tell the values you used? – egreg Jan 15 '15 at 16:12
  • I was having trouble getting the problem to show on the MWE, but if you make the thickline 1pt, the thinline and gap .5pt and use {/tiny Hello world} it happens. – theobear Jan 15 '15 at 19:40
  • @theobear Yes, that would be a problem also with the other solution, because we're telling TeX that it should be cover at least \baselineskip vertical space with copies of the construction. If the material is not very high, you should change \cleaders\doublerule{Hello world}\vskip\baselineskip into \cleaders\doublerule{Hello world}\vskip.5\baselineskip or so. – egreg Jan 15 '15 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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