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{}}}
\g@addto@macro\@afterheading{\@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{}}}
\g@addto@macro\@afterheading{\@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}
\makeatletterbefore the\AtBeginDocumentso 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\makeatotherafter the definition. (I didn't check if the code itself works). – Ulrike Fischer Sep 5 '12 at 10:14