I am trying to move a subsection to the next page if the complete subsection (title, the text and the floats) do not fit on the same page.
I don't want the figure to appear before the text.
I tried multiple approaches but I don't even get it to work in this simple example.
- minipage to group the complete subsection
- Using
filbreak
beforesubsection
- rewriting subsection command with obscure stuff I did not understand
So what is a working way to do it and what is a recommended way to do it?
EDIT: To be more clear. I want Subsection 2
moved to the next page, so that it is on the same page as the figures which belong into it. Because they fit on one page, they should be on one page.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{subcaption}
\title{SubsectionFloats}
\date{December 2022}
\begin{document}
\maketitle
\section{Introduction}
\subsection{Subsection 1}
\lipsum[2]
%\newpage --------------- Here a new page should begin since subsection does fit on a single page but would be split by a pagebreak between text and figure if no newpage command is used.
\subsection{Subsection 2}
\lipsum[5]
\begin{figure}
\begin{subfigure}{\textwidth}
\centering
\includegraphics[scale=0.5]{example-image-c}
\caption{Caption1}
\label{fig:my_label}
\end{subfigure}
\begin{subfigure}{\textwidth}
\centering
\includegraphics[scale=0.5]{example-image-c}
\caption{Caption2}
\label{fig:my_label2}
\end{subfigure}
\centering
\caption{Figures}
\end{figure}
\end{document}
\includegraphics
in place, and use\captionof
.