# Bind vertical spacing change in ToC to \*matter commands

This is a spin-off question to Change vertical space of chapters temporarly in Table of Contents. Like in the question's answer, I managed to achieve custom vertical spacing of chapters before, during and after the main content. In contrast to that question, I'm using memoir's \frontmatter, \mainmatter and \backmatter commands to separate these document parts semantically.

Now: Is it possible to move the vertical spacing commands for chapters to the preamble somehow, e.g. by using the \*matter commands as a trigger? I really would like to keep the custom formatting commands out of the document body as far as possible.

MWE with desired output, but with markup sprinkled throughout the document:

\documentclass{memoir}
\usepackage{etoolbox}

\setlength{\cftbeforechapterskip}{3pt}

\begin{document}
\frontmatter

\tableofcontents
\chapter{Foreword}
\chapter{Abstract}

\mainmatter

\chapter{Chapter One}
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}

\chapter{Chapter Two}
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}

\chapter{Chapter Three}
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}

\backmatter
\appendix
\chapter{Appendix One}
\chapter{Appendix Two}
\chapter{Appendix Three}

\end{document}

• i'm sure it should be possible. my approach would be to add those definitions to the definitions of the \*matter* commands. using a "patch" technique is what i'd look at, but i'm not familiar enough with memoir to suggest code. – barbara beeton Jun 21 '15 at 12:25

Use etoolbox for adding what you need.

\documentclass{memoir}
\usepackage{etoolbox}

\setlength{\cftbeforechapterskip}{3pt}

\pretocmd{\chapter}{\ojdospacing}{}{}
\apptocmd{\mainmatter}{\let\ojdospacing\ojdospacingon}{}{}
\apptocmd{\backmatter}{\ojdospacing\let\ojdospacing\relax}{}{}
\newrobustcmd{\ojdospacingon}{%
}
\let\ojdospacing\relax % initialize

\begin{document}
\frontmatter

\tableofcontents
\chapter{Foreword}
\chapter{Abstract}

\mainmatter

\chapter{Chapter One}
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}

\chapter{Chapter Two}
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}

\chapter{Chapter Three}
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}

\backmatter
\appendix
\chapter{Appendix One}
\chapter{Appendix Two}
\chapter{Appendix Three}

\end{document}


When \backmatter is issued, \ojdospacing is executed one last time, then it is reverted to mean \relax.