[Tex/LaTex] Minted Package, inline background color

minted

MWE:

\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}
\definecolor{bg}{rgb}{0.9, 0.9, 0.9}

\makeatother

\begin{document}

\newmintinline{python}{python3, fontsize=\scriptsize, bgcolor=bg}

Of these, \pythoninline{generators} is one of the biggest culprits. I'm guessing this is the case for most novice Python programmers.

\end{document}

producesenter image description here

Is it possible to make the bottom part of the painted area shorter?

Best Answer

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}

enter image description here