I'm trying to redefine an environment in order to add a command after the \begin without having to add this command in manually everywhere.

For example, I'd like to replace every instance of \begin{figure} with \begin{figure}\foo where \foo is a function I've defined earlier:

\newcommand{foo}{This is foobar}

If I simply do:


then this creates a recursive definition of the figure environment. This answer suggests using an intermediate command to get past the recursion problem when using \renewcommand.

But how can I get past the recursion problem when using \renewenvironment?

  • It works basically the same... – user31729 Apr 1 '16 at 17:21
  • Related (maybe duplicate): tex.stackexchange.com/questions/116670/duplicating-environments. You need to save original definitions, and redefine the environment in terms of the saved components. – Steven B. Segletes Apr 1 '16 at 17:22
  • @StevenB.Segletes yes, this answer will be used to solve my problem. The fact I'm currently struggling to work out exactly how to apply it to my problem suggests it's not quite a duplicate. – LondonRob Apr 1 '16 at 17:25

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.

% similarly for \londonrob@tablecode if needed

Full example



% similarly for \londonrob@tablecode if needed




Something else

\caption{A caption}




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

enter image description here

  • Ouch. I never expected to have to get in this deep. There's a lot to learn in this example. Could you mention what the exact pitfalls of the \let\temp\figure-approach are? It looks to me as if redefining figure in this way works ok. – LondonRob Apr 1 '16 at 18:11
  • 2
    @LondonRob \figure expands to \@float{figure} and this in turn looks for the optional argument in quite an indirect fashion. If you say \let\oldfigure\figure and then \renewcommand{\figure}{\oldfigure\foo} the optional argument will never be found. – egreg Apr 1 '16 at 18:14

If you need to add something to the figure environment, you can try this:

\def\foo{This is foobar}
  • Sorry, no, it can't work; \figure expands to \@float{figure} and \@float does a check for the optional argument. – egreg Apr 1 '16 at 17:42
  • It works. That is all. – wipet Apr 1 '16 at 18:14
  • Really? Try \begin{figure}[htp]. – egreg Apr 1 '16 at 19:25
  • 1
    That's exactly the point: such a substitution would be completely useless. Question: “Can I jump off the tour Eiffel?” Your answer: “Yes, just jump”. My answer: “Get a parachute before jumping.”. – egreg Apr 1 '16 at 19:37
  • 1
    The parachute is very dangerous when you are jumping off the Eiffel. There is no time to open it. – wipet Apr 2 '16 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.