5 tcolorbox 使用 minted 库排版代码, 实现目录报错 \tcblistof 悬赏已过期

发布于 2025-10-20 22:06:22
  • 操作系统 Windows11
  • 发行版 TeX Live 2025
  • 编辑器 TeXstudio
  • 编译命令 XeLaTeX
  • mwe? 如下
\documentclass[UTF8]{ctexbook}
%代码排版
\usepackage{tcolorbox}
\tcbuselibrary{minted,breakable}
\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,
    before={\begin{listing}[htbp!]},
    after={\label{#3}\end{listing}},
    #1,
}
\begin{document}
\tableofcontents
\tcblistof[\chapter*]{abMATLABlist}{MATLAB 代码}
%\tcblistof[\section*]{abMATLABlist}{MATLAB 代码}
%\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}

%\end{abMATLAB}
\chapter{AAA}
\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}

\end{abMATLAB}
\end{document}

可供测试的一段 MATLAB 代码

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
  • 需求

    • 使用 tcolorbox 中的 minted 库以及 \tcblistof (这可能不是好的方案) 实现 minted 宏包类 \listoflistings 排版代码目录的效果 (或许有没有进一步修改目录样式的方法)
    • 盒子的样式已经在代码中实现

image.png

image.png

  • 报错信息如下:

(可能的错误before={\begin{listing}[htbp!]},after={\label{#3}\end{listing}} , 有意思的是在下面的代码中

\tcblistof[\chapter*]{abMATLABlist}{MATLAB 代码}
%\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}

%\end{abMATLAB}
\chapter{AAA}
\begin{abMATLAB}{拉普拉斯展开计算行列式}{abMATLABlist:DetLaplace}

\end{abMATLAB}

注释掉下面一段代码 abMATLAB 环境, 然后取消上面的 abMATLAB 的注释却能正常编译)

! Extra }, or forgotten \endgroup.
\@endfloatbox ...pagefalse \outer@nobreak \egroup 
                                                  \color@endbox 
l.37 \end{abMATLAB}
                   
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Extra }, or forgotten \endgroup.
\color@endbox ->\color@endgroup \egroup 
                                        
l.37 \end{abMATLAB}
                   
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Undefined control sequence.
\endlisting ...floatbox \global \setbox \@currbox 
                                                  \float@makebox \columnwidt...
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Missing number, treated as zero.
<to be read again> 
                   \vbox 
l.37 \end{abMATLAB}
                   
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Undefined control sequence.
\float@makebox ...ze =#1 \@parboxrestore \@fs@pre 
                                                  \@fs@iftopcapt \ifvoid \@f...
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\float@makebox ...restore \@fs@pre \@fs@iftopcapt 
                                                  \ifvoid \@floatcapt \else ...
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\float@makebox ... \@fs@mid \fi \unvbox \@currbox 
                                                  \else \unvbox \@currbox \i...
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Extra \else.
\float@makebox ...mid \fi \unvbox \@currbox \else 
                                                  \unvbox \@currbox \ifvoid ...
l.37 \end{abMATLAB}
                   
I'm ignoring this; it doesn't match any \if.

! Missing number, treated as zero.
<to be read again> 
                   \unvbox 
l.37 \end{abMATLAB}
                   
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Undefined control sequence.
\float@makebox ...currbox \else \unvbox \@currbox 
                                                  \ifvoid \@floatcapt \else ...
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Extra \fi.
\float@makebox ...mid \unvbox \@floatcapt \fi \fi 
                                                  \par \@fs@post \vskip \z@ }
l.37 \end{abMATLAB}
                   
I'm ignoring this; it doesn't match any \if.

! Missing number, treated as zero.
<to be read again> 
                   \scan_stop: 
l.37 \end{abMATLAB}
                   
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Undefined control sequence.
\float@makebox ...loatcapt \fi \fi \par \@fs@post 
                                                  \vskip \z@ }
l.37 \end{abMATLAB}
                   
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.


! LaTeX Error: \begin{tcolorbox} on input line 37 ended by \end{listing}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.37 \end{abMATLAB}
                   
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.


! LaTeX Error: \begin{abMATLAB} on input line 35 ended by \end{tcb@drawing}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.37 \end{abMATLAB}
                   
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Missing number, treated as zero.
<to be read again> 
                   \tcb@footnote@cnt@i 
l.37 \end{abMATLAB}
                   
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)


! LaTeX Error: \begin{document} ended by \end{tcolorbox}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.37 \end{abMATLAB}
                   
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Extra \endgroup.
\end  ...end#1\endcsname \@checkend {#1}\endgroup 
                                                  \UseHook {env/#1/after}\if...
l.37 \end{abMATLAB}
                   
Things are pretty mixed up, but I think the worst is over.


! LaTeX Error: \begin{document} ended by \end{abMATLAB}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.37 \end{abMATLAB}
                   
Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Extra \endgroup.
\end  ...end#1\endcsname \@checkend {#1}\endgroup 
                                                  \UseHook {env/#1/after}\if...
l.37 \end{abMATLAB}
                   
Things are pretty mixed up, but I think the worst is over.



[5] (./test.aux)
 ***********
LaTeX2e <2025-06-01> patch level 1
L3 programming layer <2022/07/14>
 ***********


LaTeX Font Warning: Some font shapes were not available, defaults substituted.

 ) 
Here is how much of TeX's memory you used:
 25260 strings out of 468389
 635955 string characters out of 5444297
 1101832 words of memory out of 5000000
 53359 multiletter control sequences out of 15000+600000
 630527 words of font info for 76 fonts, out of 8000000 for 9000
 1348 hyphenation exceptions out of 8191
 116i,6n,121p,514b,1091s stack positions out of 10000i,1000n,20000p,200000b,200000s

Output written on test.pdf (5 pages).

查看更多

关注者
0
被浏览
382
2 个回答
雾月
雾月 2025-10-21
这家伙很懒,什么也没写!

listing 环境就是一个浮动体,tcolorbox 自己就有对应的功能,没必要再套一个浮动环境,tcolorbox 也不能用 beforeafter 随便套一个环境。\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,不去管就行了。

Absinthe
Absinthe 2025-10-21
这家伙很懒,什么也没写!

感谢 @u70550 Explorer 老师在 QQ 群的关注和关照,

  1. 问题的解决方案如下

    • 删掉 \tcbuselibrary 中的 breakable
      (该库与 before={\begin{listing}[htbp!]}, after={\label{#3}\end{listing}}, 冲突, 原意是为了实现引用盒子, 由于没有仔细阅读 tcolorbox 盒子, 导致该错误. 引用可以使用宏包中的 label 可选项, 然后 \ref 即可, 在最后的完整示例中给出了一个例子)
    • 修改 abMATLAB 环境的定义
  2. 基本功能已经实现 (更多的功能应当 仔细 阅读 tcolorbox 宏包手册 5.2 Lists of tcolorboxes,17 Libraries listings ,listingsutf8 , andminted 部分)

(tcolorbox 手册的获取 在线: 浏览器搜素 ctan tcolorbox 等等; 本地发行版在终端运行命令 texdoc tcolorbox)

* 使用 `tcolorbox` 宏包以及 `minted` 库定义 `MATLAB` 的代码盒子
* 实现 `Lists of tcolorboxes`: `\tcblistof[\chapter*]{abMATLABlist}{MATLAB 代码}`
* 引用盒子: 上面已经提到
  1. 完整的示例如下:

image.png
image.png

\documentclass[UTF8]{ctexbook}

\usepackage{geometry}
\geometry{
    a4paper,
    top=25.4mm,
    bottom=25.4mm,
    left=20mm, right=20mm,
    headheight=2.17cm,
    headsep=4mm,
    footskip=12mm
}

%\usepackage{nameref}

%代码排版
\usepackage{tcolorbox}
\tcbuselibrary{minted}
\newtcblisting[auto counter,number within=chapter,list inside=abMATLABlist]{abMATLAB}[2][]{%
    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,
%    nameref={\textcolor{blue}{代码列表}},
    minted language=matlab,
    list text=#2, %设置目录项的文本项
%    list entry=\protect\numberline{\thetcbcounter} #2, %在这里等价于 list text=#2,; 可以进一步修改目录条目的样式如, 计数器等等
    #1,
}

\usepackage{hyperref}
\hypersetup{colorlinks,linkcolor=blue}
\begin{document}

\tableofcontents

\tcblistof[\chapter*]{abMATLABlist}{MATLAB 代码}
\tcblistof[\section*]{abMATLABlist}{MATLAB 代码}
\begin{abMATLAB}{拉普拉斯展开计算行列式}

\end{abMATLAB}
\chapter{AAA}
%{abMATLABlist:DetLaplace}
\begin{abMATLAB}[label={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}
引用代码列表 \ref{abMATLABlist:DetLaplace}. 这也是引用代码列表 \nameref{abMATLABlist:DetLaplace} .

\chapter{BBB}

\begin{abMATLAB}{拉普拉斯展开计算行列式}
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}


\chapter{CCC}
\begin{abMATLAB}{拉普拉斯展开计算行列式}
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}
引用代码 \ref{abMATLABlist:DetLaplace}
\end{document}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览