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}
Best Answer
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 theminted
internals, and put the optional argument before the mandatory argument, as is typical.