# In memoir environment, the chapter heading placement gets messed up if a too big figure gets placed on next page

Using the memoir class, it appears that if I have a figure that is close enough to chapter heading, and big enough that it has to be placed on the next page, the chapter heading placement gets messed up. For example, I expect to see:

Here is a standalone repro of the effect. I've replaced the image with a box so that no external files are included. Note that if the box dimensions are halfed, allowing the figure and the text and the chapter heading to all fit in one page, then the chapter headings end up in the expected positions.

\documentclass[12pt,openany]{memoir}
\usepackage{caption}

\newcommand{\imageCentered}[4]{%
\begin{center}%
\captionsetup{type=figure}%
%   \includegraphics[totalheight=#4\textheight]{#1}%
\rule{350pt}{300pt} \par
\caption{#2}%
\label{#3}%
\end{center}%
}

\begin{document}
\chapterstyle{ell}
\chapter{foo}
\section{foo.}

aa'aa aaaaaaa aaaaaa aaa aa aaa aaaaaaa aa aaaaaaaaaaa aa a aaaa.  aaaa aa a aaaaaa aaaaaaaaa aaaaaaaaaa, aaa aa aaaaaa aaa aa aaaaaaaaaaa aa aaaaaaaaaa aaa aaaaaa aaaa aaa aaa aaaaaa aaaa aaa aaaaaaaa aaaaaaaaaaaaa.  a aaaaaaaaaaaa aaaaaaaaa aa aaaa aaaaaaaaaa aaaaa aa aa aaaa.  a aaaaa aaaaaaaaaaa aa aa aaaaaa aaaa aaaaaaaa aaaaaaa aa aa, aa, aaa aa aa aaaaaaaaaaa aa aaaaaa (aaa{aaa:aaaaaa:aaaaaaaaa1}), aaaaa aaa aaaaaaaa aaaaaaaa aaa aaaaaaa aa aa', aa', aaa aa'.

\imageCentered{figures/strainFig1}{caption text}{fig:1}{0.5}

\end{document}


Any idea how this occurs, and how to prevent it?

EDIT: For reference, the reason that I'd not used \begin{figure} ... \end{figure} is because this doesn't work within an exercise environment (see: How to use figure inside a exercise environment?). So once I knew the solution to that problem, I just changed all my figure commands once and for all to avoid that issue.

-
Let images float. –  egreg May 14 '12 at 17:43

You can remove the variable glue from the chapter head which solves the immediate problem, but as others have said, such a big image should probably float or be used with manual forced page breaks in any case.

This boxes the head which probably removes more stretch/shrink than absolutely necessary but keeps changes to a minimum.

\documentclass[12pt,openany]{memoir}
\usepackage{caption}

\makechapterstyle{ell}{%
\chapterstyle{default}
\renewcommand*{\chapnumfont}{\normalfont\HUGE\sffamily}
\renewcommand*{\chaptitlefont}{\normalfont\huge\sffamily}
\settowidth{\chapindent}{\chapnumfont 111}
\vspace*{\beforechapskip}%
\hrulefill
\smash{\rule{0.4pt}{15mm}}
\renewcommand*{\printchaptername}{}
\renewcommand*{\printchapternum}{%
\hfill
\raisebox{10mm}[0pt][0pt]{\chapnumfont \thechapter}%
\hspace*{1em}
\renewcommand*{\printchaptertitle}[1]{%
\vskip\onelineskip
\raggedleft {\chaptitlefont ##1}\par\nobreak}}

\newcommand{\imageCentered}[4]{%
\begin{center}%
\captionsetup{type=figure}%
%   \includegraphics[totalheight=#4\textheight]{#1}%
\rule{350pt}{300pt} \par
\caption{#2}%
\label{#3}%
\end{center}%
}

\begin{document}
\chapterstyle{ell}
\chapter{foo}
\section{foo.}

aa'aa aaaaaaa aaaaaa aaa aa aaa aaaaaaa aa aaaaaaaaaaa aa a aaaa.  aaaa aa a aaaaaa aaaaaaaaa aaaaaaaaaa, aaa aa aaaaaa aaa aa aaaaaaaaaaa aa aaaaaaaaaa aaa aaaaaa aaaa aaa aaa aaaaaa aaaa aaa aaaaaaaa aaaaaaaaaaaaa.  a aaaaaaaaaaaa aaaaaaaaa aa aaaa aaaaaaaaaa aaaaa aa aa aaaa.  a aaaaa aaaaaaaaaaa aa aa aaaaaa aaaa aaaaaaaa aaaaaaa aa aa, aa, aaa aa aa aaaaaaaaaaa aa aaaaaa (aaa{aaa:aaaaaa:aaaaaaaaa1}), aaaaa aaa aaaaaaaa aaaaaaaa aaa aaaaaaa aa aa', aa', aaa aa'.

\imageCentered{figures/strainFig1}{caption text}{fig:1}{0.5}

\end{document}

-

The vertical spaces inserted between most document elements are stretchable. This also holds for the ell chapter style construction. If other document elements don't fit within the page boundary, the stretchable lengths try to compensate (either shrinking or expanding) for this problem. You may have noticed some underfull \vbox warnings, the result of the problematic first page.

If you allow your figures to float, they will move without affecting the spacing that much, perhaps to another page where the contents is more flexible. So, use \begin{figure}[t] ... \end{figure} instead of \begin{center} ... \end{center}.

You may also attempt using \raggedbottom to avoid this, but floating is a more-preferred alternative.

-
To be honest, the vertical spaces for the chapter header should not be able to stretch. But \chapterstyle{ell} uses the adjustwidth environment (wrongly, IMO), so the stretching comes in a bit unexpected. However, a 300pt high image is best floated, or placed manually with extreme care. –  egreg May 14 '12 at 18:00
edited my question to note why I wasn't using begin{figure} (a change made based on a response you provided to a different problem, applied more globally than perhaps I should have). –  Peeter Joot May 14 '12 at 18:29