1

I saw Gonzalo's beautiful table of contents code in book class and I want to use it in the memoir class. But its output isn't similar to what is in the book class. How to make this code compatible with the memoir class? Thanks for any help!

1 Answer 1

3

Try this.

% memtocprob.tex  SE 594921

%%% This code is essentially a slightly modified version of the code
%%% by Gonzalo Medina in https://tex.stackexchange.com/questions/83184
%\documentclass{book}
\documentclass{memoir}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}
\usepackage{etoolbox}


\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}

\pretocmd{\tableofcontents}{\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]}{}{}
\apptocmd{\tableofcontents}{\end{mdframed}}{}{}
\patchcmd{\tableofcontents}{\contentsname}{\color{ptctitle}\contentsname}{}{}

\titlecontents{section}
  [4em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}

\titlecontents{lsection}
  [5.8em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}

\makeatletter
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \colorbox{ptctitle}{\strut%
        \makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
          \color{white}\bfseries\sffamily#1%
          \nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
      \penalty\@highpenalty
    \endgroup
  \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@chapapp@head}{}   %%% PW added
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother

\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
  \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
    \strut\color{white}\bfseries\sffamily\makebox[5em]{%
      Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}

\begin{document}

\tableofcontents

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

%\stopcontents[chapters]
\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}

\stopcontents[chapters]
\chapter{Chapter Three}
\PartialToC
\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

EDIT

With some difficulty I have extended the code to cater for \subsection and (partially) for \part although I don't know if you use them.

% memtocprob.tex  (extended) SE 594921 

%%% This code is essentially a slightly modified, and extended, version of the code
%%% by Gonzalo Medina in https://tex.stackexchange.com/questions/83184 to cater
%%% for the memoir xlass.
%\documentclass{book}
\documentclass{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}

\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}
\usepackage{etoolbox}

\usepackage{lipsum}
\usepackage{comment}

\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}

\pretocmd{\tableofcontents}{\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]}{}{}
\apptocmd{\tableofcontents}{\end{mdframed}}{}{}
\patchcmd{\tableofcontents}{\contentsname}{\color{ptctitle}\contentsname}{}{}

\titlecontents{section}
  [4em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}


%%%% PW added subsection code
\titlecontents{subsection}
  [7em]{\sffamily}
  {\color{secnum}\contentslabel{3.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}

\titlecontents{lsection}
  [5.8em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}

%%% PW added lsubsection code
\titlecontents{lsubsection}
  [8.8em]{\sffamily}
  {\color{secnum}\contentslabel{3.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}

%\begin{comment}
\makeatletter
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \colorbox{ptctitle}{\strut%
        \makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
          \color{white}\bfseries\sffamily#1%
          \nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
      \penalty\@highpenalty
    \endgroup
  \fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\newcommand{\@chapapp@head}{}   %%% PW added
\providecommand{\@chapapp@head}{}   %%% PW added
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother

%\end{comment}

%%%% PW added part code

%\begin{comment}
\makeatletter
\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \colorbox{ptctitle}{\strut%
        \makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
          \color{white}\bfseries\sffamily#1%
          \nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
      \penalty\@highpenalty
    \endgroup
  \fi}

\makeatother

%\end{comment}

\begin{comment}
\titlecontents{part}
  [4em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}


\newcommand\PartialPartToC{%
\startcontents[parts]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[parts]{l}{1}{\colorbox{ptctitle}{%
  \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
    \strut\color{white}\bfseries\sffamily\makebox[5em]{%
      Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}
\end{comment}

\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
  \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
    \strut\color{white}\bfseries\sffamily\makebox[5em]{%
      Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}

\begin{document}

\tableofcontents

\part{First part}
%\PartialPartToC
%\stopcontents[parts]

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

\section{Section One Four}

%\stopcontents[chapters]
\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}
\subsection{Subsection Two Three One}

\stopcontents[chapters]
\chapter[Three ToC][Three header]%
        {Chapter Three}
\PartialToC
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}
\subsection{Subsection Three Five One}

\lipsum

\end{document}

enter code[![enter image description here][2]][2] here

enter image description here

If you need something more perhaps Gonzalo will be able to help.

4
  • Thank you very much!
    – Mohammadi
    Commented Apr 30, 2021 at 1:33
  • @Mohammadi I have extended my answer. I hope it will all work for you. Commented Apr 30, 2021 at 20:14
  • Yes, my text contains subsections. Thank you for your complete answer.
    – Mohammadi
    Commented May 1, 2021 at 0:07
  • Is it possible for this TOC be referable?
    – Mohammadi
    Commented May 2, 2021 at 10:18

You must log in to answer this question.

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