[Tex/LaTex] Background colour with minted package: code misplaced

minted

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?

Best Answer

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.

Related Question