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}
According to the manual of minted
, a minipage
and an underlying \colorbox
is used if the bgcolor=...
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:
\xpatchcmd{\minted@inputpyg@inline}{%
\colorbox%
}{%
\fboxsep\mintedfboxsep%
\colorbox%
}{\typeout{Success}}{\typeout{Failure}}
\makeatother
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.
\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage[paperwidth=6in,paperheight=9in, margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{libertine}
\usepackage[semibold]{sourcecodepro}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{xpatch}
\definecolor{bg}{rgb}{0.9, 0.9, 0.9}
\newlength{\mintedfboxsep}
\setlength{\mintedfboxsep}{1.5pt}
\newmintinline{python}{python3, fontsize=\scriptsize, framesep=0.1pt,bgcolor=bg}
\xpatchcmd{\minted@inputpyg@inline}{%
\colorbox%
}{%
\fboxsep\mintedfboxsep%
\colorbox%
}{\typeout{Success}}{\typeout{Failure}}
\makeatother
\begin{document}
Of these, \pythoninline{generators} is one of the biggest culprits. I'm guessing this is the case for most novice Python programmers.
\end{document}
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 for right
, left
, top
and bottom
separation.
Use one of the various \setlength{...}{}
statements to change at will.
\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage[paperwidth=6in,paperheight=9in, margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{libertine}
\usepackage[semibold]{sourcecodepro}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{xpatch}
\definecolor{bg}{rgb}{0.9, 0.9, 0.9}
\newlength{\mintedfboxsep}
\setlength{\mintedfboxsep}{0.2pt}
\newmintinline{python}{python3, fontsize=\scriptsize, framesep=0.1pt,bgcolor=bg}
\makeatletter
\newlength{\fboxrsep}
\setlength{\fboxrsep}{\fboxsep}
\newlength{\fboxlsep}
\setlength{\fboxlsep}{\fboxsep}
\newlength{\fboxtsep}
\setlength{\fboxtsep}{\fboxsep}
\newlength{\fboxbsep}
\setlength{\fboxbsep}{\mintedfboxsep}
\xpatchcmd{\minted@inputpyg@inline}{%
\colorbox%
}{%
\long\def\color@b@x##1##2##3%
{\leavevmode
\setbox\z@\hbox{\kern\fboxlsep{\set@color##3}\kern\fboxrsep}%
\dimen@\ht\z@\advance\dimen@\fboxtsep\ht\z@\dimen@
\dimen@\dp\z@\advance\dimen@\fboxbsep\dp\z@\dimen@
{##1{##2\color@block{\wd\z@}{\ht\z@}{\dp\z@}\box\z@}}}%
\colorbox%
}{\typeout{Success}}{\typeout{Failure}}
\makeatother
\begin{document}
Of these, \pythoninline{generators} is one of the biggest culprits. I'm guessing this is the case for most novice Python programmers.
Compare:
\colorbox{bg}{generators}
\end{document}
Best Answer
Put
\setlength{\fboxsep}{0pt}
before\begin{minted}