Sunday, 2 June 2013

Using minted environment in user-defined environment (minted inside minipage)

Using minted environment in user-defined environment (minted inside minipage)

I am having to use the minted environment inside a minipage environment quite often in my document. I wanted to write a new environment for doing this, but since minted isn't designed to be included inside other environments, I decided to write a small patch using minted internals, based on Using minted to define a new environment and am including this patch in my main document. However, it still doesn't seem to work.
Error message:
Paragraph ended before \FV@BeginScanning was complete \begin{cppcode}[10.7cm]
Can someone help me figure out what I'm doing wrong?
Here is the code for the "patch". It re-defines the \newminted shortcut command to incorporate the minipage environment in the custom minted environments, using a couple of nested conditionals to check whether an optional minipage width has been provided or not.
\makeatletter
\DeclareDocumentCommand \newminted {o m m}
{
  \IfNoValueTF {#1} % check if custom env. name is provided, and create new env. accordingly
   {\def\minted@envname{#2code}}
   {\def\minted@envname{#1}}
  \DeclareDocumentEnvironment {\minted@envname} {o} % default env.
   {  % begin definition
     \IfNoValueTF {##1}
     {  % minipage width not provided

        \VerbatimEnvironment\begin{minted}[#3]{#2}   

     }
     {  % minipage width provided

        \VerbatimEnvironment
        \minted@resetoptions
        \setkeys{minted@opt}{#3}
        \centering
        \begin{minipage}{##1}   
        \begin{VerbatimOut}{\jobname.pyg}

     }
   }
   {  % end definition
     \IfNoValueTF {##1}
     {  % minipage width not provided

        \end{minted}

     }
     {  % minipage width provided

        \end{VerbatimOut}
         \minted@pygmentize{#2}
        \DeleteFile{\jobname.pyg}
        \end{minipage}

     }  
   }

   \DeclareDocumentEnvironment {\minted@envname *} {m o}    % custom env. name
   { % begin definition
     \IfNoValueTF {##2}
     {  % minipage width not provided

        \VerbatimEnvironment\begin{minted}[#3,##1]{#2}

     }
     {  % minipage width provided

        \VerbatimEnvironment
        \minted@resetoptions
        \setkeys{minted@opt}{#3,##1}
        \centering
        \begin{minipage}{##2}   
        \begin{VerbatimOut}{\jobname.pyg}

     }
    }      
    { % end definition
        \IfNoValueTF {##2}
        {   % minipage width not provided

            \end{minted}

        }
        {   % minipage width provided

            \end{VerbatimOut}
            \minted@pygmentize{#2}
            \DeleteFile{\jobname.pyg}
            \end{minipage}

        } 
     }
}
\makeatother
Here is the MWE using the above code:
\documentclass[10pt]{article}

\usepackage{xparse}   

% Page layout
\usepackage[landscape,margin=0.5in]{geometry}
\usepackage{multicol}   

% Code
\usepackage{minted}
\definecolor{col}{HTML}{F5EAB5}

\input{minted_patch2.tex} % CONTAINS THE PATCHED CODE

\usemintedstyle{trac}
\newminted{cpp}{bgcolor=col, linenos=true}
\newcommand{\keyw}[1]{\texttt{\textbf{#1}}}   

\title{}
\author{}
\date{}

\begin{document}
\begin{multicols}{2}
\maketitle

\section{Primitive Built-in Types}

\subsection{Literals}

Ev

No comments:

Post a Comment