etoolbox: AtBeginEnvironment is not At Begin Environment

The etoolbox package redefines \begin so that

  1. \@beforebegin@<envname>@hook is executed
  2. a group is started
  3. the usual bookkeeping is performed (setting \@currenvir, for instance)
  4. \@begin@<envname>@hook is executed
  5. the macro \<envname> is executed

Thus what you get is

\@beforebegin@table@hook
\begingroup
<bookkeeping>
\@begin@table@hook
\table

The macro \table is responsible for starting the creation of a float and expands to \@float{table}.

Thus the tokens AtBeginEnvironment\par and BeforeBeginEnvironment\par are seen and the paragraphs are typeset before the float is started.

To the contrary, \end is redefined to execute the \AtEndEnvironment tokens before \end<envname> is executed.

This should explain the output you get.

There is a hook for doing things when the float has already begun and is \@floatboxreset.

I would be wary of using the hooks for adding text: they're meant for additional setup.

Tags:

Etoolbox