[Tex/LaTex] “Undefined control sequence” problem in memoir class

chaptersmemoirtable of contents

I use memoir class and want to customize the table-of-contents and chapters style. When I use \renewcommand*\l@chapter[2] I get this error after the second compilation:

Undefined control sequence.
\chapternumberline ... \@tempdima {\@chapapp@head
\@cftbsnum #1\@cftasnum \h...
l.2 ...pter}{\chapternumberline {1}Chapter One}{3}

My code:

\documentclass{memoir} 
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed} 
\usepackage{titletoc}
\usepackage{etoolbox}


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

\makeatletter
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 2.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

\begin{document}

\tableofcontents

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

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

\end{document}

In book class, however, it works with no error!

Best Answer

The definition you have been modifying stems from book.cls, so it is clear it works there. memoir.cls has a completely different way of defining chapters, so you can easily produce a conflict by "injecting" code from book.cls there. Document structure etc. are completely in the resposibility of document classes, so you need to consider which class you are using. Please modify the code from memoir.cls instead.

Here is a starting point. I tried to be as "minimally invasive" as possible, but I'm really no Memoir expert. From the look of it, it'll need some more fiddling, but the definitions are really quite different:

\makeatletter
\setlength{\cftbeforechapterskip}{2.0em \@plus\p@}
\renewcommand{\cftchapterfont}{\bfseries\sffamily}
\renewcommand*{\l@chapapp}[3]{%
  \ifnum \c@tocdepth >\m@ne
    \cftchapterbreak
    \vskip \cftbeforechapterskip
    {%\leftskip \cftchapterindent\relax
     \memRTLleftskip \cftchapterindent\relax
%%%     \rightskip \@tocrmarg
     \memRTLrightskip \@tocrmarg
%%%     \parfillskip -\rightskip
     \parfillskip -\memRTLrightskip
     \parindent \cftchapterindent\relax
     \@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \let\@cftbsnum \cftchapterpresnum
     \let\@cftasnum \cftchapteraftersnum
     \let\@cftasnumb \cftchapteraftersnumb
     \def\@chapapp@head{#3}%
     \settowidth{\@tempdima}{\cftchapterfont\@chapapp@head}%
     \addtolength{\@tempdima}{\cftchapternumwidth}%
%%%     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     \advance\memRTLleftskip \@tempdima \null\nobreak\hskip -\memRTLleftskip
      \colorbox{ptctitle}{\strut%
        \makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
          \color{white}\cftchapterfont#1%
          \nobreak{\cftchapterleader}\nobreak
    \cftchapterformatpnum{#2}%
    \cftchapterafterpnum}}\par\smallskip}%
  \fi}
\makeatother

example output

Related Question