# “Undefined control sequence” problem in memoir class?

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.
\@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
\vskip 2.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode
\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!

-
You have invisible characters U+200E "left to right mark" in your lines 13, 32, 33. If you remove these, your MWE compiles just fine with either book or memoir. (I found these characters by copying the entire line for which I was given an error and pasting it into the "UTF8 String" field on www.isthisthingon.org/unicode, which gave me a list of all characters used.) Which editor are you using? These invisible character questions have been popping up fairly frequently recently, perhaps there's a bug in the editor. (Also, voting to close as too localized, let us know if it's not working) – doncherry Nov 26 '12 at 6:20
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. – Stephan Lehmke Nov 26 '12 at 6:21
Because my language is persian, that "left to right mark" characters is there. How should i do for modifying this definition of chapter? – mohammad azadi Nov 26 '12 at 6:41
@doncherry The right-to-left marks are giving errors when processing the MWE with pdflatex (I wonder whether it works with XeTeX???), but that's not the error message reported by the OP. That one really stems from transplanting book.cls code to memoir. – Stephan Lehmke Nov 26 '12 at 9:39
@StephanLehmke I failed to see that the error messages only pop up when you compile for the second time, i.e. when the ToC is actually set. I'll edit the question post to remove the U+200E's and vote to reopen. Sorry about the closing, Mohammad! – doncherry Nov 26 '12 at 16:36

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

Thank you. I place \def\@chapapp@head{} befor \setlength\@tempdima{1.5em}‎ in my code and it works fine. – mohammad azadi Nov 26 '12 at 10:17