2

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
4
  • Welcome to TeX.SE. I can understand you desire not to let floats cross section-level divisions. But subsection-level and subsubsection-level divisions too? Please clarify you use case.
    – Mico
    Commented Mar 5, 2022 at 12:20
  • @Mico yes I need not crossing subsection-level and subsubsection-level divisions because my use case is very long and detailed technical reports with many graphs.
    – Caridorc
    Commented Mar 14, 2022 at 13:48
  • Thanks for explaining your use case in more detail. It helped me formulate an answer, which I posted a few minutes ago.
    – Mico
    Commented Mar 14, 2022 at 14:38
  • 1
    I don't think it does you any good to issue \FloatBarrier instructions after \section, \subsection, and \subsubsection.
    – Mico
    Commented Mar 14, 2022 at 14:48

2 Answers 2

2

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.

3
  • should the second and third ones be subsection and subsubsection?
    – Caridorc
    Commented Mar 17, 2022 at 12:11
  • 1
    @Caridorc - Absolutely! Let me fix that right away.
    – Mico
    Commented Mar 17, 2022 at 12:42
  • Thanks a lot @Mico for your help!
    – Caridorc
    Commented Mar 18, 2022 at 8:49
3

You have not said but I assume you mean the command from the placeins package, in which case \usepackage[section]{placeins} does what you need.

A lat this dos what is asked for in the title, for sections, it does not automatically do the same for subsections, but forcing page breaks at the lower order headings isn't usually desirable.

4
  • I wonder if you didn't answer too quickly. IIRC the option section adds Floatbarrier to section only and not to subsection, isn't it ?
    – Jhor
    Commented Mar 5, 2022 at 12:16
  • Yes I meant the placeins package, thanks a lot for your answer. I also found another relevant question for this problem: tex.stackexchange.com/questions/118662/…
    – Caridorc
    Commented Mar 5, 2022 at 12:19
  • 1
    @Jhor well I answered the question in the title, you are right that doesn't actually match the example code. I'll add a clarifying note... Commented Mar 5, 2022 at 12:37
  • @DavidCarlisle thanks, sorry I wanted to avoid an overly long title
    – Caridorc
    Commented Mar 6, 2022 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .