问题:
我希望某些章节名称采用粗体样式,而其他章节名称则不采用。
最小工作示例:
\documentclass{book}
\usepackage[titles]{tocloft}
%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold
\begin{document}
\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}
\pagenumbering{Roman}
\setcounter{page}{1}
\addcontentsline{toc}{chapter}{Test Chapter Two}
\end{document}
输出:
期望输出:
获取普通样式目录中的第二个章节名称。
答案1
另一种方法:将等宏的改变写入\cftchap...
目录并使字体变为粗体或者仅仅是\normalfont
。
我已经将其调整为适合页面字体以及领导者......
此宏\enableboldchapterintoc
全局启用此功能,直到\disableboldchapterintoc
被使用。\enableboldchapterintoc
在需要时,使用其他宏等重新启用它。
\documentclass{book}
\usepackage[titles]{tocloft}
\newcommand*{\enableboldchapterintoc}{%
\addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont\protect\bfseries}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont\protect\bfseries}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\bfseries\protect\cftdotfill{\protect\cftsecdotsep}}}% dot leaders in bold
}
\newcommand*{\disableboldchapterintoc}{%
\addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\normalfont\protect\cftdotfill{\protect\cftsecdotsep}}}%
}
%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries\fi}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold
\begin{document}
\enableboldchapterintoc
\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}
\pagenumbering{Roman}
\setcounter{page}{1}
\disableboldchapterintoc
\addcontentsline{toc}{chapter}{Test Chapter Two}
\enableboldchapterintoc
\chapter{Foo}
\end{document}
答案2
您在评论中指出,普通样式的章节是用 来添加的\addcontentsline
。因为您不想像普通的 那样格式化它们chapter
,所以您可以创建一种新的 TOC 条目,而不是重复使用chapter
。为此,您可以主要复制的tocloft
定义。在这里我使用了名称:chapter
titletoc.sty
chapterNB
\makeatletter
\newcommand*{\l@chapterNB}[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip \cftbeforechapNBskip
{\leftskip \cftchapNBindent\relax
\rightskip \@tocrmarg
\parfillskip -\rightskip
\parindent \cftchapNBindent\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima \cftchapNBnumwidth\relax
\let\@cftbsnum \cftchapNBpresnum
\let\@cftasnum \cftchapNBaftersnum
\let\@cftasnumb \cftchapNBaftersnumb
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{\cftchapNBfont #1}\nobreak
\cftchapNBfillnum{#2}}%
\fi}%
\newlength{\cftbeforechapNBskip}
\setlength{\cftbeforechapNBskip}{\cftbeforechapskip}
\newlength{\cftchapNBindent}
\setlength{\cftchapNBindent}{\cftchapindent}
\newlength{\cftchapNBnumwidth}
\setlength{\cftchapNBnumwidth}{\cftchapnumwidth}
\let\cftchapNBfont\cftchapfont
\let\cftchapNBpresnum\cftchappresnum
\let\cftchapNBaftersnum\cftchapaftersnum
\let\cftchapNBaftersnumb\cftchapaftersnumb
\newcommand{\cftchapNBleader}{\bfseries\cftdotfill{\cftchapNBdotsep}}
\let\cftchapNBdotsep\cftchapdotsep
\let\cftchapNBpagefont\cftchappagefont
\let\cftchapNBafterpnum\cftchapafterpnum
\newcommand{\cftchapNBfillnum}[1]{%
{\cftchapNBleader}\nobreak
\makebox[\@pnumwidth][\cftpnumalign]{\cftchapNBpagefont #1}\cftchapNBafterpnum\par
}
\makeatletter
此后,您可以更改新的格式chapterNB
:
\renewcommand{\cftchapNBfont}{\normalfont}
\renewcommand{\cftchapNBpagefont}{\normalfont}
\renewcommand{\cftchapNBleader}{\cftdotfill{\cftsecdotsep}}
要将普通(非粗体)样式的“章节”添加到目录中,您可以使用:
\addcontentsline{toc}{chapterNB}{Test Chapter Two}
综合起来,结果如下
\documentclass{book}
\usepackage[titles]{tocloft}
%: ----------------------- Table of contents ------------------------
\renewcommand{\contentsname}{Table of contents}
\renewcommand{\cftchapfont}{\normalfont\bfseries}% titles in bold
\renewcommand{\cftchappagefont}{\normalfont\bfseries}% page numbers in bold
\renewcommand{\cftdotsep}{1}
\renewcommand{\cftchapleader}{\bfseries\cftdotfill{\cftsecdotsep}}% dot leaders in bold
\makeatletter
\newcommand*{\l@chapterNB}[2]{%
\ifnum \c@tocdepth >\m@ne
\addpenalty{-\@highpenalty}%
\vskip \cftbeforechapNBskip
{\leftskip \cftchapNBindent\relax
\rightskip \@tocrmarg
\parfillskip -\rightskip
\parindent \cftchapNBindent\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima \cftchapNBnumwidth\relax
\let\@cftbsnum \cftchapNBpresnum
\let\@cftasnum \cftchapNBaftersnum
\let\@cftasnumb \cftchapNBaftersnumb
\advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
{\cftchapNBfont #1}\nobreak
\cftchapNBfillnum{#2}}%
\fi}%
\newlength{\cftbeforechapNBskip}
\setlength{\cftbeforechapNBskip}{\cftbeforechapskip}
\newlength{\cftchapNBindent}
\setlength{\cftchapNBindent}{\cftchapindent}
\newlength{\cftchapNBnumwidth}
\setlength{\cftchapNBnumwidth}{\cftchapnumwidth}
\let\cftchapNBfont\cftchapfont
\let\cftchapNBpresnum\cftchappresnum
\let\cftchapNBaftersnum\cftchapaftersnum
\let\cftchapNBaftersnumb\cftchapaftersnumb
\newcommand{\cftchapNBleader}{\bfseries\cftdotfill{\cftchapNBdotsep}}
\let\cftchapNBdotsep\cftchapdotsep
\let\cftchapNBpagefont\cftchappagefont
\let\cftchapNBafterpnum\cftchapafterpnum
\newcommand{\cftchapNBfillnum}[1]{%
{\cftchapNBleader}\nobreak
\makebox[\@pnumwidth][\cftpnumalign]{\cftchapNBpagefont #1}\cftchapNBafterpnum\par
}
\makeatletter
\renewcommand{\cftchapNBfont}{\normalfont}
\renewcommand{\cftchapNBpagefont}{\normalfont}
\renewcommand{\cftchapNBleader}{\cftdotfill{\cftsecdotsep}}% dot leaders in bold
\begin{document}
\tableofcontents
\chapter{Test Chapter One}
\section{Test Section One}
\section{Test Section One}
\subsection{Test Subsection A}
\subsection{Test Subsection B}
\pagenumbering{Roman}
\setcounter{page}{1}
\addcontentsline{toc}{chapterNB}{Test Chapter Two}
\end{document}