Floats – How to Automatically Split Subfigure Over Multiple Pages for Proper Pagination

floatssubfig

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}}
\qquad
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\qquad
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\qquad
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\qquad
\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}}
\qquad
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\qquad
\subfloat[]{\label{fig:1}\includegraphics[width=0.43\textwidth]{fig1}}
\qquad
\subfloat[]{\label{fig:2}\includegraphics[width=0.43\textwidth]{fig2}}
\qquad
\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}

Best Answer

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}}%
  \setbox3=\vbox{\addtocounter{figure}{-1}\caption{#2}}%
  \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}
Related Question