listing 环境就是一个浮动体,tcolorbox 自己就有对应的功能,没必要再套一个浮动环境,tcolorbox 也不能用 before 和 after 随便套一个环境。\label 也有对应的选项。
如果只要给代码添加目录,无需使用浮动体。要改目录条目的样式只要重定义 \l@tcolorbox 即可(\renewcommand\l@tcolorbox[2]{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}),见 tcolorbox 文档 5.2 节 /tcb/new/list type 选项的说明。
\documentclass[UTF8]{ctexbook}
%代码排版
\usepackage{tcolorbox}
\tcbuselibrary{minted,breakable,hooks}
\newtcblisting[auto counter,list inside=abMATLABlist]{abMATLAB}[3][]{%
coltitle=black,
colbacktitle=white,
colupper=black,
colback=white,
boxrule=0pt,
toprule=0.08em,
titlerule=0.05em,
bottomrule=0.08em,
fonttitle=\bfseries,
title={\heiti 代码列表 \thetcbcounter: #2},
listing only,
sharp corners=all,
minted language=matlab,
label={#3}, % \label %% <--
float=htbp!, % 浮动环境 %% <--,如果代码不要浮动,删掉这行
#1,
}
\begin{document}
\tableofcontents
\tcblistof[\chapter*]{abMATLABlist}{MATLAB 代码}
%\tcblistof[\section*]{abMATLABlist}{MATLAB 代码}
%\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}
%\end{abMATLAB}
\chapter{AAA}
\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}
function d=DetLaplace(A)
% DETLAPLACE 使用 Lapace 展开计算行列式
% d = DetLaplace(A); 计算矩阵 A 的行列式 (determinant)
% 对第一行 (row) 使用 Laplace 展开 (expansion)
n = length(A);
if n==1
d=A(1,1);
else
d=0; v=1;
for j=1:n
M1j = [A(2:n,1:j-1) A(2:n,j+1:n)];
d = d + v*A(1,j)*DetLaplace(M1j);
v = -v;
end
end
\end{abMATLAB}
\end{document}至于 \chapter 前面那个例子可以编译,可能是某个奇怪的 bug,不去管就行了。








问 tcolorbox 使用 minted 库排版代码, 实现目录报错 \tcblistof