7

I have a memoir project where the chapters run right after each other with a divider line between chapters. If the chapter falls at the top of a page there shouldn't be a line or a space. Is there any automatic way to do this or is the best way to just use 2 chapter styles and manually switch between them to remove the line and extra space?

\documentclass[12pt,a4paper,article]{memoir} 
\usepackage{lipsum}

%----------depthead
\makeatletter
 \makechapterstyle{mychapter}{%
\renewcommand{\printchaptername}{}% suppress "Chapter" from heading
\renewcommand*{\printchapternum}{}% suppress numbering from heading
\renewcommand*{\chaptitlefont}{\centering}% title formatting
\renewcommand\afterchapternum{%
  \vskip3em\hrulefill\vskip1em}%  
\setlength\beforechapskip{-10pt}% 
\setlength\afterchapskip{30pt}% adjust vertical space after the title
}
\makeatother

\chapterstyle{mychapter}
\begin{document}

\chapter{Sample Chapter 1 should not have line and space}
\lipsum[1]

\chapter{Sample Chapter 2}
\lipsum[1]

\newpage

\chapter{Sample Chapter 3 should not have line and space}
\lipsum[1]


\end{document}

1 Answer 1

7

You cannot test if your macro is processed right now the page is empty because the processing is asynchronous. But when the page is empty, TeX is in special state in which it ignores all vertical spaces (glues). This means you need to make the rule as "vertical space". This is possible by \leaders primitive: it is only a special type of the vertical or horizontal space. Thus, I've replaced your \hrulefill by \leaders.

The second problem of your case is that the \chapterheadstar includes invisible \hrule to the vertical list, so the page isn't empty after this. I redefined this macro to empty.

\documentclass[12pt,a4paper,article]{memoir}
\usepackage{lipsum}

%----------depthead
\makechapterstyle{mychapter}{%
  \renewcommand{\printchaptername}{}% suppress "Chapter" from heading
  \renewcommand*{\printchapternum}{}% suppress numbering from heading
  \renewcommand*{\chaptitlefont}{\centering}% title formatting
  \renewcommand\afterchapternum{%
    \vskip3em \leaders\hrule width\hsize\vskip.4pt\vskip1em}%  
  \setlength\beforechapskip{-10pt}% 
  \setlength\afterchapskip{30pt}% adjust vertical space after the title
  \def\chapterheadstart{}%
}

\chapterstyle{mychapter}
\begin{document}

\chapter{Sample Chapter 1 should not have line and space}
\lipsum[1]

\chapter{Sample Chapter 2}
\lipsum[1]

\newpage

\chapter{Sample Chapter 3 should not have line and space}
\lipsum[1]

\end{document}

Edit: The following code is a reaction to the comment from august 16 below. Define:

\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 by1pt\leaders\vrule height\ruleheight depth-1pt\hfil}}

and use

\vskip1em \cleaders\doublerule{Hello world}\vskip\baselineskip \vskip3em

instead of \vskip3em \leaders\hrule width\hsize\vskip.4pt\vskip1em.

8
  • #wipet, thanks so much. I'm not sure I understand why this works but it does. I wonder if it could be carried even further, and indstead of a simple line I could have a graphic, or a line of text, or something like this tex.stackexchange.com/questions/195220/…
    – theobear
    Commented Aug 13, 2014 at 1:06
  • @theobear You can use \cleaders\hbox{hello world}\vskip\baselineskip instead the \leaders construction. The amount of \vskip space have to be set in order exactly one \hbox can fit in it. If no then you get the \hbox repeated or none \hbox is printed. You can put arbitrary graphics inside the \hbox. You can read appropriate manulas about \leaders.
    – wipet
    Commented Aug 13, 2014 at 6:26
  • I've been messing around with this but can't seem to get the "hello world" to center... from what I understood the \cleaders should be centering in the \hbox, is this because the \hbox is only as big as the text "hello word"
    – theobear
    Commented Aug 13, 2014 at 19:49
  • Try \cleaders\hbox to\hsize{\hss hello world\hss}\vskip\baselineskip
    – wipet
    Commented Aug 13, 2014 at 19:57
  • This works great. I've been trying to understand these commands, wondering if they could be combined. I can do a rule above and below the "hello World" but what I would like to do is something like this "==== Hello world ====" with 2 rules on each side of Hello world with perhaps the top rule thicker than the lower. (I've ordered the advanced TeXbook, hoping that helps me understand these concepts)
    – theobear
    Commented Aug 16, 2014 at 12:55

You must log in to answer this question.

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