目录中使用 tcolorbox 的控制序列未定义

目录中使用 tcolorbox 的控制序列未定义

当我使用 tcolorbox 在部件的内容行中获取灰色背景时,出现 toc 错误。尽管出现错误,但 pdf 仍可正确创建。

Undefined control sequence. \contentsline {part}{I\hspace {1em}Part1}{3}
Undefined control sequence. \contentsline {part}{II\hspace {1em}Part2}{7}
\documentclass[12pt,a4paper,openright,twoside]{book}
\usepackage[english]{babel}
\usepackage{tcolorbox}

\makeatletter
\renewcommand*\l@part[2]{%
    \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \begin{tcolorbox}[colback=black!10, right=0.5ex, left=0cm, arc=0pt,outer arc=0pt, boxrule=0mm, width=1.01\textwidth]
        {\leavevmode
            \large \bfseries \vbox{\sffamily\bfseries#1}%
            \hb@[email protected]{\hss #2}
        }
    \end{tcolorbox}
    \vspace*{.5ex}
    \nobreak
    \global\@nobreaktrue
    \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
    \fi}
\makeatother


\begin{document}
    \tableofcontents
    \part{Part1}
    \chapter{Chapter1}

    \part{Part2}
    \chapter{Chapter2}
\end{document}

我的日志的文件列表是。

*File List*
    book.cls    2014/09/29 v1.4h Standard LaTeX document class
    bk12.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
   babel.sty    2014/09/25 3.9l The Babel package
 bblopts.cfg    2006/07/31 v1.0 MiKTeX 'babel' configuration
 english.ldf    2012/08/20 v3.3p English support from the babel system
tcolorbox.sty    2015/07/16 version 3.70 text color boxes
     pgf.sty    2013/12/18 v3.0.0 (rcs-revision 1.14)
  pgfrcs.sty    2013/12/20 v3.0.0 (rcs-revision 1.28)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.0 (rcs-revision 1.7)
graphicx.sty    2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2007/01/18 v1.5 graphics configuration of teTeX/TeXLive
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2013/11/30 v3.0.0 (rcs-revision 1.47)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.0 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.0 (rcs-revision 1.1)
verbatim.sty    2014/10/28 v1.5q LaTeX2e package for verbatim enhancements
 environ.sty    2014/05/04 v0.3 A new way to define environments
trimspaces.sty    2009/09/17 v1.1 Trim spaces around a token list
etoolbox.sty    2015/05/04 v2.2 e-TeX tools for LaTeX (JAW)
supp-pdf.mkii
 ***********

答案1

这不是问题的答案(Thomas F. Sturm 已经指出 3.70 中有一个错误tcolorbox),但我想建议newtcolorbox对此\l@part命令使用单独的命令——这样可以更容易地进行更改:

\documentclass[12pt,a4paper,openright,twoside]{book}
\usepackage[english]{babel}
\usepackage[most]{tcolorbox}

\newtcolorbox{lpartbgbox}{%
  colback=yellow, right=0.5ex, left=0cm, arc=0pt,outer arc=0pt, boxrule=0mm, width=1.01\textwidth
}%

\makeatletter
\renewcommand*\l@part[2]{%
    \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \begin{lpartbgbox}
        {\leavevmode
            \large \bfseries \vbox{\sffamily\bfseries#1}%
            \hb@[email protected]{\hss #2}
        }
    \end{lpartbgbox}
    \vspace*{.5ex}
    \nobreak
    \global\@nobreaktrue
    \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
    \fi}
\makeatother


\begin{document}
    \tableofcontents
    \part{Part1}
    \chapter{Chapter1}

    \part{Part2}
    \chapter{Chapter2}
\end{document}

相关内容