Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

closed as too localized by lockstep, Werner, Tom Bombadil, zeroth, tohecz 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.

1  
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

Browse other questions tagged or ask your own question.