# Automatic splitting of subfigure over multiple pages

I have lots of subfigures that I want to split across multiple pages. These subfigures are of different sizes. I'm still editing my document so figures are being pushed around. I don't want to hard-wire where the page-break between the subfigures occurs.

How can I:

1. Automatically get a subfigure to start a new page when the current page is full?
2. ONLY add a caption when a new page is started?
3. Prevent other contents from going between the subfigures?

Here is an example of my current document. As you can see \ContinueFloat is hard-wired. Other contents are going between the first and last 5 subfigures.

\begin{figure}
\centering
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\caption{Here are the first 5 figures of a continued figure.}
\label{fig:cont}
\end{figure}

\begin{figure}
\ContinuedFloat
\centering
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\caption{Here are the last 5 figures of a continued figure.}
\label{fig:cont}
\end{figure}

• Since all the examples are 0.43\textwidth wide, it would be easier to break them into groups of 6 and 4. Dec 12, 2021 at 17:21
• 0.43\textwidth is just a mock up. The subfigures all have different aspect ratios.... Dec 12, 2021 at 17:34
• Does this answer your question? Split subfigures over multiple pages Dec 12, 2021 at 19:25

Interestingly, \subfloat seems to end with \ignorespaces, so you either need a blank line, \allowbreak or something else which allows a linebreak. All the subfloats are formatted in one go, so \ContinuedFloat is not needed.

I can turn this into an environment using udbox (see https://tex.stackexchange.com/questions/563109/render-captions-above-figures-with-caption-below-includegraphics-while-using-e/563241?r=SearchResults&s=2|22.2526#563241).

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{lipsum}

\let\bottomfraction=\topfraction
\let\floatpagefraction=\topfraction

\makeatletter
\newif\if@firstpageoffigure
\newcommand{\multipagefigure}[3]% #1 = caption, #2 = caption (cont.), #3 = contents
{\bgroup
\def\@captype{figure}% enable captions
\setbox2=\vbox{\caption{#1}}%
\setbox0=\vbox{#3}%
\@firstpageoffiguretrue
\loop\ifvoid0\else
\if@firstpageoffigure
\@firstpageoffigurefalse
\dimen0=\dimexpr \pagegoal-\pagetotal-\textfloatsep\relax
\ifdim\dimen0>\bottomfraction\textheight
\dimen0=\bottomfraction\textheight
\fi
\setbox1=\copy0% compute smallest possible figure
\setbox4=\vsplit1 to 0pt
\setbox1=\vbox{\unvbox4}% restore true size
\ifdim\dimen0<\dimexpr \ht1+\dp1+\ht2+\dp2\relax
\dimen0=\textheight
\fi
\setbox1=\vsplit0 to \dimexpr \dimen0-\ht2-\dp2\relax
\begin{figure}[bp]
\unvbox1
\unvbox2
\end{figure}
\else
\setbox1=\vsplit0 to \dimexpr \textheight-\ht3-\dp3\relax
\begin{figure}[tp]
\unvbox1
\usebox3
\end{figure}
\fi
\repeat
\egroup}
\makeatother

\begin{document}

\lipsum[1-2]

\multipagefigure{Every page\label{fig:cont}}{Every page (cont.)}{%
\centering
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:3}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:4}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:5}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:6}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:7}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:8}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:9}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:10}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:11}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:12}\includegraphics[width=0.43\textwidth]{example-image-b}}

\subfloat[]{\label{fig:13}\includegraphics[width=0.43\textwidth]{example-image-a}}\hfil
\subfloat[]{\label{fig:14}\includegraphics[width=0.43\textwidth]{example-image-b}}
}
\lipsum[3-6]

\end{document}

• This works wonderful! One minor warning I am getting: Label 'fig:cont' multiply defined. You have used the same label more than once. Check that each \label{...} labels only one item. Any idea how to get rid of this? Dec 13, 2021 at 16:08
• For my own future reference. I edited your solution a bit to use a different caption (cont.) from page2+. Also worked around a repeated label warning Dec 13, 2021 at 17:31
• I made a few changes to the first page. Even if you have a completely empty page, you cannot put a [p] float until the next page. OTOH, if there is very little space left on the current page, you might as well wait and use the whole page. Dec 15, 2021 at 16:18