\let and sneaky redefinitions that are wrappers for the original macro: is there a similar thing for environments?
An environment foo
defines the two macros \foo
and \endfoo
. You can redefine both in the same as you already did:
\let\origfoo\foo
\let\endorigfoo\endfoo
\renewenvironment{foo}
{<your stuff>\origfoo<your stuff>}
{<your stuff>\endorigfoo<your stuff>}
However, using the package etoolbox
with its macros makes life easier.
The most practical way is to exploit etoolbox's utilities:
\preto\foo{\hspace{1em}}
\appto\foo{\hspace{1em}}
The problem arises for commands with "apparent arguments", for example \section
. If you look at the output of \show\section
, you'll see that it has no argument whatsoever. It's a technique frequently employed in LaTeX, because it's more efficient. Actually \section
is transformed into
\@startsection{...}{...}{...}{...}{...}
and \@startsection
has six arguments; you see that \section
supplied the first five, the sixth is what comes after \section
(there's more to this, because there can be a * or an optional argument, but let's keep it simple).
Using the trick with \appto
for a command like \section
would result in a disaster, because the thing added will provide the sixth argument for \@startsection
, which is clearly not what is wanted.
As Herbert says, you can use \preto
and \appto
also for environments, using \foo
and \endfoo
if the environment is foo
, provided the environment has no argument. If the argument is part of the command (i.e. it is not "apparent") one can use \pretocmd
and \apptocmd
; if the argument is apparent (it's the case of \tabular
, for example) \appto
and \apptocmd
must not be used, but \preto
and \pretocmd
are quite safe.
For environments, you can use \AtBeginEnvironment
, \AtEndEnvironment
, \BeforeBeginEnvironment
, and \AfterEndEnvironment
provided by etoolbox
. etoolbox
add some hooks for environments. You may also use \appto
and \preto
to patch \foo
and \endfoo
. The two methods work differently.
An example:
\documentclass{article}
\usepackage{etoolbox}
\newenvironment{foo}{(}{)}
\begin{document}
\begin{foo}foo\end{foo}
\BeforeBeginEnvironment{foo}{[}
\AfterEndEnvironment{foo}{]}
\begin{foo}foo\end{foo}
\appto\foo{\{}
\preto\endfoo{\}}
\begin{foo}foo\end{foo}
\end{document}