无法使用 fancyvrb 将可选参数传递给 \newenvironment

无法使用 fancyvrb 将可选参数传递给 \newenvironment

我正在尝试定义一个基于环境script构建的新环境。fancyvrbVerbatim

前言:

\usepackage{fancyvrb}
\newenvironment{script}[1][firstnumber=1]
               {\VerbatimEnvironment\begin{Verbatim}
               [frame=single,numbers=left,framerule=0.4mm,{#1}]}
               {\end{Verbatim}}

script环境的使用方式如下:

\begin{script}
(1st line of code)
(2nd line of code)
\end{script}

some text

\begin{script}[firstnumber=3]
(3rd line of code)
\end{script}

但这无法编译,并产生以下错误消息:

! Illegal parameter number in definition of \@tempa.
<to be read again> 
                    
l.6846 ... function is needed to show intermediate
                                                  
! FancyVerb Error:
  Extraneous input `## the 'print' function is needed to show intermediate' bet
ween \begin{script}[<key=value>] and line end
.

我的代码中是否存在错误或者缺陷fancyvrb

答案1

由于您想要定义逐字环境,请使用适当的命令:

\documentclass{article}
\usepackage{fvextra}

\DefineVerbatimEnvironment{script}{Verbatim}{
  frame=single,
  numbers=left,
  framerule=0.4mm,
  firstnumber=1,
}

\begin{document}

\begin{script}
(1st line of code)
(2nd line of code)
\end{script}

some text

\begin{script}[firstnumber=3]
(3rd line of code)
\end{script}

\end{document}

在此处输入图片描述

答案2

我应该更仔细地阅读文档fancyvrb。以下是解决方案(序言):

\DefineVerbatimEnvironment{script}{Verbatim}
               {frame=single,numbers=left,framerule=0.4mm}

相关内容