# Environment decorators

I'm looking for a way to write a "decorator" (in Python terminology) or a "wrapper" for LaTeX environments: a way to give addition behaviour with save of the notation. For example, before every \begin{quote} execute a code that will add a \hrule.

I could make it for a single command using \let:

\let\oldCommand\commandIWantToDecorate


That will execute <addition code> without the need to change the command throughout the document.

Is there a similar way to decorate the whole environment? Maybe LaTeX creates some inner macros I can work with?

UPD the question Can I redefine a command to contain itself? doesn't solve my problem, because I'm interested in redefining environments, not solo macros.

The etoolbox package partly solve the problem, thanks @samcarter!

But in "education" reasons I'm interesting is there a way to do decorate without additional packages, using just (La)TeX pre-build commands?

The LaTeX kernel provides \g@addto@macro which works for the environment starter command as well, i.e. for the environment quote the start macro is \quote, so say \g@addto@macro\quote{foo} etc.

No extra packages are needed for this, apart from the specific additions that are to be made, but that depends on personal choices of the O.P, but in general 'anything' can be placed in \g@addto@macro\quote{...}.

\let\oldquote\quote


would work as well, but this may fail for other environments that have optional arguments. In this case, \LetLtxMacro from letltxmacro package is needed, which means another package, however.

\documentclass{book}

\makeatletter
\medskip

\textit{be careful}

\medskip

Now the real stuff begins\dots

}
\makeatother

\begin{document}

\begin{quote}
test
\end{quote}

\end{document}


Have a look at the etoolbox package, in particular to its \AtBeginEnvironment command. This allows you to add aditional code to the begin of environment.

\documentclass{book}

\usepackage{etoolbox}
\AtBeginEnvironment{quote}{\hrule}

\begin{document}

\begin{quote}
test
\end{quote}

\end{document}