![使用包 titletoc 是否在目录中的每一章(在主要内容内)前添加“章节”和章节标签?](https://linux22.com/image/362219/%E4%BD%BF%E7%94%A8%E5%8C%85%20titletoc%20%E6%98%AF%E5%90%A6%E5%9C%A8%E7%9B%AE%E5%BD%95%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%80%E7%AB%A0%EF%BC%88%E5%9C%A8%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9%E5%86%85%EF%BC%89%E5%89%8D%E6%B7%BB%E5%8A%A0%E2%80%9C%E7%AB%A0%E8%8A%82%E2%80%9D%E5%92%8C%E7%AB%A0%E8%8A%82%E6%A0%87%E7%AD%BE%EF%BC%9F.png)
\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-tables-of-contents (minitocs) at the beginning of every chapter, part or section.
\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
答案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
如果您不喜欢冒号后的空格,则可以将其删除。