%%
%% This is file `fewerfloatpages.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% fewerfloatpages.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright 2019-2020 Frank Mittelbach
%% 
%% This file was generated from file(s) of the LaTeX `fewerfloatpages Bundle'.
%% --------------------------------------------------------------------------
%% 
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%% 
%% This file may only be distributed together with a copy of the LaTeX
%% `fewerfloatpages Bundle'. You may however distribute the `fewerfloatpages Bundle'
%% without such generated files.
%% 
%% The newest sources can be found below
%% 
%%    https://github.com/FrankMittelbach/fmitex/
%% 
%% where one can also log issues in case there are any.
%% 
%% File: fewerfloatpages.dtx (C) Copyright 2019-2020 Frank Mittelbach

\def\fewerfloatpagesdate   {2020/02/14}
\def\fewerfloatpagesversion{v1.0a}



\NeedsTeXFormat{LaTeX2e}[2018-04-01]
\ProvidesPackage{fewerfloatpages}
                [\fewerfloatpagesdate\space \fewerfloatpagesversion\space
                 improve float page generation (FMi)]
\DeclareOption{trace}
              {\providecommand\fl@trace[1]%
                {{\let\@elt\@empty\typeout{fewerfloatpages: #1}}}}
\def\fp@strategy{0}%
\DeclareOption{nocheck}{\def\fp@strategy{0}}  % better name?
\DeclareOption{addbang}{\def\fp@strategy{1}}
\DeclareOption{checktb}{\def\fp@strategy{2}}
\ExecuteOptions{checktb}
\ProcessOptions
\providecommand\fl@trace[1]{}
\newcommand\floatpagekeepfraction{\textfraction}

\newcounter{floatpagedeferlimit}  \setcounter{floatpagedeferlimit}{3}

\newcounter{floatpagekeeplimit}   \setcounter{floatpagekeeplimit}{3}


\AtBeginDocument{%
\def \@tryfcolumn #1{%
  \global \@fcolmadefalse
  \ifx #1\@empty
  \else
    \fl@trace{PAGE: trying to make a float
                    \if@twocolumn column/page\else page\fi}%
    \fl@trace{----- \string #1: #1}%
    \xdef\@trylist{#1}%
    \global \let \@failedlist \@empty
    \begingroup
      \let \@elt \@xtryfc \@trylist
    \endgroup
    \if@fcolmade
      \fp@candidates\z@
      \def\@elt##1{\advance\fp@candidates\@ne}%
        #1%
      \let\@elt\relax
      \ifnum \fp@candidates >\c@floatpagedeferlimit
        \fl@trace{----- too many deferred floats for unraveling
                   (\the\fp@candidates\space> \the\c@floatpagedeferlimit)}%
      \else
        \global\@fcolmadefalse
        \fp@candidates\z@
        \let\@elt\fp@analyse@floats@for@unraveling
          \@flsucceed
        \let\@elt\relax
        \if@fcolmade  \else
          \@tempdima\floatpagekeepfraction\@colht
          \ifdim \fp@unused@space >\@tempdima
            \fl@trace{----- current float page unraveled^^J%
                      \@spaces\@spaces\@spaces\space\space\space
                      (free space \fp@unused@space\space > \the\@tempdima)}%
            \xdef #1{\@failedlist\@flsucceed\@flfail}%
            \let\@elt\fp@maybe@add@bang
              \@flsucceed
            \let\@elt\relax
          \else
            \global \@fcolmadetrue
            \fl@trace{----- current float page kept, full enough^^J%
                      \@spaces\@spaces\@spaces\space\space\space
                      (free space \fp@unused@space\space < \the\@tempdima)}%
          \fi
        \fi
      \fi
    \else
      \fl@trace{ --> fail: no float page made}%
    \fi
    \if@fcolmade
        \@vtryfc #1%
    \fi
  \fi}%
}% -- END of \AtBeginDocument
\AtBeginDocument{%
\def\@makefcolumn #1{%
  \begingroup
    \@fpmin -\maxdimen
    \let \@testfp \@gobble
    \global \@fcolmadefalse
    \ifx #1\@empty
    \else
      \fl@trace{PAGE: trying to make a float
                      \if@twocolumn column/page\else page\fi}%
      \fl@trace{----- \string #1: #1}%
      \xdef\@trylist{#1}%
      \global \let \@failedlist \@empty
      \begingroup
        \let \@elt \@xtryfc \@trylist
      \endgroup
      \if@fcolmade
        \@vtryfc #1%
      \fi
    \fi
  \endgroup
}%
}% -- END of \AtBeginDocument
\def\@xtryfc #1{%
  \fl@trace{ starting with \string#1}%
  \@next\reserved@a\@trylist{}{}%
  \@currtype \count #1%
  \divide\@currtype\@xxxii
  \multiply\@currtype\@xxxii
  \@bitor \@currtype \@failedlist
  \@testfp #1%
  \@testwrongwidth #1%
  \ifdim \ht #1>\@colht
     \@testtrue
  \fi
  \if@test
    \@cons\@failedlist #1%
    \fl@trace{ --> fail}%
  \else
    \@ytryfc #1%
  \fi
}%
\def\@ytryfc #1{%
  \begingroup
    \gdef\@flsucceed{\@elt #1}%
    \global\let\@flfail\@empty
    \@tempdima\ht #1%
    \let\@elt\@ztryfc
    \@trylist
    \ifdim \@tempdima >\@fpmin
      \global\@fcolmadetrue
      \@tempdimb\@colht
      \advance\@tempdimb-\@tempdima
      \xdef\fp@unused@space{\the\@tempdimb}%
    \else
      \@cons\@failedlist #1%
      \fl@trace{ --> fail}%
    \fi
  \endgroup
  \if@fcolmade
    \let\@elt\@gobble
    \fl@trace{ --> success: \@flsucceed}%
  \fi}
\def \@largefloatcheck{%
  \ifdim \ht\@currbox>\textheight
    \@tempdima -\textheight
    \advance \@tempdima \ht\@currbox
    \@latex@warning {Float too large for page by \the\@tempdima}%
    \ht\@currbox \textheight
  \fi
  \fp@maybe@check@tb
}
\newcount\fp@candidates
\def\fp@unused@space{}
\def\fp@analyse@floats@for@unraveling#1{%
  \advance\fp@candidates\@ne
  \ifnum \fp@candidates <\c@floatpagekeeplimit
    \@tempcntb\count#1%
    \divide\@tempcntb 8\relax
    \multiply\@tempcntb 8\relax
    \ifnum \count#1=\@tempcntb
      \global \@fcolmadetrue
      \fl@trace{----- current float page kept, contains a float}%
      \fl@trace{\@spaces\space\space with p but no t or b specifier}%
      \let\@elt\@gobble
    \fi
  \else
    \global \@fcolmadetrue
    \fl@trace{----- current float page kept
              (contains at least \the\fp@candidates\space floats)}%
    \let\@elt\@gobble
  \fi
}
\ifnum\fp@strategy=1
  \def\fp@maybe@add@bang#1{%
    \@boxfpsbit #1\sixt@@n
    \ifodd \@tempcnta
      \global\advance\count#1-\sixt@@n
    \fi
  }
\else
  \let\fp@maybe@add@bang\@gobble
\fi
\ifnum\fp@strategy=2
  \def\fp@maybe@check@tb{%
    \@getfpsbit \sixt@@n
    \ifodd \@tempcnta
      \ifdim \ht\@currbox>\topfraction\textheight
        \@getfpsbit \tw@
        \ifodd \@tempcnta
          \global\advance\count\@currbox -\tw@
          \fp@add@p@bit
          \@latex@warning {Float too large for top area: t changed to p}%
        \fi
      \fi
      \ifdim \ht\@currbox>\bottomfraction\textheight
        \@getfpsbit 4\relax
        \ifodd \@tempcnta
          \global\advance\count\@currbox -4\relax
          \fp@add@p@bit
          \@latex@warning {Float too large for bottom area:
                           b changed to p}%
        \fi
      \fi
    \fi
  }
\else \let\fp@maybe@check@tb\relax \fi
\def\fp@add@p@bit{%
   \@getfpsbit 8\relax
   \ifodd \@tempcnta \else \global\advance\count\@currbox 8\relax \fi}

\endinput
%%
%% End of file `fewerfloatpages.sty'.
