答案1
您可能会收到错误,因为您插入的内容\addtocontents
已扩展,这可能会破坏tcolorbox
。您需要将所需内容放入强大的命令中,这样就不必太担心扩展。
\fbox
我将使用和来举例\parbox
,但它应该以相同的方式工作tcolorbox
:
\documentclass{book}
\usepackage{blindtext}% To create a sample document
\begin{document}
% Here you define the robust command, which I named \TOCbox
% This command has to be defined before the call to \tableofcontents
\DeclareRobustCommand\TOCbox{%
\fbox{%
\parbox{\dimexpr\textwidth-2\fboxsep}{%
\Huge Example text
}
}%
\par
}
% Then you insert it in the .toc file
\addtocontents{toc}{\TOCbox}
\tableofcontents
% dummy document
\blinddocument
\end{document}
得出的结果为:
如果你不想Contents
以及您可以使用的框\unskip
(如果您使用其中一个标准类,否则它可能无法工作):
\DeclareRobustCommand\TOCbox{%
\unskip% <- Removes the space after "Contents"
\vspace{\baselineskip}% A vertical space to unclutter things
\fbox{%
\parbox{\dimexpr\textwidth-2\fboxsep}{%
\Huge Example text
}
}%
\par
}