在新环境中铸造

在新环境中铸造

我正在尝试将 Minted 封闭在一个新的环境中,如下所示:

\documentclass{article}
\usepackage{minted}
\usepackage{mdframed}
\usepackage[varqu]{zi4}

\definecolor{codebggray}{rgb}{0.95,0.95,0.95}

\newenvironment{codeblock}[1]{%
  \begin{mdframed}[backgroundcolor=codebggray]
  \begin{minted}[fontfamily=zi4]{#1}
}{%
  \end{minted}
  \end{mdframed}
}

\begin{document}
\begin{codeblock}{python}
  colors    = ['b','g','r','c','m','y','k']
  linetypes = ['-','--','-.',':']
  linestyle = itertools.product(colors,linetypes)
\end{codeblock}
\end{document}

问题是,当我尝试编译它时,我得到:

) (./bob.pyg) (/usr/local/texlive/2014/texmf-dist/tex/latex/upquote/upquote.sty
)
! FancyVerb Error:
  Extraneous input ` ' between \begin{minted}[<key=value>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.17 \begin{codeblock}{python}

!  ==> Fatal error occurred, no output PDF file produced!

是什么赋予了?

答案1

为了轻松定义您的新环境,您可以使用\newtcblisting功能强大的tcolorbox包以及它与以下组件配合的事实minted

\documentclass{article}
\usepackage{minted}
\usepackage{tcolorbox}
\tcbuselibrary{minted}

\definecolor{codebggray}{rgb}{0.95,0.95,0.95}

\newtcblisting{codeblock}[2][]{%
  colback=codebggray,
  listing only,
  minted options={
    fontsize=\small
  },  
  minted language=#2,
  #1
}

\begin{document}

\begin{codeblock}{python}
  colors    = ['b','g','r','c','m','y','k']
  linetypes = ['-','--','-.',':']
  linestyle = itertools.product(colors,linetypes)
\end{codeblock}

\end{document}

结果:

在此处输入图片描述

相关内容