4

What I do have, is grouped frontmatter entries (chapter-like) in ToC, and the rest of the chapters have larger vertical space.
What I want is to change the font style of the grouped part to, say, normalfont or italics while keeping the rest of the entries unchanged. How do I do this?

(In the future I might change entries of the chapters 1 to 3 to small caps or to something other format...)

\documentclass{memoir}
\usepackage{etoolbox}

\setlength{\cftbeforesectionskip}{4pt}

\begin{document}

\tableofcontents

\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
\chapter*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}

\pretocmd{\chapter}{\addtocontents{toc}{\addvspace{16pt}}}{}{}

\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}

\end{document}

A sample

This is continuation to the question Change vertical space of chapters temporarly in Table of Contents. Package etoolbox is for the grouping the front matter entries.

1
  • Found a solution, kind of... Add \renewcommand*{\cftchapterfont}{} to the preamble and change the \pretocmd... to \pretocmd{\chapter}{\addtocontents{toc}{\addvspace{16pt}\bfseries}}{}{}. Only problem is that if the grouped entries should be italics and the rest not, it won't work.
    – TKO
    Commented Nov 19, 2012 at 19:12

2 Answers 2

4

Declare a new boolean switch (say, specialtoc). Set it (inside the .toc file) to true at the start of the document body and to false at the start of the main chapters. Redefine \cftchapterfont and \cftchapterpagefont (which are responsible to format ToC chapter entries) so that their meaning depends on the state of specialtoc.

\documentclass{memoir}

\setlength{\cftbeforesectionskip}{4pt}

\usepackage{etoolbox}

\newbool{specialtoc}

\renewcommand{\cftchapterfont}{%
  \ifbool{specialtoc}{%
    \itshape
  }{%
    \bfseries
  }%
}

\renewcommand{\cftchapterpagefont}{%
  \ifbool{specialtoc}{%
    \itshape
  }{%
    \bfseries
  }%
}

\begin{document}

\addtocontents{toc}{\protect\booltrue{specialtoc}}

\tableofcontents

\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
\chapter*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}

\addtocontents{toc}{\protect\boolfalse{specialtoc}}

\pretocmd{\chapter}{\addtocontents{toc}{\addvspace{16pt}}}{}{}

\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}

\end{document}

enter image description here

3
  • Nice! I can see that you can use specialtoc multiple times (or add other boolean switches) to change the appearance of, say, bibliography or appendices. As a side note, makes me wonder, why there is no options in memoir or a dedicated package to change the appearance of ToC to look like this - it does look better than the original!
    – TKO
    Commented Nov 20, 2012 at 6:58
  • 2
    I usually use the toc hooks to insert macro hooks into the toc file itself, and then in the preamble define what those hooks should do, I'm not a fan of changing \chapter. See \cftinserthook and \cftinsertcode, the later is for defining what a hook does. Adding things like locksteps solution would make memoir a lot more difficult to understand than it already is.
    – daleif
    Commented Nov 20, 2012 at 9:08
  • @daleif Because this question was continuation to other question, I used etoolbox. Your point makes this "nuisance" unnecessary, thank you!
    – TKO
    Commented Nov 20, 2012 at 10:02
3

Altough lockstep gave already very nice answer to the problem, I'll add minimal solution based on daleif's comment on \cftinserthook and \cftinsertcode. This way the etoolbox-package is not needed at all.

\documentclass{memoir}

\setlength{\cftbeforechapterskip}{4pt}
\renewcommand*{\cftchapterfont}{\itshape}

\cftinsertcode{SPECIALTOC}{%
  \renewcommand*{\cftchapterfont}{\bfseries}%
  \setlength{\cftbeforechapterskip}{16pt}%
}
\begin{document}

\tableofcontents

\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
\chapter*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}

\cftinserthook{toc}{SPECIALTOC}

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

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

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

\end{document}

The output is similar to lokcstep's solution.

1
  • Beautiful. Can we get this using book.cls?
    – Khaaba
    Commented Apr 9, 2018 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .