[Tex/LaTex] Prevent pagebreak between section header and figure

floatspage-breakingsectioning

In a document, I have an appendix with several screenshots, i.e., the different figures are displayed without any descriptive text right after the section header.

I notice, however, that in several cases, the figure environment and the header section are separated on different pages. I would like to avoid this, i.e., ideally, the header should then be moved to the next page.

Is there a generic solution for this? Ideally, I would have to renew the command for the section header, so that figures (or other blocks) and headers are not separated. I am stuck at this point though

Mini working example:

\documentclass[]{article}

\usepackage[centerlast]{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{tikz}

\title{}

\begin{document}

\maketitle

\section{Section 1}
%
\begin{figure}[tbh]
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
    \caption{Front and Back Side of the Device}
\end{figure}
%
\FloatBarrier
%
\section{Section 2}
%
\begin{figure}[tbh]
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
        \fbox{
            \includegraphics[width=0.48\textwidth]{example-image-b}
        }
    \caption{Main Interface of the Device}
\end{figure}
%
\FloatBarrier

\end{document}

Any suggestions or help with this matter are greatly appreciated.

Best Answer

For such an appendix you don't really want the figures to float at all so rather than specifying that they float and hoping they don't, use [H] to tell latex not to float them, then the usual rules about keeping section headings with teh first text in the section will kick in.

I also added some % to ends of lines to avoid the overfull box warnings in the original.

\documentclass[]{article}

\usepackage[centerlast]{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{tikz}

\title{}

\begin{document}

\maketitle

\section{Section 1}
%
\begin{figure}[H]
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }
    \caption{Front and Back Side of the Device}
\end{figure}
%
\FloatBarrier
%
\section{Section 2}
%
\begin{figure}[H]
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }\hfill
        \fbox{%
            \includegraphics[width=0.48\textwidth]{example-image-b}%
        }
    \caption{Main Interface of the Device}
\end{figure}
%
\FloatBarrier

\end{document}