32

I would like to place figures in my document in certain subsections. Normally I would achieve this with float using [H]. But this would mean, that the figures are at fixed points in my text and sometimes this just doesn't look good.

I already found this question: How do I ensure that figures appear in the section they're associated with?

How can I tell LaTeX use subsections as float barriers instead of sections.

  • My eyes keep wanting to pronounce placeins like proteins. :) – Kaz Jun 11 '13 at 20:29
21

The placeins package, with the section option, redefines \section. You might succeed with \usepackage{placeins} and

\makeatletter
\AtBeginDocument{%
  \expandafter\renewcommand\expandafter\subsection\expandafter
    {\expandafter\@fb@secFB\subsection}%
  \newcommand\@fb@secFB{\FloatBarrier
    \gdef\@fb@afterHHook{\@fb@topbarrier \gdef\@fb@afterHHook{}}}%
  \g@addto@macro\@afterheading{\@fb@afterHHook}%
  \gdef\@fb@afterHHook{}%
}
\makeatother

This will do the same the section option does, but for \subsection instead of \section.

Untested. On a side note, don't use [H]: it's a sure cause for problems in placing floats.

| improve this answer | |
  • This solves my problem with the positioning, but it changes all my section/subsection numbering. – user31386 Jun 11 '13 at 13:00
  • @DaniWho Sorry, I missed doing a change. Fixed. The \section command should have been \subsection. – egreg Jun 11 '13 at 13:04
  • Note that this solution eliminates the \FloatBarrier in \section! It can happen that a float ends between a section title and its first subsection. To avoid this, you need to redefine both. – Luis Jul 29 '16 at 17:43
  • this can clearly be used also for subsubsection – Alessandro Cuttin Oct 10 '18 at 15:29
  • 1
    @skan That happens if you call \usepackage[section]{placeins}. The answer tells you to call \usepackage{placeins}. – egreg Aug 18 at 20:16
41

If \subsection implicitely calls \FloatBarrier, then also \section should do it:

\usepackage[section]{placeins}

The following adds the implicit \FloatBarrier to \subsection:

\makeatletter
\AtBeginDocument{%
  \expandafter\renewcommand\expandafter\subsection\expandafter{%
    \expandafter\@fb@secFB\subsection
  }%
}
\makeatother
| improve this answer | |
  • Don't you mean "If \section implicitely calls \FloatBarrier, then also \subsection should do it"? – StrawberryFieldsForever Nov 4 '17 at 21:01
  • 2
    @StrawberryFieldsForever No. – Heiko Oberdiek Nov 4 '17 at 21:17
  • I'm wondering whether or not we actually need all these expandafter commands... – Luc Evertzen Apr 24 at 12:13
  • 1
    @LucEvertzen You will get an infinite recursion without \expandafter, when \subsection is called. – Heiko Oberdiek Apr 30 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy