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
被浏览
66
0 个回答
暂无答案,快来添加答案吧

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览