铸造和上市的嵌套

铸造和上市的嵌套

minted和的耦合是否可以listing被包装到一个新的环境中?

我无法使此代码工作:

\documentclass[a4paper]{article}

\usepackage{minted}
\usepackage{xparse}
  \NewDocumentEnvironment{captionminted}{ o }
  {%
  \begin{listing}%
    \begin{minted}{cpp}%
  }%
  {%
    \end{minted}%
  \IfNoValueTF{#1}{}{\caption{#1}}%
  \end{listing}%
  }


\begin{document}
\begin{captionminted}[abc]
  cout << "This is my message: "; 
  cout << str << endl; 
\end{captionminted}
\end{document}

由于此错误信息:

! LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.3   i
       nt i = 0;

答案1

你忘记了\VerbatimEnvironment。不过,我认为这种方法不会给你带来太多好处:你反而失去了灵活性。

\documentclass[a4paper]{article}

\usepackage{minted}
\usepackage{xparse}

\NewDocumentEnvironment{captionminted}{ o }
 {%
  \VerbatimEnvironment\begin{listing}%
  \begin{minted}{cpp}%
 }
 {%
  \end{minted}%
  \IfNoValueF{#1}{\caption{#1}}%
  \end{listing}%
 }


\begin{document}

\begin{captionminted}[abc]
cout << "This is my message: ";
cout << str << endl;
\end{captionminted}

\end{document}

不要缩进逐字环境:行首的空格不会被忽略。

在此处输入图片描述

相关内容