minted 环境 minipage 环境内部

minted 环境 minipage 环境内部

我在文档中经常需要minted在环境中使用环境。我想编写一个新的环境来执行此操作,但由于它不设计为包含在其他环境中,因此我决定使用编写一个小补丁,基于 minipagemintedminted internals使用 minted 定义新环境我将这个补丁包含到我的主要文档中。但是,它似乎仍然不起作用。

错误信息:

Paragraph ended before \FV@BeginScanning was complete \begin{cppcode}[10.7cm]

有人能帮我找出我做错什么了吗?

这是“补丁”的代码。它重新定义了\newminted快捷方式命令以将minipage环境合并到自定义minted环境中,并使用几个嵌套条件来检查是否minipage提供了可选宽度。

\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

以下是使用上述代码的 MWE:

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

答案1

此代码可以稍微清理一下,但应该可以满足您的大部分需求。在没有强制参数的情况下,获取 verbatim 环境的可选参数总是很棘手。请参阅以供参考。

我将您的环境重命名为,cpp这样我就不必深入研究minted内部结构,并将可选参数放在强制参数之前,这是很典型的。

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

相关内容