6

I'm using minitoc package and I would like to automate the command \minitoc after every chapter.

I'm looking a command to put in my preamble in the spirit of the following (which doesn't work):

\AtBeginChapter[]{\minitoc}

MWE

The game is to remove every \minitoc with only one command in the preamble.

\documentclass{book}
\usepackage{minitoc}
\begin{document}
\dominitoc
\faketableofcontents

\chapter{chapter 1}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 2}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 3}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 4}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 5}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 6}
\minitoc

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\end{document}
5

book sets the chapter heading with \@makechapterhead:

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}

Ideally we'd like to insert \minitoc at the end of this macro, just after \vskip 40\p@, which would be equivalent to your usage of

\chapter{...}
\minitoc

We can use etoolbox to patch this macro, inserting \minitoc after the vertical skip - my <search> tag for the appropriate insertion:

enter image description here

\documentclass{book}
\usepackage{etoolbox,minitoc}
\makeatletter
\patchcmd{\@makechapterhead}% <cmd>
  {40\p@}% <search>
  {40\p@\minitoc}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}
\dominitoc
\faketableofcontents

\chapter{chapter 1}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 2}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 3}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 4}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 5}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\chapter{chapter 6}

\section{section}
\subsection{subsection}
\subsubsection{subsubsection}

\end{document}

If you wish to have the same automated insertion for \chapter*, you can perform the same patch for \@makeschapterhead.

Note that this will not work if you use other sectional packages since the formatting of \@makechapterheading may be different.

  • Nice answer but a little bit complicated. Beamer has \AtBeginSection[]{} command to automatically add something at every section. I'm surprised there is no equivalent command for book class. – ppr Jul 30 '14 at 16:42
  • 2
    @ppr: You're referring to the default classes which provide sufficient yet basic layout components. More modern classes (perhaps memoir and KOMA-script classes) provide hooks into many components of the document structure... book not so much. – Werner Jul 30 '14 at 16:46

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.