2

I try to insert some Python code in a LaTeX document using an mdframed minted but it take really really ... really long to compile (so long I must abort, CPU goes to 100% all the time).

In the stdout, I got thousands of thousands of lines:

Overfull \vbox (2655.50832pt too high) detected at line 3665
Overfull \vbox (2656.50832pt too high) detected at line 3665

Here's how I tried based on minted truncates the code if it doesn't fit into one page :

\documentclass[a4paper,12pt,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish]{babel}
\usepackage[T1]{fontenc}
(...)
% pretend to already have loaded float
\makeatletter 
\@namedef{ver@float.sty}{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}

With small snippets of code it works like a charm.

I also tried:

\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}

I got the same behaviour.


Update:

Here's a useful (?) extract of the log file:

(...)

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
 []

(...)
  • With a fake long Python script I get ! Dimension too large. Probably tcolorbox is better instead of mdframed. – egreg Jul 19 '17 at 9:48
  • Thanks for your suggestion. Anyway, tcolorbox doesn't seem to split the code on several pages. It's just like printing only the minted block without embedding it into "something else" like an mdframed or tcolorbox. – s.k Jul 19 '17 at 10:05
  • 1
    You probably have to tell the box (mdframed or tcolorbox) that the box is allowed to break. I think it is the default in mdframed, but not i tcolorbox. – daleif Jul 19 '17 at 10:07
  • I found the breakable option here : tex.stackexchange.com/questions/187741/very-large-tcolorbox which doesn't seem to be recognized by my side : ! Package pgfkeys Error: I do not know the key '/tcb/breakable', to which you passed 'unlimited', and I'm going to ignore it. – s.k Jul 19 '17 at 10:44
  • 1
    Maybe, you forgot to set the breakable package option? I added an answer which works for me. – Thomas F. Sturm Jul 19 '17 at 10:50
6

mdframed and tcolorbox should be able to be applied quite the same. But, if your content is really long, using breakable=unlimited for tcolorbox may solve your problem:

\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}

Just to explain what happens:

  • mdframed and tcolorbox both use basic TeX \vsplit commands to split a content box. The maximal total height is about 65536pt for the content which should be enough for usual content (ca. 90 pages).

  • breakable=unlimited is an extension which avoids measuring the box height. Here, the compiler memory is the limiting force, but about 300 pages should work (and even more with extended memory).

Update:

Alternatively, to input a file, you may use the following:

\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}

Note that bgcolor=lightgray has to be removed to avoid boxing the minted content! If you really need a gray interior, that could be done by tcolorbox options, but I think it looks better now.

  • Thanks for your answer, is there a way to use an \inputminted statement instead rather than copying all the code within the *.tex document? A first try with copy-pasting let me with an empty yellowish box taking the place of a full page only. – s.k Jul 19 '17 at 10:56
  • 1
    @s.k Yes, with \tcbinputlisting. I updated my answer. Also note that bgcolor=lightgray is to be removed to make the content breakable. – Thomas F. Sturm Jul 19 '17 at 11:10
  • By the way I'm currently relying on the listings package instead if minted to have some clean output: \lstinputlisting[columns=fullflexible,caption={script.py},language=Python] – s.k Jul 19 '17 at 11:13
  • @s.k The same thing could be done for listings as well with different options (but you asked for minted). – Thomas F. Sturm Jul 19 '17 at 11:16
  • 1
    @s.k When minted's breaklines option is used, it will only break lines at spaces. You need to add breakanywhere, or specify characters for breakbefore or breakafter, to control breaking when there are no spaces. – G. Poore Jul 19 '17 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.