[Tex/LaTex] Modifying mini-toc content

formattingmemoirtable of contentstitletoc

I have adapted the pretty table of contents solution written by Gonzalo Medina with the modification to the "blue box style" that Stephan Lehmke helped me with to design a somewhat consistent look-and-feel for chapter heads and table of contents, as illustrated below. However, I am facing one last problem that has me stomped.

For the MWE example below, the table of contents looks like the following, where I have changed some of the colors for the chapter names.

Table of contents

A "typical" chapter head, that includes a mini table of contents looks as follows:

Correct chapter head

However, a "typical" Appendix head has the Appendix title repeated in the mini table of contents:

Incorrect Appendix head

Note that I can achieve (!) the same look for a chapter if I include the package titletoc at the beginning of the preamble rather than at the end, as illustrated below.
Incorrect chapter head

In other words, in order to include the proper amount of material in the mini table of contents at the beginning of a chapter, I need to include titletoc after I include the code to create the "pretty table of contents"; somehow, titletoc must redefine some macros which, perhaps (?) are rewritten when \appendix is interpreted – or perhaps are not defined in titletoc in the first place.

So, how do I get rid of the extra "Appendix title" in the mini table of contents?

\documentclass[openany]{memoir}

\usepackage{lipsum}   % for testing purpose only
\usepackage{mdframed}   
\usepackage{mathtools}
\usepackage{hyphenat}       % I use it to prevent hyphenation of table of contents items
\usepackage{xcolor}

%%% Do not include titletoc here - do it at the end
 %\usepackage{titletoc}             

%%% colors definitions
\definecolor{MainRed}{rgb}{.6, .1, .1}
\definecolor{GoldDecoration}{RGB}{170, 120, 70}

% Colored section, subsections, as well as
% chapter and section name in the head - included for completeness
\setsecheadstyle{\color{MainRed}\large\bfseries}
\setsubsecheadstyle{\color{MainRed}\bfseries}
\copypagestyle{myheadings}{headings}
\makeevenhead{myheadings}{\thepage}{}{\color{MainRed}\leftmark}
\makeoddhead{myheadings}{\color{MainRed}\rightmark}{}{\thepage}

%% RedBox style -- adapted from BlueBox style, pp:43-44
% http://mirror.csclub.uwaterloo.ca/CTAN/info/latex-samples/MemoirChapStyles/MemoirChapStyles.pdf
% with modifications from 
% https://tex.stackexchange.com/questions/49864/automatically-adjusting-size-of-a-box-based-on-other-content
% and others modifications of my own
\newcommand{\RedBarLength}{3em}

\newsavebox{\ChpNumBox}
\newsavebox{\ChpContBox}
\makeatletter
\newcommand*{\thickhrulefill}{%
  \leavevmode\leaders\hrule height 3\p@ \hfill \kern \z@}
