13

I want to add a background color to the minted environment in my LaTeX document. However, when I add the the background color the environment does not start in a new line. Could someone help me fix this problem?

\documentclass[11pt,letterpaper]{article}

\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{minted} 
\definecolor{bg}{rgb}{0.85,0.85,0.85}
\newminted[bashcode1]{bash}{linenos=true, texcl=true, bgcolor=bg}
\newminted[bashcode2]{bash}{linenos=true, texcl=true}

\listfiles

\begin{document}
\noindent The following minted environment does not begin in a new line because of adding a background color to the minted environment.
\begin{bashcode1}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode1}
When I remove the background color the environment begins in a new line, which is what I am looking for.
\begin{bashcode2}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode2}

\end{document}

The following is the result of listfiles.

*File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size11.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
geometry.sty    2010/09/12 v5.6 Page Geometry
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
  ifvtex.sty    2010/03/01 v1.5 Detect VTeX and its facilities (HO)
 ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
geometry.cfg
  minted.sty    2011/09/17 v1.7 Yet another Pygments shim for LaTeX
fancyvrb.sty    2008/02/07
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
   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    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
catchfile.sty    2011/03/01 v1.6 Catch the contents of a file (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
supp-pdf.mkii
mintedproblem.pyg
mintedproblem.out.pyg
mintedproblem.out.pyg

I get the following output from the MWE.

enter image description here

1 Answers1

24

It's a bug in the minted package, that doesn't properly treat the box containing the listing.

\documentclass[11pt,letterpaper]{article}

\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{minted}

%% fix the minted@colorbg environment
\makeatletter
\renewenvironment{minted@colorbg}[1]
 {\def\minted@bgcol{#1}%
  \noindent
  \begin{lrbox}{\minted@bgbox}
  \begin{minipage}{\linewidth-2\fboxsep}}
 {\end{minipage}%
  \end{lrbox}%
  \setlength{\topsep}{\bigskipamount}% set the vertical space
  \trivlist\item\relax % ensure going to a new line
  \colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}%
  \endtrivlist % close the trivlist
 }
\makeatother

\definecolor{bg}{rgb}{0.85,0.85,0.85}
\newminted[bashcode1]{bash}{linenos=true, texcl=true, bgcolor=bg}
\newminted[bashcode2]{bash}{linenos=true, texcl=true}


\begin{document}

The following minted environment begins in a new line even if 
a background color is added to the minted environment.
\begin{bashcode1}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode1}
When I remove the background color the environment begins in a new line, 
which is what I am looking for.
\begin{bashcode2}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode2}

\end{document}

enter image description here

I suggest you to have a look at the very powerful tcolorbox package, that has an interface for minted. Here's an example:

\documentclass[11pt,letterpaper]{article}

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\newtcblisting{bashcode}{
  listing engine=minted,
  colback=bashcodebg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=bash,
  minted options={linenos=true,texcl=true},
  left=1mm,
}
\definecolor{bashcodebg}{rgb}{0.85,0.85,0.85}

\begin{document}

The following minted environment begins in a new line even if
a background color is added to the minted environment.
\begin{bashcode}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode}

\end{document}

enter image description here

Yet a different version with a possibly better placement of line numbers:

\documentclass[11pt,letterpaper]{article}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\newtcblisting{bashcode}[1][]{
  listing engine=minted,
  colback=bashcodebg,
  colframe=black!70,
  listing only,
  minted style=colorful,
  minted language=bash,
  minted options={linenos=true,numbersep=3mm,texcl=true,#1},
  left=5mm,enhanced,
  overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
            rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}}
}
\definecolor{bashcodebg}{rgb}{0.85,0.85,0.85}

\begin{document}

The following minted environment begins in a new line even if 
a background color is added to the minted environment.
\begin{bashcode}
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode}

The following minted environment begins in a new line even if 
a background color is added to the minted environment.
\begin{bashcode}[linenos=false]
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer # Java version 8
\end{bashcode}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 1
    Thank you very much for introducing me tcolorbox. It is just a great package. – shashashamti2008 Apr 28 '14 at 04:43
  • 1
    @ egreg: I could run your MWE using tcolorbox with no problem. However, when I put it in my document I get Illegal parameter number in definition of \next # error and texstudio highlights the line which points to colback=bg,. When I comment the line: colback=bg, I still get the same error. Could you advise me on this? – shashashamti2008 Apr 28 '14 at 18:47
  • @Ahm Sorry, it's not possible without a minimal example showing the issue. It may depend on tens of different factors. – egreg Apr 28 '14 at 21:32
  • Is there a way to make this work on beamer as well? I get an empty box right now. – IVlad Jul 08 '16 at 16:31
  • 1
    @IVlad I get the expected result, but I had to change the name for the background color from bg to something else. Remember to make a fragile frame. – egreg Jul 08 '16 at 16:35
  • @egreg I asked another question. If I change the name of bg, the background becomes gray, but no code shows up. – IVlad Jul 08 '16 at 16:45