3

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:

TOC of a memoir document with custom spacing for chapters

\documentclass{memoir}
\usepackage{etoolbox}

\setlength{\cftbeforechapterskip}{3pt}

\begin{document}
\frontmatter

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

\mainmatter
\pretocmd{\chapter}{\addtocontents{toc}{\addvspace{6pt}}}{}{}

\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}
\pretocmd{\chapter}{\addtocontents{toc}{\addvspace{-6pt}}}{}{}
\chapter{Appendix Two}
\chapter{Appendix Three}

\end{document}
1
  • 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
2

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}{%
  \addtocontents{toc}{\addvspace{6pt}}%
}
\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.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.