\newcommand*\BuildChpNum[3]{%
  \begin{tabular}[t]{@{}c@{}}
    \makebox[0pt][c]{#1\strut} \\[.5ex]
    \colorbox{MainRed}{%
      \rule[-\RedBarLength-(#3)]{0pt}{0pt}%
      \rule{1ex}{0pt}\color{white}#2\strut
      \rule{1ex}{0pt}}%
  \end{tabular}}
\makechapterstyle{BoxedChapNum}{%
  \renewcommand{\chapnamefont}{\large\scshape}
  \setlength{\beforechapskip}{-30pt}
  \setlength{\midchapskip}{10pt}
  \setlength{\afterchapskip}{30pt}
  \renewcommand{\printchaptername}{}
  \renewcommand{\chapternamenum}{}
  \renewcommand{\printchapternum}{%
    \startcontents[chapters]
    \sbox{\ChpContBox}{%
      \parbox{\linewidth}{%
        \printcontents[chapters]{}{1}{\color{blue}}%
      }}%
    \sbox{\ChpNumBox}{%
      \BuildChpNum{\color{GoldDecoration}\bfseries\chapnamefont\@chapapp}%
      {\chapnumfont\thechapter}%
      {\ht\ChpContBox+\dp\ChpContBox}%
    }}
  \renewcommand{\printchapternonum}{%
    \startcontents[chapters]
    \sbox{\ChpContBox}{%
      \parbox{\linewidth}{%
        %\printcontents[chapters]{}{1}{}%  Useless?
      }}%
    \sbox{\ChpNumBox}{%
      \BuildChpNum{\chapnamefont\vphantom{\@chapapp}}%
      {\chapnumfont\hphantom{\thechapter}}%
      {\ht\ChpContBox+\dp\ChpContBox}%
    }}
  \renewcommand{\afterchapternum}{}
  \renewcommand{\printchaptertitle}[1]{%
    \usebox{\ChpNumBox}\hfill
    \parbox[t]{\hsize-\wd\ChpNumBox-1em}{%
      \raggedright\vspace{\midchapskip}%
        {\color{GoldDecoration}\thickhrulefill}\\[10pt]
      {\chaptitlefont\LARGE\textcolor{MainRed}{\nohyphens{##1}}}\par\vspace*{10pt}
      \renewcommand\cftsectionpagefont{\sffamily\itshape\color{black}}
      \renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{black}}
      \renewcommand\cftsectionfont{\sffamily\color{MainRed}}
      \renewcommand\cftsubsectionfont{\sffamily\color{MainRed}}
      \printcontents[chapters]{}{1}{}%
    }}%
} 
\makeatother 

%%%%%% use the newly defined style
\chapterstyle{BoxedChapNum}

%%% Pretty ToC
%%%%% adapted from https://tex.stackexchange.com/questions/35825/pretty-table-of-contents/35835
% a modification of the leftbar environment defined by the framed package
% will be used to place a vertical colored bar separating the page number and the
% title in chapter entries
\renewenvironment{leftbar}{%
  \def\FrameCommand{\textcolor{GoldDecoration}{\vrule width 2pt depth 6pt}\hspace*{15pt}}%
  \MakeFramed{\advance\hsize-\width\FrameRestore}}%
 {\endMakeFramed}

\makeatletter

% redefinitions for chapter entries
\renewcommand\chapternumberline[1]{\mbox{\small\@chapapp~#1}\par\noindent\Large}
\renewcommand\cftchapterfont{\sffamily}
\cftsetindents{chapter}{0pt}{0em}
\renewcommand\cftchapterpagefont{\Huge\sffamily\bfseries\color{MainRed}}

\newcommand*{\l@mychap}[3]{%
  \def\@chapapp{\color{MainRed}#3}
  \vskip1ex%
  \par\noindent\begin{minipage}{\textwidth}%
  \parbox{4.5em}{%
    \hfill{\cftchapterpagefont#2}%
  }\hspace*{1.5em}%
  \parbox{\dimexpr\textwidth-4.5em-15pt\relax}{%
    \leftbar\cftchapterfont{\color{MainRed}#1}\hspace{1sp}\endleftbar%
  }%
  \end{minipage}\par%
}
\renewcommand*{\l@chapter}[2]{%
  \l@mychap{#1}{#2}{\chaptername}%
}
\renewcommand*{\l@appendix}[2]{%
  \l@mychap{#1}{#2}{\appendixname}%
}

% redefinitions for section entries
\renewcommand\cftsectionfont{\sffamily\color{black}}
\renewcommand\cftsectionpagefont{\sffamily\itshape\color{MainRed}}
\renewcommand\cftsectionleader{\nobreak}
\renewcommand\cftsectiondotsep{\cftnodots}
\renewcommand\cftsectionafterpnum{\hspace*{\fill}}
\setlength\cftsectionnumwidth{12em}
\cftsetindents{section}{6em}{3em}
\renewcommand\cftsectionformatpnum[1]{%
  \hskip1em\hbox to \@pnumwidth{{\cftsectionpagefont #1\hfill}}}

% redefinitions for subsection entries
\renewcommand\cftsubsectionfont{\sffamily\color{black}}
\renewcommand\cftsubsectionpagefont{\sffamily\itshape\color{MainRed}}
\renewcommand\cftsubsectionleader{\nobreak}
\renewcommand\cftsubsectiondotsep{\cftnodots}
\renewcommand\cftsubsectionafterpnum{\hspace*{\fill}}
\setlength\cftsubsectionnumwidth{12em}
\cftsetindents{subsection}{9em}{3em}
\renewcommand\cftsubsectionformatpnum[1]{%
  \hskip1em\hbox to \@pnumwidth{{\cftsubsectionpagefont #1\hfill}}}
\makeatother

%%%% End of Pretty ToC

\settocdepth{subsection}
\setsecnumdepth{subsection}

% include after the change above
\usepackage{titletoc}   

\begin{document}
\nouppercaseheads
\pagestyle{myheadings}
\frontmatter
    \tableofcontents*
    \chapter{Foreword}
    \lipsum[9]

\mainmatter
    \chapter{First Chapter}
        \lipsum[1]
        \section{First section}
        \lipsum[2]
        \subsection{First subsection}
        \lipsum[3]
        \section{Second section}
        \lipsum
    \chapter{Second Chapter}
        \section{First section}

    \appendix
    \chapter{First Appendix}
        \lipsum[7]
        \section{First section}
        \lipsum[8]
        \section{Second section}
\end{document}

Note that I have included a few extra lines (such as the color selection for section heads) as various attempts I have made have resulted in changing other elements; those included are the ones needed for the "complete look and feel".

Best Answer

The problem here is that memoir defines a "new" sectioning structure named appendix which gets its own \l@appendix. This doesn't exist in LaTeX standard classes. A chapter in the appendix is just a chapter.

titletoc now "hooks" into the commands \l@..., saving the old definitions at the time the package is loaded (this is the positional effect you have noticed; it would probably be better for titletoc to save the old definitions \AtBeginDocument, then it wouldn't make a difference when the package is loaded).

You are redefining the \l@appendix, but titletoc doesn't know about \l@appendix and thus doesn't hook into it.

I (naively!) mimicked titletocs chapter hook for appendix like this:

\let\ttl@savel@appendix\l@appendix
\def\l@appendix{\let\ttl@savel@chapter\ttl@savel@appendix\ttl@lselect{chapter}}

(inserted immediately before \makeatother in your MWE)

This should make an appendix behave like a chapter as far as titletoc is concerned. You might make a less naive definition if you need chapters and appendices to behave differently. At least in my test, the redundant appendix heading disappeared.