![使用 \renewenvironment 重新创建更新环境会产生副作用!](https://linux22.com/image/256878/%E4%BD%BF%E7%94%A8%20%5Crenewenvironment%20%E9%87%8D%E6%96%B0%E5%88%9B%E5%BB%BA%E6%9B%B4%E6%96%B0%E7%8E%AF%E5%A2%83%E4%BC%9A%E4%BA%A7%E7%94%9F%E5%89%AF%E4%BD%9C%E7%94%A8%EF%BC%81.png)
我在一个模块(在 lyx 中)示例中有一个构建环境theorems-ams-bytype.inc
,我复制了该环境并为名为 的环境创建了一个具有所需更改的副本pitfall
。
Style Pitfall
CopyStyle Definition
LatexName pitfall
LabelString "Pitfall \thepitfall."
Preamble
\theoremstyle{definition}
\newtheorem{pitfall}{\protect\pitfallname}[chapter]
\renewcommand{\thepitfall}{\arabic{pitfall}} % this will number the pitfall number as normal 1,2, etc while if u remove this line then it would be chapterno.pitfall number.
EndPreamble
Requires amsthm
LangPreamble
\providecommand{\pitfallname}{_(Pitfall)}
EndLangPreamble
BabelPreamble
\addto\captions$$lang{\renewcommand{\pitfallname}{_(Pitfall)}}
EndBabelPreamble
LabelCounter pitfall
End
现在我使用 renewenvironment 命令为这个环境添加一些漂亮的装饰。重新定义环境的代码如下。
% for pitfall
\mdfdefinestyle{pitfallstyle}{%
linecolor=gray,middlelinewidth=7pt,%
frametitlerule=false,%
%tikzsetting={shading=axis,top color=gray!10},
% apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
%shade,left color=white, right color=gray!20}}}, frametitlerulecolor=gray!60,
roundcorner=1pt,
splittopskip=1cm,
splitbottomskip=1cm,
bottomline=false,
leftline=false,
rightline=false,
backgroundcolor=gray!10,
frametitlerulewidth=0pt,
innertopmargin=\topskip, }
% changes in pitfall environment
\let\origpitfall\pitfall
\let\endorigpitfall\endpitfall
\renewenvironment{pitfall}{%
% here you can make changes in the original pitfall class
\begin{mdframed}[style=pitfallstyle] \origpitfall }{
\endorigpitfall
\end{mdframed}
}
现在的问题是,如果我不在\begin{pitfall} ... \end{pitfall}
整个文档中使用,则会出现错误。错误如下。
请帮助我理解。
答案1
我认为这是一个时机问题;无论如何,这样做\let\origpitfall=\pitfall
可能会隐藏一个陷阱。
你应该能够通过给出其他名字到内部环境,说
\theoremstyle{definition}
\newtheorem{pitfallx}{\protect\pitfallname}[chapter]
\renewcommand{\thepitfallx}{\arabic{pitfallx}}
\providecommand{\pitfallname}{_(Pitfall)}
pitfall
并根据以下内容定义“真实”环境pitfallx
(您永远不会直接使用):
\mdfdefinestyle{pitfallstyle}{%
linecolor=gray,middlelinewidth=7pt,%
frametitlerule=false,%
%tikzsetting={shading=axis,top color=gray!10},
% apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
%shade,left color=white, right color=gray!20}}}, frametitlerulecolor=gray!60,
roundcorner=1pt,
splittopskip=1cm,
splitbottomskip=1cm,
bottomline=false,
leftline=false,
rightline=false,
backgroundcolor=gray!10,
frametitlerulewidth=0pt,
innertopmargin=\topskip, }
% changes in pitfall environment
\newenvironment{pitfall}
{\begin{mdframed}[style=pitfallstyle]\pitfallx}
{\endpitfallx\end{mdframed}}