# Redefining environment within a macro

Hel-lo, I am a LaTeX programming newbie.

While formatting my document, I found that it will look nice if I can modify the theorem environment (with amsthm package). So I tried the following code

\makeatletter
\newcount\@envcount
\newcommand{\enclosebox}[2]{%
% Create copies of old environments
\expandafter\let\expandafter\@old\csname #1\endcsname%
\expandafter\let\expandafter\@oldend\csname end#1\endcsname%
% Redefine environment using old ones.
\renewenvironment{#1}[1][]{%
\@nameuse{@old}[##1]%
\begin{#2}%
}{%
\end{#2}%
\@nameuse{@oldend}%
}%
}
\makeatother


This command inserts a new environment (with name #2) to the original environment (with name #1). With this, I can modify theorem environments like this:

 \newtheorem{theorem}{Theorem}
\enclosebox{theorem}{<some box environment>}


The problem is, the token \@old and \@oldend are overwritten whenever I call \enclosebox command. For example,

 \enclosebox{theorem}{shadebox}
\enclosebox{example}{leftbar}


overwrites my old definition and theorem environment, resulting in something like:

So I tried using \csname ~ \endcsname to assign the name dynamically, but the compiler just spits out a bunch of errors.

Of course I can resolve this problem by pasting multiple copy of the code and modifying each one by hands... but I am doing in this way just to avoid this clumsy solution.

I have no idea how to fix my code. Can you help me?

-

## For a solution with the original framed package, see below

As an ready to use alternative, instead of using framed and doing all the work, I'd suggest you to use \surroundwithmdframed from the mdframed that was designed specifically for this purpose:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newtheorem{theorem}{Theorem}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\theoremstyle{remark}
\newtheorem{example}{Example}

\surroundwithmdframed[hidealllines=true,backgroundcolor=gray!30]{theorem}
\surroundwithmdframed[backgroundcolor=red,backgroundcolor=cyan!30]{definition}
\surroundwithmdframed[backgroundcolor=red,backgroundcolor=orange!30,roundcorner=10pt]{example}

\begin{document}

\begin{theorem}
\lipsum[4]
\end{theorem}

\begin{definition}
\lipsum[4]
\end{definition}

\begin{example}
\lipsum[4]
\end{example}

\end{document}


And here's a possibility using framed and the etoolbox package to easily define your command \enclosebox:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage{framed}
\usepackage{lipsum}

\newcommand\enclosebox[2]{%
\BeforeBeginEnvironment{#1}{\begin{#2}}%
\AfterEndEnvironment{#1}{\end{#2}}%
}

\newtheorem{theorem}{Theorem}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\theoremstyle{remark}
\newtheorem{example}{Example}

\enclosebox{theorem}{framed}
\enclosebox{definition}{leftbar}

\begin{document}

\begin{theorem}
\lipsum[4]
\end{theorem}

\begin{definition}
\lipsum[4]
\end{definition}

\begin{example}
\lipsum[4]
\end{example}

\end{document}


-
Thank you for suggesting such a nice package. It does fit into my problem. Actually, however, I also want to learn something new LaTeX programming skills so that eventually I can resolve other similar problems by my hand. If you don't mind, would you help me also in this way? (Or maybe I can look through the source code of that package...) –  sos440 Feb 13 '14 at 3:31
Thank you again! –  sos440 Feb 13 '14 at 3:36
@sos440 I added another approach to my answer; this time using framed and defining your command, with the help of the etoolbox package. If you want to, I could also add a manual solution, but I'm tired now, so it will be in some hours :-) –  Gonzalo Medina Feb 13 '14 at 3:36
@sos440 -- the recommendation to use mdframed is a good one. an attempt by the maintainers of amsthm to provide a facility like the one you ask for ended with a result in which equation numbers on the right extended outside the frame area. with the availability of mdframed, the attempt to provide a native solution were abandoned. –  barbara beeton Feb 13 '14 at 14:12