# New environment for Python source code

I am trying hard to create an environment for my source code. Here's my current state

\documentclass[a4paper,fontsize=12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{beton,euler}
\usepackage[english]{babel}

\usepackage{minted}
\usepackage{xcolor}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\newminted{python3}{bgcolor=bg, tabsize=4}

\newenvironment{sourcecode}[1]
{
\def\savedcaption{\caption{#1}}
\begin{listing}[ht]
\begin{python3code}
}
{
\end{python3code}
\savedcaption
\end{listing}
}

\title{Research}

\author{Me}

\date{\today}

\begin{document}
\maketitle
\begin{sourcecode}{"Hello"}
print("Hello")
\end{sourcecode}

\end{document}


I get fancyVerbErrors. I am lost. Any help would be very appreciated

• Please post a complete example that we can run to get the errors, a fragment is not so useful, – David Carlisle Jan 26 '16 at 12:43

This should do what you want.

\newenvironment{sourcecode}[1]
{%
\VerbatimEnvironment
\def\savedcaption{\caption{#1}}%
\begin{listing}[ht]%
\begin{python3code}%
}
{%
\end{python3code}%
\savedcaption
\end{listing}%
}


You need \VerbatimEnvironment, because you are creating an environment that uses fancyvrb internally. You need a % after \begin{python3code} to prevent the newline that follows it from being treated as a space and triggering a fancyvrb error.

• @tschm I just added a note on this to the documentation for the next version of minted, so it should be easier to find in the future. – G. Poore Jan 26 '16 at 13:43