Knowing about \addvspace I wrote my own environment that adds a minimum space before and after it. But using memoir, if a \section precedes it, \addvspace always adds all its space to the space that usually follows a section. MWE:

\documentclass{memoir}

\usepackage{lipsum}

\begin{document}

\section{Sec 1}

\lipsum[1]

\section{Sec 2}

\lipsum[1]

\section{Sec 3}

\section{Sec 4}

\end{document}


You can see that the second section has way too much vspace after it. I had a look at the memoir \@startsection code and found that it does (rightly) use \vskip and \addvspace. After reading this question I suspect it is maybe a \noindent problem since \@afterindent is used. But there must be a trick somewhere that I am missing because Sec 3 and Sec 4 have the proper space. Any ideas of how to fix this?

-
The problem is that the last skip called by \section is \vskip-\parskip which is 0pt plus -1pt and so when \addvspace tests whether the last skip is zero (in which case it always adds its space), the test will be true because the first component of the skip is 0pt (the plus part is discarded). To solve your problem, you can put an \unskip just before your \addvspace. –  Philippe Goutet Oct 7 '12 at 7:07
Hm.. it sounds promising, but it doesn't quite work yet: it breaks with "You can't use \unskip in vertical mode." Is there an equivalent for vertical mode? And a conditional one that only unskips if the \vskip was 0pt? –  MiB Oct 7 '12 at 14:59
@MiB: Sorry, I should have mentioned it, but for \unskip to work, you mustn't leave a blank line before it (so it's more a one time quick fix than a real solution). –  Philippe Goutet Oct 7 '12 at 18:24

Aha! Thanks to the tip from Philippe I found the following solution:

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@xsect}

\@xsect didn't use \addvspace but plain \vskip. Changing that fixes my problem.