How can I detect if an environment is right after a sectioning command?

I'd like to build an environment that would detect if it is right after a sectioning command (\section, \subsection or \subsubsection) and smart enough to detect if a float-type environment (say figure) has been inserted between the sectioning command and itself. Here is a sketch of what I have in mind (written for the article class, my real case is with the book class, but I don't think it matters that much).

\documentclass{article}
\newif\ifRightAfterASectioningCommand
\newenvironment{myenv}{%
% Some detection mechanism
% that sets RightAfterASectioningCommand
% to true or false
% ...
\ifRightAfterASectioningCommand
Right after a sectioning command
\else
Not right after a sectioning command
\fi
}{}
\begin{document}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\section{Section}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsection{Subsection}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsubsection{Subsubsection}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\subsubsection{Subsubsection}

\begin{figure}
\end{figure}

\begin{myenv}
% RightAfterASectioningCommand is true
\end{myenv}

\begin{myenv}
% RightAfterASectioningCommand is false
\end{myenv}

\end{document}

-
\ifRightAfterASectioningCommand==\if@nobreak –  egreg May 16 '13 at 10:31

All sectional command globally set the internal conditional \if@nobreak to true. This conditional is reset to false as soon as a paragraph is started. However, floats would change it only if really typeset at the spot. The rationale is that a float appearing immediately after a sectional title counts as the first paragraph after the title.

You probably will be satisfied with

\makeatletter
\newenvironment{myenv}{%
% Some detection mechanism
% that sets RightAfterASectioningCommand
% to true or false
% ...
\if@nobreak
Right after a sectioning command
\else
Not right after a sectioning command
\fi
}{}
\makeatother

-