我对 的内容有点迷茫titletoc.sty
。我想访问目录条目,即章节标题、节标题等,将它们放入颜色框中。我认为它位于 的某个地方,\def\ttl@tocentry
我希望能够编写类似以下内容的内容:
\titlecontents{chapter}[0pc]
{\colorbox{red}{chapter title}}
{}
{}
{}%
编辑: 梅隆银行:
\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\titlecontents{chapter}%section
[0pc]%left
{}%above
{}%before with label
{}%before without label
{}%filler and page
[]%after
\begin{document}
\tableofcontents
\chapter{chapter title}
\end{document}
答案1
正如手册中解释的那样:“与 \titleformat 一样,最后一个命令可以接受标题作为参数”。因此:
\titlecontents{chapter}[0pc]
{}
{\colorbox{red}} % the second argument is left implicit
{}
{}%
答案2
我会重新定义chapter
\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addcontentsline{toc}{chapter}%
{\colorbox{red}{\protect\numberline{\thechapter}#1}}%
\else
\addcontentsline{toc}{chapter}{\colorbox{red}{#1}}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
\makeatother
\begin{document}
\tableofcontents
\chapter{chapter title}
\end{document}
这是另一个解决方案:
\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\titlecontents{chapter}%section
[0pc]%left
{}%above
{}%before with label
{}%before without label
{}%filler and page
[]%after
\makeatletter
\def\addcontentsline#1#2#3{%
\addtocontents{#1}{\protect\contentsline{#2}{\colorbox{red}{#3}}{\thepage}}}
\makeatother
\begin{document}
\tableofcontents
\chapter{chapter title}
\section{foo}
\end{document}