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}
theobear
  • 837
  • 6
  • 17

1 Answers1

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.

wipet
  • 74,238
  • #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 http://tex.stackexchange.com/questions/195220/using-macros-in-memoir-chapter-for-a-diary – theobear Aug 13 '14 at 01: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 Aug 13 '14 at 06: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 Aug 13 '14 at 19:49
  • Try \cleaders\hbox to\hsize{\hss hello world\hss}\vskip\baselineskip – wipet Aug 13 '14 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 Aug 16 '14 at 12:55
  • See my comment above... is this possible? Thanks for any help you can give. – theobear Sep 09 '14 at 12:21
  • Yes. it is possible. See the edit of my answer above. – wipet Sep 09 '14 at 13:23
  • sorry to revisit this after so long... but do you think you could add comments to your answer to let me know what I need to edit to change the thickness of the rules... for instance if I wanted a .5pt rule on top of a 1pt rule with .25 pt of white space between. I tried fiddling with the numbers but nothing behaved as I would expect it to. – theobear Jan 11 '15 at 03:20