minted icon indicating copy to clipboard operation
minted copied to clipboard

bgcolor incompatible when using xleftmargin and/or xrightmargin

Open natzei opened this issue 5 years ago • 5 comments

Hi all. The bug is pretty obvious from the images. Any workaround?

w/o margins: schermata del 2018-09-06 16-21-08

w/ margins: schermata del 2018-09-06 16-21-38

natzei avatar Sep 06 '18 14:09 natzei

Can you provide the source for your example?

gpoore avatar Sep 06 '18 14:09 gpoore

Sure.

\documentclass[]{article}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{minted}

\setminted{%
    frame=single,
    bgcolor=lightgray
}

%opening
\title{}
\author{}

\begin{document}

\begin{minted}[]{c}
    hello world
\end{minted}


\begin{minted}[xleftmargin=0.1\textwidth,xrightmargin=0.1\textwidth]{c}
hello world
\end{minted}

\end{document}

Result: schermata del 2018-09-06 17-13-56

natzei avatar Sep 06 '18 15:09 natzei

Does anyone get any solutions?

StevenTang1998 avatar Mar 21 '24 03:03 StevenTang1998

Try this:

\documentclass{article}
\usepackage{minted}

\setminted{
    frame=single,
}

\makeatletter
\renewenvironment{minted@snugshade*}[1]{%
  \def\FrameCommand##1{\hskip\@totalleftmargin
    %% old code
    % \colorbox{#1}{##1}%
    %% new code
    \hskip\FV@XLeftMargin
    \colorbox{#1}{\hskip-\FV@XLeftMargin##1\hskip-\FV@XRightMargin}%
    \hskip\FV@XRightMargin
    %% end new code
    \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
  \MakeFramed{\advance\hsize-\width
    \@totalleftmargin\z@ \linewidth\hsize
    \advance\labelsep\fboxsep
    \@setminipage}%
 }{\par\unskip\@minipagefalse\endMakeFramed}
\makeatother

\begin{document}

\begin{minted}[bgcolor=lightgray]{c}
    hello world
\end{minted}

\begin{minted}[xleftmargin=0.1\textwidth,xrightmargin=0.2\textwidth]{c}
hello world
\end{minted}

\begin{minted}[
  bgcolor=lightgray,
  xleftmargin=0.1\textwidth,
  xrightmargin=0.2\textwidth
]{c}
hello world
\end{minted}

\end{document}

image

Note in general tcolorbox's minted library provides more powerful hence better control on decorating minted environment output.

muzimuzhi avatar Mar 21 '24 04:03 muzimuzhi

Wonderful! Thanks so much for your help!

StevenTang1998 avatar Mar 21 '24 05:03 StevenTang1998