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
\g@addto@macro\@floatboxreset{\londonrob@floatcode{\@captype}}
\newcommand\londonrob@floatcode[1]{\@nameuse{londonrob@#1code}}
\newcommand\londonrob@figurecode{WHATEVER}
% similarly for \londonrob@tablecode if needed
\makeatother
Full example
\documentclass{article}
\usepackage{lipsum}
\makeatletter
\g@addto@macro\@floatboxreset{\londonrob@floatcode{\@captype}}
\newcommand\londonrob@floatcode[1]{\@nameuse{londonrob@#1code}}
\newcommand\londonrob@figurecode{WHATEVER}
% similarly for \londonrob@tablecode 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.