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

Mico
  • 506,678
Caridorc
  • 123
  • 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 Mar 05 '22 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 Mar 14 '22 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 Mar 14 '22 at 14:38
  • 1
    I don't think it does you any good to issue \FloatBarrier instructions after \section, \subsection, and \subsubsection. – Mico Mar 14 '22 at 14:48

2 Answers2

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.

David Carlisle
  • 757,742
  • 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 Mar 05 '22 at 12:16
  • Yes I meant the placeins package, thanks a lot for your answer. I also found another relevant question for this problem: https://tex.stackexchange.com/questions/118662/use-placeins-for-subsections – Caridorc Mar 05 '22 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... – David Carlisle Mar 05 '22 at 12:37
  • @DavidCarlisle thanks, sorry I wanted to avoid an overly long title – Caridorc Mar 06 '22 at 18:20
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.

Mico
  • 506,678