我想将目录中的章节编号和章节标题都放入 \tcolorbox 中。虽然这对于编号来说很容易,但我不知道如何访问标题的名称以将其放入框中。有没有简单的解决方案?
\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{titletoc}
\titlecontents{section}%section
[0pc]%left
{}%above
{\begin{tcolorbox}[colback=green,width=1cm,sharp corners,nobeforeafter]
\thecontentslabel
\end{tcolorbox}}
{}
{}
[]
\begin{document}
\tableofcontents
\section{section title}
\end{document}
答案1
我找到了以下解决方案:
\newcommand\tocbox[1]%
{\begin{tcolorbox}[colframe=white, colback=blue!15,nobeforeafter,sharp corners,left=1.2cm,top=0.1cm,height=0.7cm,width=15.77cm,enhanced,boxrule=0pt]
\bfseries\color{blue}#1\hfill\contentspage
\end{tcolorbox}%
}
\titlecontents{section}[0pc]
{\vspace*{0.2cm}\begin{tcolorbox}[colback=blue,colframe=white,width=0.7cm,height=0.7cm,left=0.13cm,top=0.07cm,sharp corners,extrude right by=0.1cm,nobeforeafter,enhanced,boxrule=0pt]
\Large\bfseries\color{white}\thecontentslabel
\end{tcolorbox}}
{\tocbox} % the second argument is left implicit
{}
{}