# Recreating a renewenvironment using the \renewenvironment is creating a side effect!

I have a buildin environment in a module theorems-ams-bytype.inc (in lyx) example, which i copied and created a duplicate with required changes for a environment called as 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\captionslang{\renewcommand{\pitfallname}{_(Pitfall)}}
EndBabelPreamble
LabelCounter   pitfall
End


Now I add some nice decorations to this environment using the renewenvironment command. The code for this redefinition of the environment is as follows.

% for pitfall
\mdfdefinestyle{pitfallstyle}{%
linecolor=gray,middlelinewidth=7pt,%
frametitlerule=false,%
% 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}
}


Now the trouble is if I don't use \begin{pitfall} ... \end{pitfall} in the entire document then there is an error. and the error is below.

Kindly help me understand.

-
You should show a complete (but minimal) LaTeX document from \documentclass to \end{document} that shows the problem. –  egreg Oct 13 '12 at 8:51

I believe that this is a timing problem; in any case, doing \let\origpitfall=\pitfall may hide a, well, pitfall.

You should be able to solve the problem by giving another name to the inner environment, say

\theoremstyle{definition}
\newtheorem{pitfallx}{\protect\pitfallname}[chapter]
\renewcommand{\thepitfallx}{\arabic{pitfallx}}
\providecommand{\pitfallname}{_(Pitfall)}


and define the "real" pitfall environment in terms of pitfallx (that you won't be ever using directly):

\mdfdefinestyle{pitfallstyle}{%
linecolor=gray,middlelinewidth=7pt,%
frametitlerule=false,%
% 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}}

-