使用包 titletoc 是否在目录中的每一章(在主要内容内)前添加“章节”和章节标签?

使用包 titletoc 是否在目录中的每一章(在主要内容内)前添加“章节”和章节标签?
\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}

我想在目录中的每个章节(在主要内容中)前添加“章节”和章节标签,例如“第 1 章入门”、“第 2 章变量和基本类型”。但是,我的代码总是在附录中的章节前添加“章节”。

在此处输入图片描述

答案1

使用

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

插入“章节”和编号章节的章节标签:

在此处输入图片描述

请注意,有关于minitoc和不兼容的警告titlesec

答案2

这对我来说很有效:使用你的 MWE,在你命名为“你的试用”的代码片段中,{\chaptername\ \thecontentslabel:\quad}在前面添加{\chaptername}(在你之前的位置{})。

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

这导致:

在此处输入图片描述

当然,您可以更改一些细节,例如,\quad如果您不喜欢冒号后的空格,则可以将其删除。

相关内容