# Avoid recursion in \renewenvironment

Patching existing environments or commands is almost a black art. One has to know in detail how the environment or command works.

Even saving a copy of \figure the newly defined environment will not work like the original one, particularly with respect to optional arguments.

The best place where placing code to be executed at every figure is in \@floatboxreset.

However, since you want to do it only for figure, there's something more to do.

\makeatletter
\[email protected]@macro\@floatboxreset{\[email protected]{\@captype}}
\newcommand\[email protected][1]{\@nameuse{[email protected]#1code}}
\newcommand\[email protected]{WHATEVER}
% similarly for \[email protected] if needed
\makeatother


Full example

\documentclass{article}

\usepackage{lipsum}

\makeatletter
\[email protected]@macro\@floatboxreset{\[email protected]{\@captype}}
\newcommand\[email protected][1]{\@nameuse{[email protected]#1code}}
\newcommand\[email protected]{WHATEVER}
% similarly for \[email protected] if needed
\makeatother

\begin{document}

\lipsum[2]

\begin{figure}[htp]

Something else

\caption{A caption}

\end{figure}

\lipsum[3]

\end{document}


Of course, in place of WHATEVER you'll add the code you want to be executed.