Floats – Automatically Insert FloatBarrier Before and After Each Section

floatsplaceinssectioning

I am looking for a command like:

\alwaysFloatBarrier

Such that whenever I write a normal section or subsection or subsubsection:

\section{Example 1}

\subsection{Example 2}

\subsubsection{Example 3}

They get automatically converted to:

\FloatBarrier
\section{Example 1}
\FloatBarrier

\FloatBarrier
\subsection{Example 2}
\FloatBarrier

\FloatBarrier
\subsubsection{Example 3}
\FloatBarrier

Best Answer

You could achieve your objective by loading the placeins package (for the \FloatBarrier macro) along with the etoolbox package (for the \pretocmd macro) and issuing the instructions

\pretocmd{\section}{\FloatBarrier}{}{}
\pretocmd{\subsection}{\FloatBarrier}{}{}
\pretocmd{\subsubsection}{\FloatBarrier}{}{}

in the preamble. If you need this in macro form, you could write

\newcommand\alwaysFloatBarrier{%
   \pretocmd{\section}{\FloatBarrier}{}{}%
   \pretocmd{\subsection}{\FloatBarrier}{}{}%
   \pretocmd{\subsubsection}{\FloatBarrier}{}{}%
}

and then issue the instruction \alwaysFloatBarrier.