11

minted works well when the code snippet is short, or fit into one page. The problem occurs when I pasted a code snippet with several hundred lines. Sometimes it processed but the output was truncated; other times it produced a bunch of errors. And if I break it into pages, the amount of characters in each page matter, i.e. it could yield a blank page after the \end{minted}. For example:

....
\begin{minted}[bgcolor=bg,mathescape]{c++}
code here
\end{minted}
... -> a blank page sometimes appear
\begin{minted}[bgcolor=bg,mathescape]{c++}
code here
\end{minted}
....

Has anyone encountered this awkward behavior with minted package? Any idea or suggestion would be greatly appreciated. Thanks.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
roxrook
  • 9,907
  • FYI, this behaviour is known, it’s a bug, and I have no idea how to fix it. – Konrad Rudolph Oct 09 '11 at 16:11
  • @KonradRudolph: Please turn your comment into an answer. – lockstep Dec 03 '11 at 22:14
  • 1
    I have recently converted a lot of source files to pdf using minted package with no problems and errors, each resulting pdf files several pages long. For each source file, a .tex file was generated, and the source code was inserted with \inputminted[opts...]{c++}{file}. – guillem Nov 26 '12 at 09:59
  • 1
    I find that the problem is not in \inputminted or \begin{minted} per se, but in the \begin{listing}. If I have a long listing inserted only using \inputminted then it works fine. – fikovnik Mar 09 '15 at 23:12
  • See also the comparison of minted to the verbments package, written by the minted maintainer: http://tex.stackexchange.com/a/103471/8666 – 0 _ May 29 '16 at 03:15
  • and a simpler, flatter, tidier, and likely more robust solution suggested here: http://tex.stackexchange.com/questions/12428/code-spanning-over-two-pages-with-minted-inside-listing-with-caption, by just placing a stand-alone caption and label after the minted code, without using any listing environment. Flat is better than nested. – 0 _ May 29 '16 at 03:23

2 Answers2

10

Unfortunately, this behaviour is due to a known bug. Worse, I have no idea how to fix it.

In the long run, I plan to get rid of the fancyvrb package altogether since strictly speaking minted doesn’t need the verbatim capabilities – LaTeX sequences get escaped anyway. This would lead to a lot more flexibility.

For now, though, this is unfortunately not a trivial change. If you want to get your hands dirty you can try \letting \Verbatim and \endVerbatim to \relax, and then tinkering a bit to get the rendering right again. Disclaimer: I haven’t tried this.

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
10

The bad behaviour of the minted package in truncating code can be prevented through the mdframed package. Example:

\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
  leftline=false, rightline=false, backgroundcolor=yellow!20!white]
    \begin{minted}[mathescape]{c++}
        int main()
            {
                bla bla...
            }
    \end{minted}
\end{mdframed}

Obviously you should have package mdframed loaded in your preamble, simply by \usepackage{mdframed} and eventually with global options. Even better, without pasting:

\begin{mdframed}[linecolor=black, topline=true, bottomline=true,
  leftline=false, rightline=false, backgroundcolor=yellow!20!white]
    \inputminted[fontsize=\scriptsize, linenos, frame=lines]{c++}{path/to/your/code.cpp}
\end{mdframed}
fmonegaglia
  • 1,754