6

I'm trying to enclose Minted in a new environment as follows:

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

Problem is, when I try to compiled this, I get:

) (./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!

What gives?

Richard
  • 5,723

1 Answers1

4

To easily define your new environment, you could use \newtcblisting from the powerful tcolorbox package and the fact that it cooperates with 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}

The result:

enter image description here

Gonzalo Medina
  • 505,128