# Possible bug in minted package

(just now while typing the topic title I found possible related but my post is too long to discard it now :-) )

(the minipages is only to show side by side. I'll not use it)

I'm using minted package to type code blocks.

The basic environment is minted which produces the code block with nice vertical spaces above/below the code (see example below: left side).

We can change the style of the code block making use of \newminted.

But the problem is that if we use a background color for the block those vertical spaces are affected (see example below: right side).

Reading the minted documentation (section 8.4) we find the code (commented 2nd line in original form)

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


So I noticed that there is no vertical space around the box and then I edited that code and inserted \vspace{\abovedisplayskip} and \vspace{\belowdisplayskip} before and after lrbox, as shown below.

% from texdoc minted
\makeatletter
\renewenvironment{minted@colorbg}[1]{
%\setlength{\fboxsep}{-\fboxrule}
\def\minted@bgcol{#1}
\vspace{\abovedisplayskip} %%             my code
\noindent%
\begin{lrbox}{\minted@bgbox}
\begin{minipage}{\linewidth-2\fboxsep}}
{\end{minipage}
\end{lrbox}%
\vspace{\belowdisplayskip} %%             my code
\colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}
\makeatother


The result was almost satisfactory since the space after the code is not the same.

Question 1: is this the better way to correct the problem?

Question 2: What is the correct space to be inserted in this case?

Question 3: Could it be a bug in the package code?

## MWE

\documentclass{report}
\usepackage{lipsum}
\usepackage{minted}
\usepackage[margin=0cm,paperheight=13cm,paperwidth=21cm]{geometry}

\usepackage{tikz,eso-pic}
\usetikzlibrary{shapes.misc}
\AtBeginDocument{%
\begin{tikzpicture}[overlay,remember picture]
\draw[red!30!white]
(current page.south west) grid[step=5mm]
(current page.north east);
\draw[red!50!white]
(current page.south west) grid[step=10mm]
(current page.north east);
\end{tikzpicture}
}%
}

\definecolor{bg}{rgb}{0.95,0.95,0.95}
\newminted{tex}{%
bgcolor=bg,%
%    gobble=0,%
%    linenos=true,%
%    firstline=1,%
%    firstnumber=1,%
%    fontfamily=courier,%
%    fontsize=\footnotesize,%
%    numberblanklines=false,%
%    numbersep=3mm,%
%    xleftmargin=6mm,%
%    xrightmargin=5mm%
}

% from texdoc minted
\makeatletter
\renewenvironment{minted@colorbg}[1]{
%\setlength{\fboxsep}{-\fboxrule}
\def\minted@bgcol{#1}
\vspace{\abovedisplayskip}
\noindent%
\begin{lrbox}{\minted@bgbox}
\begin{minipage}{\linewidth-2\fboxsep}}
{\end{minipage}
\end{lrbox}%
\vspace{\belowdisplayskip}%
\colorbox{\minted@bgcol}{\usebox{\minted@bgbox}}}
\makeatother

\begin{document}\pagestyle{empty}
\noindent
\begin{minipage}[t]{10cm}
\lipsum[2]%
\begin{minted}{tex}
$$\frac{3}{2}=\frac{a}{b}$$
\end{minted}
\lipsum[2]
\end{minipage}%
\hfill%
\begin{minipage}[t]{10cm}
\lipsum[2]%
\begin{texcode}
$$\frac{3}{2}=\frac{a}{b}$$
\end{texcode}
\lipsum[2]
\end{minipage}

\end{document}

• If you really want good, configurable background colors (and perhaps want code that will break across pages when there is a background color), you should use a proper framing package like tcolorbox or mdframed instead. The current background color in minted could probably be improved, but given its limitations (only a few lines of code), framing packages are a better solution in many cases. – G. Poore Jul 1 '14 at 0:20
• – Werner Jul 1 '14 at 2:59
• @Werner, sorry. I don't know if this is the case but the minipage was used only to show side by side. I guess that my problem is not related with the minipage. – Sigur Jul 1 '14 at 3:15