12

I want to create a new environment to transform this:

\begin{listing}
  \caption{some caption}
  \label{code:label}
  \begin{minted}[options]{language}
My code here
  \end{minted}
\end{listing}

into this:

\begin{mycode}{language}{some caption}{code:label}
My code here
\end{mycode}

I've tried the code from Using minted to define a new environment without success.

edit

This is what I've tried:

\newenvironment{mycode}[3]{%
  \VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{linenos,fontfamily=courier, fontsize=\scriptsize, xleftmargin=21pt}
  \begin{listing}
    \caption{#2}
    \label{#3}
    % \centering
    \begin{VerbatimOut}{\jobname.pyg}}
{%
    \end{VerbatimOut}
    \minted@pygmentize{#1}
    \DeleteFile{\jobname.pyg}
  \end{listing}}

3 Answers3

10
\documentclass[12pt]{article}
\usepackage{minted}    
\makeatletter
\newenvironment{mycode}[3]
 {\VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{frame=single}% from fancyvrb
  \renewcommand{\minted@proglang}[1]{#1}
  \begin{figure}[htp]%% default placing
    \centering
    \caption{#2}\label{#3}
      \begin{VerbatimOut}{\jobname.pyg}}
 {\end{VerbatimOut}
  \minted@pygmentize{\minted@proglang{}}
  \DeleteFile{\jobname.pyg}
  \end{figure}}
\makeatother

\begin{document}
\begin{mycode}{latex}{some caption}{code:label}
My \code here
\end{mycode}

\end{document}

enter image description here

5

You don't need so complicated a code:

\documentclass[12pt]{article}
\usepackage{minted}

\newenvironment{mycode}[4][]
 {\VerbatimEnvironment
  \begin{listing}
  \caption{#3}\label{#4}
  \begin{minted}[
    linenos,
    fontfamily=courier,
    fontsize=\scriptsize,
    xleftmargin=21pt,
    #1]{#2}}
 {\end{minted}\end{listing}}

\begin{document}

\begin{mycode}{latex}{some caption}{code:label}
My \code here
\end{mycode}

\begin{mycode}[fontsize=\large]{latex}{some caption}{code:label2}
My \code here
\end{mycode}

\end{document}

enter image description here

egreg
  • 1,121,712
0

Here is a solution with a much easier definition of environments. It requires you to use but one more package, caption, in addition to listings and minted.

\documentclass[12pt]{article}

\usepackage{listings} \usepackage{caption} \usepackage[newfloat]{minted} \newminted{python}{ fontsize=\scriptsize, numbersep=8pt, frame=lines, framesep=3mm } \newminted{latex}{ fontsize=\footnotesize, numbersep=8pt, xleftmargin=21pt, linenos, fontfamily=courier, }

\begin{document}

\begin{listing} \captionof{listing}{Example for python\label{python}} \begin{pythoncode} from xyz imort abc

print("Example python code") \end{pythoncode} \end{listing}

\begin{listing} \captionof{listing}{Example for Latex\label{latex}} \begin{latexcode} \usepackage{latex} \end{latexcode} \end{listing}

\end{document}

Example

There's an additional benefit to that. For example, with \SetupFloatingEnvironment{listing}{name=Source Code} you can change the display name of the environment. In the example it'd called Source Code 1, Source Code 2, etc.

MERose
  • 228