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 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}

Every literal has a type determined by its \textit{form} and \textit{value}

\subsubsection{Integer \& Floating Point Literals}

Integer literals can be represented using \textbf{decimal}, \textbf{octal}, or \textbf{hexadecimal} notations. Integer
literals that begin with \texttt{0} (zero) are interpreted as octal. Those that begin with either
\texttt{0x} or \texttt{0X} are interpreted as hexadecimal:

% Minipage width specified. NO on-the-fly options

\begin{cppcode}[10.7cm]
20 /* decimal */ 024 /* octal */ 0x14 /* hexadecimal */
\end{cppcode}

By default, decimal literals are signed. whereas octal and hexadecimal literals can either  be signed or unsigned. Decimal literal has the smallest type of \keyw{int}, \keyw{long}, or \keyw{long long} in which the value fits. Octal and hexadecimal literals have the smallest type of \keyw{int}, \keyw{unsigned int}, \keyw{long}, \keyw{unsigned long}, \keyw{long long}, or \keyw{unsinged long long}

Floating-point literals include either a decimal point or an exponent specified using scientific notation. Using scientific notation, the exponent is indicated by either E or e:

% Minipage width specified. WITH on-the-fly options

\begin{cppcode*}{linenos=false}[6cm]
3.14159 3.14159E0 0. 0e0 .001
\end{cppcode*} 

\end{multicols}
\end{document}
share|improve this question
    
All those blank lines look very suspicious. Can you make the example into a complete one, with a use case? –  egreg May 31 '13 at 22:46
    
@egreg I've added an MWE! –  Chatterjee May 31 '13 at 23:07
    
@egreg Any suggestions? –  Chatterjee May 31 '13 at 23:43
    
Verbatim environments are bad beasts. –  egreg May 31 '13 at 23:44
    
Should I drop the idea altogether? Or can something be done with the code? –  Chatterjee May 31 '13 at 23:44

1 Answer 1

up vote 4 down vote accepted

This code could be cleaned up a little, but should give you most of what you need. Getting an optional argument for a verbatim environment, when there is no mandatory argument, is always tricky. See this for reference.

I renamed your environment to cpp so that I wouldn't have to dig in the minted internals, and put the optional argument before the mandatory argument, as is typical.

\documentclass[10pt]{article}

% 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}

\makeatletter

\newif\ifmintedminipage

\def\getoptarg[#1]{%
  \endgroup
  \ifthenelse{\equal{#1}{}}%
    {\mintedminipagefalse\begin{cppcode}}%
    {\mintedminipagetrue
      \begin{minipage}{#1}\begin{cppcode}}%
}

\newenvironment{cpp}%
  {\VerbatimEnvironment
    \begingroup\obeylines
    \@ifnextchar[{\getoptarg}{\getoptarg[]}}%
  {\end{cppcode}%
    \ifmintedminipage\end{minipage}\fi
  }

\newenvironment{cpp*}[2][]%
  {\VerbatimEnvironment
    \ifthenelse{\equal{#1}{}}%
      {\mintedminipagefalse\begin{cppcode*}{#2}}%
      {\mintedminipagetrue
        \begin{minipage}{#1}\begin{cppcode*}{#2}}%
  }%
  {\end{cppcode*}%
    \ifmintedminipage\end{minipage}\fi
  }

\makeatother


\newcommand{\keyw}[1]{\texttt{\textbf{#1}}}    

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

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

\section{Primitive Built-in Types}

\subsection{Literals}

Every literal has a type determined by its \textit{form} and \textit{value}

\subsubsection{Integer \& Floating Point Literals}

Integer literals can be represented using \textbf{decimal}, \textbf{octal}, or \textbf{hexadecimal} notations. Integer
literals that begin with \texttt{0} (zero) are interpreted as octal. Those that begin with either
\texttt{0x} or \texttt{0X} are interpreted as hexadecimal:

% Minipage width specified. NO on-the-fly options

\begin{cpp}[10cm]
20 /* decimal */ 024 /* octal */ 0x14 /* hexadecimal */
\end{cpp}

\begin{cpp}
20 /* decimal */ 024 /* octal */ 0x14 /* hexadecimal */
\end{cpp}

By default, decimal literals are signed. whereas octal and hexadecimal literals can either  be signed or unsigned. Decimal literal has the smallest type of \keyw{int}, \keyw{long}, or \keyw{long long} in which the value fits. Octal and hexadecimal literals have the smallest type of \keyw{int}, \keyw{unsigned int}, \keyw{long}, \keyw{unsigned long}, \keyw{long long}, or \keyw{unsinged long long}

Floating-point literals include either a decimal point or an exponent specified using scientific notation. Using scientific notation, the exponent is indicated by either E or e:

% Minipage width specified. WITH on-the-fly options

\begin{cpp*}[10cm]{linenos=false}
3.14159 3.14159E0 0. 0e0 .001
\end{cpp*}

\begin{cpp*}{linenos=false}
3.14159 3.14159E0 0. 0e0 .001
\end{cpp*}

\end{multicols}
\end{document}
share|improve this answer
    
Perfect! Works like a charm. Just one question though. How are the default options provided by newminted getting passed to these environments? Once again, thank you very much for the help. Much appreciated :) –  Chatterjee Jun 1 '13 at 4:50
    
Let's say if I want to change the language, how would I pass the name change to the environments? –  Chatterjee Jun 1 '13 at 4:57
    
The \newminted command creates cppcode and cppcode* environments. The default options are put into those environments at the time of definition; the options aren't stored somewhere and passed later. Changing the language is not supported. If you want multiple languages, you could create a pair of environments for each language. Or you could use something like my definition of cpp*, except with minted internally, and add a minted option key for the minipage width so that all options can be passed in a single optional argument. –  G. Poore Jun 1 '13 at 5:03
    
Just an update: I incorporated your code into minted.sty and everything is working smoothly for the time being. I also tried out pythontex, and boy was it fast! Just one thing though, I tried defining a new environment where I embedded a pygments environment inside a minipage environment, but it wasn't working. On the other hand, there was absolutely no problem in embedding pygment inside a tabularx cell. –  Chatterjee Jun 2 '13 at 8:36
1  
I recently learnt about the varwidth package which allows for automatic width adjustment for minipages based on the natural width of the text. Any suggestions on how I can incorporate the varwidth environment to enclose the minted environment? I tried wrapping the minted environments with this in minted.sty, but am getting some weird errors –  Chatterjee Jun 26 '13 at 2:35

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.