我尝试使用在 LaTeX 文档中插入一些 Python 代码,mdframed
minted
但是编译需要很长时间(所以我必须中止,CPU 一直达到 100%)。
在标准输出中,我得到了成千上万行:
Overfull \vbox (2655.50832pt too high) detected at line 3665
Overfull \vbox (2656.50832pt too high) detected at line 3665
以下是我尝试的方法如果代码不适合一页,minted 会截断代码:
\documentclass[a4paper,12pt,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish]{babel}
\usepackage[T1]{fontenc}
(...)
% pretend to already have loaded float
\makeatletter
\@namedef{[email protected]}{3000/12/31}
\makeatother
\usepackage[newfloat]{minted}
\usepackage{floatrow}
\usepackage{mdframed}
%%%%%%
\begin{document}
\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
leftline=false, rightline=false, backgroundcolor=yellow!20!white]
\begin{minted}[
frame=lines,
framesep=2mm,
baselinestretch=1.0,
bgcolor=lightgray,
fontsize=\footnotesize,
linenos
]
{python}
reaaaaally long code with hundreds of line here (it must end up on multiple pages)
\end{minted}
\end{mdframed}
\end{document}
只需一小段代码,它就能很好地发挥作用。
我也尝试过:
\begin{mdframed}[linecolor=white, topline=false, bottomline=false,
leftline=false, rightline=false]
\inputminted[
frame=lines,
framesep=2mm,
baselinestretch=1.0,
bgcolor=lightgray,
fontsize=\footnotesize,
linenos
]
{python}
{/path/to/script.py}
\end{minted}
\end{mdframed}
我也有同样的行为。
更新:
这是日志文件的一个有用的摘录:
(...)
Package mdframed Info: Not enough space on this page on input line 89.
Overfull \vbox (170.2141pt too high) detected at line 89
[]
Package mdframed Info: Box was splittet wrong
starting loop to iterate the splitting point
(mdframed) on input line 89.
Overfull \vbox (171.2141pt too high) detected at line 89
[]
Overfull \vbox (172.2141pt too high) detected at line 89
[]
Overfull \vbox (173.2141pt too high) detected at line 89
[]
(...)
答案1
mdframed
并且tcolorbox
应该能够完全一样地应用。但是,如果你的内容很长,使用breakable=unlimited
fortcolorbox
可能会解决您的问题:
\documentclass[a4paper,12pt,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish]{babel}
\usepackage[T1]{fontenc}
%(...)
\usepackage[skins,minted,breakable]{tcolorbox}
%%%%%%
\begin{document}
\begin{tcblisting}{
enhanced,
listing only,
minted options={
%frame=lines,
%framesep=2mm,
baselinestretch=1.0,
%bgcolor=lightgray,
fontsize=\footnotesize,
linenos,
breaklines,
},
minted language=python,
colback=yellow!20!white,
boxrule=0pt,
boxsep=0pt,
frame hidden,
borderline horizontal={0.4pt}{0pt}{black},
breakable=unlimited,
}
reaaaaally long code with hundreds of line here (it must end up on multiple pages)
\end{tcblisting}
\end{document}
只是为了解释一下发生了什么:
mdframed
并且tcolorbox
都使用基本的 TeX\vsplit
命令来分割内容框。内容的最大总高度约为 65536pt,应该足够通常内容(约90页)。breakable=unlimited
是一种避免测量框高度的扩展。这里,编译器内存是限制因素,但大约 300 页应该可以工作(如果使用扩展内存,甚至可以更多)。
更新:
或者,要输入文件,您可以使用以下命令:
\documentclass[a4paper,12pt,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish]{babel}
\usepackage[T1]{fontenc}
%(...)
\usepackage[skins,minted,breakable]{tcolorbox}
%%%%%%
\begin{document}
\tcbinputlisting{
listing file=\jobname.tex,% <------- file name
enhanced,
listing only,
minted options={
%frame=lines,
%framesep=2mm,
baselinestretch=1.0,
%bgcolor=lightgray,
fontsize=\footnotesize,
linenos,
breaklines,
},
minted language=python,
colback=yellow!20!white,
boxrule=0pt,
boxsep=0pt,
frame hidden,
borderline horizontal={0.4pt}{0pt}{black},
breakable=unlimited,
}
\end{document}
请注意,
bgcolor=lightgray
必须将其删除以避免限制minted
内容!如果您确实需要灰色内饰,可以通过tcolorbox
选项来实现,但我认为现在看起来更好了。