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.
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}
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}
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}
Best Answer
According to the manual of
minted
, aminipage
and an underlying\colorbox
is used if thebgcolor=...
option is applied.Since
\colorbox
uses the\fboxsep
length as separation between text and box 'frame', it's sufficient to change\fboxsep
.Applying a patch to
\minted@inputpyg@inline
this will succeed:Since this all happens in a group (see the detailed definition in
minted.sty
, the outer\fboxsep
value isn't changed.However:
\fboxsep
is a common length for all four separations of a\fbox
or\colorbox
box, i.e. the top, bottom, left and right separations are changed all if\fboxsep
is modified!This is a quick patch to change the underlying
\fboxsep
length.Edit
What I've stated above about
\fboxsep
is true here, but I've changed the internal definition of\colorbox
for the minted inline code only, introducing four lenghts:\fboxrsep
,\fboxlsep
,\fboxtsep
and\fboxbsep
, each responsible forright
,left
,top
andbottom
separation.Use one of the various
\setlength{...}{}
statements to change at will.