1
\documentclass[openany]{book}
\raggedbottom       %reduce underfull \vbox

\usepackage{titlesec, titletoc}
\titlelabel{\thetitle\quad}
\titleformat{\part}[frame]{\normalfont\huge\bfseries}{\partname\ \thepart }{20pt}{\Huge\centering}
\titlecontents{part}%
[0pt]{\sffamily\bfseries\huge\protect\addvspace{25pt}\titlerule\addvspace{1.5ex}}
{}{\partname~}
{\hfill\contentspage}%
[\addvspace{0.7ex}\titlerule\addvspace{10pt}]%

 %This is my trial!!!!!I also try to change \chaptername to \chaptertitlename
\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{}{\chaptername~}
{\hfill\contentspage}%
[\addvspace{0.3ex}\titlerule\addvspace{1.5ex}]%




%customize page layout
\usepackage[a4paper, left=3cm, right=3cm]{geometry}

%color
\usepackage[dvipsnames]{xcolor}

%page headers and footers   left,right,odd,even
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}}
\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}}
\fancyhead[LE,RO]{\thepage}



% add mini-ta­bles-of-con­tents (mini­tocs) at the be­gin­ning of ev­ery chap­ter, part or sec­tion.
\usepackage{minitoc}
\setcounter{minitocdepth}{1} 
\renewcommand\tightmtcfalse


%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak} 
\makeatother

\title{\Huge C++ Notes}

\begin{document}
%initialize minitoc
\dominitoc 


\frontmatter
\maketitle
\chapter*{Preface}
\tableofcontents

\mainmatter

\chapter{Getting Started}
\minitoc 
    \section{Writing a Simple C++ Program}
        \subsection{Compiling and Executing Our Program}
    \section{A First Look at Input/Output}
    \section{A Word about Comments}
    \section{Flow of Control}
        \subsection{The while Statement}
        \subsection{The for Statement}
        \subsection{Reading an Unknown Number of Inputs}
        \subsection{The if Statement}
    \section{Introducing Classes}   
        \subsection{The Sales\_item Class}
        \subsection{A First Look at Member Functions}
    \section{The Bookstore Program}
    \section*{Chapter Summary} \markright{Chapter Summary} \addcontentsline{toc}{section}{Chapter Summary}
    \section*{Defined Terms} \markright{Defined Terms} \addcontentsline{toc}{section}{Defined Terms}

\part{The Basics}
\chapter{Variables and Basic Types}
\minitoc 
    \section{Primitive Built-in Types}
        \subsection{Arithmetic Types}
        \subsection{Type Conversions}
        \subsection{Literals}

\appendix
\chapter{The Library}


\backmatter
\chapter{Index}
\chapter{New features in C++11}

\end{document}

I want to add "Chapter" and chapter label before each chapter(within mainmatter) in table of contents, such as "Chapter 1 Getting Started","Chapter 2 Variables and Basic Types". However, my code always adds "Chapter" before chapter within backmatter.

enter image description here

2

Use

\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptername\ \thecontentslabel\enskip}{}% <- changed
{\hfill\contentspage}%
[\addvspace{0.3ex}\titlerule\addvspace{1.5ex}]%

to insert "Chapter" and the chapter label for numbered chapters:

enter image description here

Note that there are warnings regarding the incompatibility of minitoc and titlesec.

2

This did the trick for me: using your MWE, in the snippet which you entilted 'your trial', add {\chaptername\ \thecontentslabel:\quad} in front of {\chaptername} (where before you had {}).

\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptername\ \thecontentslabel:\quad}{\chaptername~}
{\hfill\contentspage}%
[\addvspace{0.3ex}\titlerule\addvspace{1.5ex}]%

This leads to:

enter image description here

Of course, you can change some details, e.g. remove \quad in case you don't like the space after the colon etc.

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.