16

With the following example

\listfiles
\documentclass{article}
\usepackage{minted}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\begin{document}
Some text
\begin{minted}{latex}
A test
\end{minted}

Some text
\begin{minted}[linenos=true]{latex}
A test
\end{minted}

Some text
\begin{minted}[bgcolor=bg]{latex}
A test
\end{minted}
\end{document}

the first two code blocks are typeset below the text, but the last one is to the side: Example output

I have Pymentize v1.4 installed, and \listfiles gives

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
  minted.sty    2010/01/27 v1.6 Yet another Pygments shim for LaTeX
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
fancyvrb.sty    2008/02/07
   color.sty    2005/11/14 v1.0j Standard LaTeX Color (DPC)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2010/11/26 v0.05c Graphics/color for pdfTeX
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
  ifthen.sty    2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
    calc.sty    2007/08/22 v4.3 Infix arithmetic (KKT,FJ)
ifplatform.sty    2010/10/22 v0.4 Testing for the operating system
pdftexcmds.sty    2010/04/01 v0.9 Utility functions of pdfTeX for LuaTeX (HO)
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/message (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
 ltxcmds.sty    2010/04/26 v1.7 LaTeX kernel commands for general use (HO)
catchfile.sty    2010/04/28 v1.5 Catches the contents of a file (HO)
etexcmds.sty    2010/01/28 v1.3 Prefix for e-TeX command names (HO)
minted-test.w18
supp-pdf.mkii
minted-test.pyg
minted-test.out.pyg
minted-test.out.pyg
minted-test.out.pyg
 ***********

This seems like something that would have been noticed as a bug, so what have I got wrong?

3 Answers 3

5

minted uses the following macro internally if a background colour is selected:

\newsavebox{\minted@bgbox}

\newenvironment{minted@colorbg}[1]{
  \def\minted@bgcol{#1}
  \noindent
  \begin{lrbox}{\minted@bgbox}
  \begin{minipage}{\linewidth-2\fboxsep}}
 {\end{minipage}
  \end{lrbox}%
  \colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}

Without further testing I’m assuming that the use of a minipage doesn’t introduce a new paragraph. If so, a simple \par command before the box should correct the problem.

This seems like something that would have been noticed as a bug,

It should have. The fact is, the documentation minted.pdf pretty much serves as the only test case at the moment and in the documentation the bgcolor option is only used in a single example without surrounding text, so the lack of a paragraph break makes no difference.

2
  • Thanks for that Konrad: I'll use a patched version of the environment for the moment. I'm quite familiar with 'use the documentation as a test file' as that is what I do for siunitx. The reason I thought other people might have noticed is that coloured background for code snippets is reasonably common.
    – Joseph Wright
    Commented Jan 17, 2011 at 8:16
  • 1
    @Joseph: the problem is that coloured background is kind of broken in minted: try having your code span over a page break. This only works properly without background colour, unfortunately, and I’m at a complete loss how to fix that. :-( Commented Jan 17, 2011 at 9:37
4

You can solve this issue by making use of mdframed package (\usepackage{mdframed}), this way:

\definecolor{bg}{rgb}{0.95,0.95,0.95}
...

\begin{mdframed}[backgroundcolor=bg]
    \begin{minted}{latex}
        your code
    \end{minted}
\end{mdframed}
2

To update the previous answers, this is what the manual says about it as 2016:

This option will prevent breaklines from working with \mintinline. A \colorbox cannot break across lines.

This option will prevent environments from breaking across pages. If you want support for page breaks and advanced options, you should consider a framing package such as framed, mdframed, or tcolorbox. It is easy to add framing to minted commands and environments using the etoolbox package. For example, using mdframed:

\BeforeBeginEnvironment{minted}{\begin{mdframed}}
\AfterEndEnvironment{minted}{\end{mdframed}}

Some framing packages also provide built-in commands for such purposes. For example, mdframed provides a \surroundwithmdframed command, which could be used to add a frame to all minted environments:

\surroundwithmdframed{minted} 

tcolorbox even provides a built-in framing environment with minted support.

1
  • This should be updated again.
    – paleonix
    Commented Mar 2, 2021 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .