在 minted 环境中添加背景颜色时出现问题

在 minted 环境中添加背景颜色时出现问题

我想minted在 LaTeX 文档的环境中添加背景颜色。但是,当我添加背景颜色时,环境不会从新行开始。有人能帮我解决这个问题吗?

\documentclass[11pt,letterpaper]{article}

\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{minted} 
\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}

\listfiles

\begin{document}
\noindent The following minted environment does not begin in a new line because of adding a background color 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}

以下是的结果listfiles

*File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size11.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
geometry.sty    2010/09/12 v5.6 Page Geometry
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
  ifvtex.sty    2010/03/01 v1.5 Detect VTeX and its facilities (HO)
 ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
geometry.cfg
  minted.sty    2011/09/17 v1.7 Yet another Pygments shim for LaTeX
fancyvrb.sty    2008/02/07
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
  ifthen.sty    2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
    calc.sty    2007/08/22 v4.3 Infix arithmetic (KKT,FJ)
ifplatform.sty    2010/10/22 v0.4 Testing for the operating system
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
catchfile.sty    2011/03/01 v1.6 Catch the contents of a file (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
supp-pdf.mkii
mintedproblem.pyg
mintedproblem.out.pyg
mintedproblem.out.pyg

我从 MWE 获得以下输出。

在此处输入图片描述

答案1

这是包装中的一个错误minted,没有正确处理包含列表的盒子。

\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}

在此处输入图片描述

我建议你看一下这个非常强大的tcolorbox包,它有一个接口minted。下面是一个例子:

\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}

在此处输入图片描述

还有另一个版本,其行号位置可能更好:

\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}

在此处输入图片描述

相关内容