# Expand placeins functonality to subsections [closed]

I'm using the placeins package to ensure that floats stay within their respective sections (using \usepackage[section]{placeins}).

I'd like to expand this functionality to include subsections, without me having to manually add a \FloatBarrier each time.

I found this answer while looking online, however, I don't want to add the extra package to my distribution, as it will not get updated and might break in the future.

I fumbled around in the file, and found that this part of the .sty file does what I want:

\AtBeginDocument{%
\expandafter\renewcommand\expandafter\subsection\expandafter
{\expandafter\@fb@subsecFB\subsection}%
\newcommand\@fb@subsecFB{\FloatBarrier
\gdef\@fb@afterHHook{\@fb@topbarrier \gdef\@fb@afterHHook{}}}
\gdef\@fb@afterHHook{}
}


i.e. it appends \FloatBarrier to the subsection. However, placing this piece of code right after \usepackage[section]{placeins} doesn't seem to work.

I'm guessing there is more to LaTeX command syntax than what I've written and that I'm not getting it right, but my knowledge of LaTeX does not extend far enough to debug this on my own. Anyone willing to help?

Here's a MWE:

\documentclass{report}

\usepackage[section]{placeins}
\AtBeginDocument{%
\expandafter\renewcommand\expandafter\subsection\expandafter
{\expandafter\@fb@subsecFB\subsection}%
\newcommand\@fb@subsecFB{\FloatBarrier
\gdef\@fb@afterHHook{\@fb@topbarrier \gdef\@fb@afterHHook{}}}
\gdef\@fb@afterHHook{}
}
\begin{document}

\section{Section 1}

\begin{figure}
\centering
\Large A
\caption{First figure}
\end{figure}

\subsection{Subsection 1.1}

\begin{figure}
\centering
\Large B
\caption{Subsection 1.1 figure}
\end{figure}

\subsection{Subsection 1.1}

\begin{figure}
\centering
\Large C
\caption{Subsection 1.2 figure}
\end{figure}

\section{Section 2}

\end{document}

-

## closed as too localized by lockstep, Werner, Tom Bombadil, zeroth, yo'Sep 26 '12 at 20:14

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

You must put \makeatletter before the \AtBeginDocument so that LaTeX recognize that the @ (at) is meant to be a letter and so is a valid part of the command names. Revert this change by putting \makeatother after the definition. (I didn't check if the code itself works). –  Ulrike Fischer Sep 5 '12 at 10:14
@UlrikeFischer yep that did the trick! Thanks –  Kpantzas Sep 5 '12 at 12:01