为 minted 环境定义命令

为 minted 环境定义命令

我如何为 minted 定义一个新命令宏或环境?什么原因导致以下问题不起作用?

\newcommand{\CppSourceCode}{
\begin{minted}[linenos=true, mathescape, xleftmargin=1cm]{c++}
 #1 
\end{minted}
}

输出 :

    ! Missing number, treated as zero.
<to be read again> 
                   }
l.23 ...pe, xleftmargin=1cm]{c++} #1 \end{minted}}
                                                  
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Illegal parameter number in definition of \CppSourceCode.
<to be read again> 
                   1
l.23 ...pe, xleftmargin=1cm]{c++} #1 \end{minted}}
                                                  
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

(C:\Users\ZfMGPU\Desktop\Xetextest\main\main.aux)
LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for EU1/lmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
runsystem(pygmentize -S default -f latex > main.pyg)...executed.

 (C:\Users\ZfMGPU\Desktop\Xetextest\main\main.pyg)
! Illegal parameter number in definition of \@tempa.
<to be read again> 
                   1
l.37 \CppSourceCode{asdasdasd asd asd }
                                       
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! FancyVerb Error:
  Extraneous input ` ##1 \end {minted}{asdasdasd asd asd }' between \begin{minted}[<key=value>] and line end
.
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
                                                  
l.37 \CppSourceCode{asdasdasd asd asd }
                                       
This input will be discarded. Hit <return> to continue.

如果在这里阅读该帖子环境铸造 我在 miktex 2.9 中使用 xelatex,但这是唯一的解决方案吗?

这里有一种可能性:

这就是在包中解释的用于创建新环境的命令,可能应该使用这个代码!!

\newcommand\newminted[3][]{
      \ifthenelse{\equal{#1}{}}
       {\def\minted@envname{#2code}}
       {\def\minted@envname{#1}}
      \newenvironment{\minted@envname}
       {\VerbatimEnvironment\begin{minted}[#3]{#2}}
       {\end{minted}}
      \newenvironment{\minted@envname *}[1]
       {\VerbatimEnvironment\begin{minted}[#3,##1]{#2}}
       {\end{minted}}}

有人知道为什么这有效吗? 主要区别是什么? 为什么我的不起作用?

多谢!!

答案1

请参阅 Martin 的评论,了解您的方法不起作用的原因。请参阅minted手册中的“定义快捷方式”下的命令,该\newminted命令可执行您想要的操作(但通过环境而不是命令):

\newminted[CppSourceCode]{c++}{linenos=true, mathescape, xleftmargin=1cm}

允许

\begin{CppSourceCode}
    c++ ; // goes here
\end{CppSourceCode}

如果您更喜欢命令,请使用\newmint- 有关详细信息,请参阅手册。

相关内容