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.

Can I define a newenvironment that has inside it a minted environment?

eg and environment that would be a quickhand for the following code:

\begin{figure}[htp]
    \centering
    \begin{minipage}{9cm}
        \begin{minted}[frame=single]{ruby}
some minted code...
        \end{minted}
    \end{minipage}
    \caption{a caption}
\end{figure}

I've tried using both the normal \newenvironment and the \NewEnviron from the environ package. Nothing seems to work, as I always get:

Paragraph ended before \FV@BeginScanning was complete.\par  \end{terminal} 
Extra }, or forgotten \endgroup.
\begin{VerbatimOut} on input line 87 ended by \end{minipage}.
Extra }, or forgotten \endgroup.

MWE:

\documentclass{article}
\usepackage{minted}
\usepackage{pgfkeys}
\usepackage{color}
\usepackage{environ}
\usemintedstyle{bw}

\definecolor{bg}{rgb}{0.95,0.95,0.95}

\pgfkeys{
    /terminal/.cd,
    caption/.code={\pgfkeyssetvalue{terminal/caption}{\caption{#1}}},
    label/.code={\pgfkeyssetvalue{terminal/label}{\label{#1}}},
}

\pgfkeyssetvalue{terminal/label}{}
\pgfkeyssetvalue{terminal/caption}{\caption{}}

\NewEnviron{terminal}[1]{
\begingroup
    %\pgfkeys{/terminal/.cd,caption={}}
    \begin{figure}[\thefigpos]
    \centering
    \begin{minipage}{9cm}
\begin{minted}[frame=single]{ruby}
\BODY
\end{minted}

    \end{minipage}
    %\pgfkeysvalueof{terminal/caption}
    %\pgfkeysvalueof{terminal/label}
    \end{figure}
\endgroup
}

\begin{document}

\begin{terminal}
a tryout
\end{terminal}
\end{document}
share|improve this question

1 Answer 1

up vote 9 down vote accepted

Unfortunately, there is no simple way since minted uses verbatim environments and isn’t designed to be included into other environments (mea culpa).

The following does what you want. It uses minted internals instead of the minted environment to achieve this. As a consequence, you need to put \makeatletter before the command if you want to include it into your tex file:

\newenvironment{terminal}{%
  \VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{frame=single}
  \begin{figure}[htp]
    \centering
    \begin{minipage}{9cm}    
      \begin{VerbatimOut}{\jobname.pyg}}
{%
      \end{VerbatimOut}
      \minted@pygmentize{ruby}
      \DeleteFile{\jobname.pyg}
    \end{minipage}
  \end{figure}}
share|improve this answer
2  
If there's any possibility of including something like this snippet in the main documentation, or providing a more suitable mechanism for allowing users to embed minted environments in custom ones, that'd be much appreciated. –  semperos Mar 29 '13 at 2:16

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.