Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}
share|improve this question
1  
I removed the {listings} tag since it doesn't seem like you're using the listings package. Would you be able to post what you've tried? –  Werner Jan 26 '12 at 17:33
    
Sorry, my bad. I'm really not using the listing package I'm using the floated listings, like the item 4 of the minted documentation‌​. I'll edit and add what I've tried. –  Hugo Maia Vieira Jan 27 '12 at 1:56
add comment

1 Answer 1

up vote 8 down vote accepted
\documentclass[12pt]{article}
\usepackage{minted}    
\makeatletter
\newenvironment{mycode}[3]
 {\VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{frame=single}
  \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

share|improve this answer
    
This worked like a charm. I just replace the figure by listing and remove the \centering. Thanks @Hebert. I didn't knew \makeatletter and \makeatother, but now I do. The \minted@proglang thing I can't get yet. –  Hugo Maia Vieira Jan 27 '12 at 2:26
1  
And I add an \floatstyle{ruled} at minted.sty. Exist an elegant way to do that? –  Hugo Maia Vieira Jan 27 '12 at 2:55
    
look at the package floatrow how floating objects can be configured –  Herbert Jan 27 '12 at 8:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.