%% This is the LaTeX package dblfloatfix which solves two problems:
%%   a)~floats in a twocolumn document come out in the right order and
%%   b)~allowed float positions are now [tbp].
%%
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% 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
\ProvidesPackage{dblfloatfix}[2003/11/18 v1.0 (MH)]
%%
%% USAGE:
%%
%% This package redefines internals of the LaTeX kernel by merging
%% the code of the packages fixltx2e (the fix2col part) and stfloats.
%%
%% If either of the packages stfloats or fix2col are loaded you will
%% get an error message and your document will probably not be able
%% to compile. So don't load them!
%%
%% When the new output routine for LaTeX3 is done, this package will
%% be obsolete. The sooner the better...
%%
%% If you're using the subfigure package this package will provide a
%% fix for \end@dblfloat by Steven Cochran (the author of subfigure).
%%
%% Morten H\o gholm, <moho01ab@student.cbs.dk>
\RequirePackage{fixltx2e}
%% An error message for those who didn't read the instructions.
\newcommand*{\DBLFIXerrormessage}[1]{%
  \PackageError{dblfloatfix}{%
  Don't use the #1 package!^^J%
  Didn't you read the instructions in dblfloatfix.sty?!^^J%
  You're in big trouble now...^^J%
  Quit immediately (type `X') and correct the mistake}%
  {Type `X' dammit!}%
}
%% A check for those users who don't read manuals.
\AtBeginDocument{%
  \@ifpackageloaded{stfloats}{\DBLFIXerrormessage{stfloats}}{}%
  \@ifpackageloaded{fix2col}{\DBLFIXerrormessage{fix2col}}{}%
%% Here's a fix by Steven Douglas Cochran (the author of subfigure).
%% It is needed in order to make subfigure work with this package as
%% both packages redefine \end@dblfloat.
  \@ifpackageloaded{subfigure}{%
    \typeout{Redefining \string\end@dblfloat\ for use with subfigure}%
    \def\end@dblfloat{%
      \if@twocolumn
        \@listsubcaptions{\@captype}%
        \@endfloatbox
        \ifnum\@floatpenalty <\z@
          \@largefloatcheck
          \global\dp\@currbox1sp %
          \expandafter\@gobble\subfig@end@float
        \fi
      \else
        \end@float
      \fi}%
  }{}%
}
%% Here we go...
\def\@dblbotlist{}
\newcount\@dblbotnum
\newcounter{dblbotnumber}
\setcounter{dblbotnumber}{2}
\def\@dbflt#1{\@ifnextchar[{\@xdblfloat{#1}}{\@xdblfloat{#1}[tbp]}}

\def \@dblfloatplacement {%
  \global \@dbltopnum \c@dbltopnumber
  \global \@dblbotnum \c@dblbotnumber
  \global \@dbltoproom \dbltopfraction\@colht
  \@textmin \@colht
  \advance \@textmin -\@dbltoproom
  \@fpmin \dblfloatpagefraction\textheight
  \@fptop \@dblfptop
  \@fpsep \@dblfpsep
  \@fpbot \@dblfpbot
  \def\f@depth{1sp}%
}

\def\@addtodblcol{%
  \begingroup
  \@insertfalse
  \@setfloattypecounts
  \@getfpsbit \tw@
  \ifodd\@tempcnta
    \@flsetnum \@dbltopnum
    \ifnum \@dbltopnum>\z@
      \@tempswafalse
      \ifdim \@dbltoproom>\ht\@currbox
        \@tempswatrue
      \else
        \ifnum \@fpstype<\sixt@@n
          \advance \@dbltoproom \@textmin
          \ifdim \@dbltoproom>\ht\@currbox
            \@tempswatrue
          \fi
          \advance \@dbltoproom -\@textmin
        \fi
      \fi
      \if@tempswa
        \@bitor \@currtype \@deferlist
        \@testwrongwidth\@currbox
        \if@test
        \else
          \@bitor \@currtype \@dblbotlist
          \if@test
          \else
            \@tempdima -\ht\@currbox
            \advance\@tempdima
            -\ifx \@dbltoplist\@empty \dbltextfloatsep
            \else \dblfloatsep
            \fi
            \global \advance \@dbltoproom \@tempdima
            \global \advance \@colht \@tempdima
            \global \advance \@dbltopnum \m@ne
            \@cons \@dbltoplist \@currbox
            \@inserttrue
          \fi
        \fi
      \fi
    \fi
  \fi
  \if@insert
  \else
    \@getfpsbit 4\relax
      \ifodd\@tempcnta
        \@flsetnum \@dblbotnum
        \ifnum \@dblbotnum>\z@
          \@tempswafalse
          \ifdim \@dbltoproom>\ht\@currbox
            \@tempswatrue
          \else
            \ifnum \@fpstype<\sixt@@n
              \advance \@dbltoproom \@textmin
              \ifdim \@dbltoproom>\ht\@currbox
                \@tempswatrue
              \fi
              \advance \@dbltoproom -\@textmin
            \fi
          \fi
          \if@tempswa
            \@bitor \@currtype \@deferlist
            \if@test
            \else
              \@tempdima -\ht\@currbox
              \advance\@tempdima
              -\ifx \@dblbotlist\@empty \dbltextfloatsep
              \else \dblfloatsep
              \fi
              \global \advance \@dbltoproom \@tempdima
              \global \advance \@colht \@tempdima
              \global \advance \@dblbotnum \m@ne
              \@cons \@dblbotlist \@currbox
              \@inserttrue
            \fi
          \fi
        \fi
      \fi
    \fi
      \if@insert
      \else
        \@cons\@deferlist\@currbox
      \fi
  \endgroup
}

\def\@combinedblfloats{%
    \ifx \@dbltoplist\@empty \else \@cdblflt \fi
    \ifx \@dblbotlist\@empty \else \@cdblflb \fi
}

\def\@cdblflt{%
    \setbox\@tempboxa \vbox{}%
    \let \@elt \@comdblflelt
    \@dbltoplist
    \let \@elt \relax
    \xdef \@freelist {\@freelist\@dbltoplist}%
    \global\let \@dbltoplist \@empty
    \setbox\@outputbox \vbox% to\textheight
      {%\boxmaxdepth\maxdepth   %% probably not needed, CAR
       \unvbox\@tempboxa\vskip-\dblfloatsep
       \ifnum \@dbltopnum>\m@ne
         \dblfigrule
       \fi
       \vskip \dbltextfloatsep
       \unvbox \@outputbox
       }%
}

\def\@cdblflb{%
    \setbox\@tempboxa \vbox{}%
    \let \@elt \@comdblflelt
    \@dblbotlist
    \let \@elt \relax
    \xdef \@freelist {\@freelist\@dblbotlist}%
    \global\let \@dblbotlist \@empty
    \setbox\@outputbox \vbox% to\textheight
      {%\boxmaxdepth\maxdepth   %% probably not needed, CAR
       \unvbox\@outputbox
       \vskip \dbltextfloatsep
       \ifnum \@dblbotnum>\m@ne
         \dblfigrule
       \fi
       \unvbox\@tempboxa
       \vskip -\dblfloatsep
       }%
}

\def \@doclearpage {%
  \ifvoid\footins
    \setbox\@tempboxa\vsplit\@cclv to\z@ \unvbox\@tempboxa
    \setbox\@tempboxa\box\@cclv
    \xdef\@deferlist{\@toplist\@botlist\@deferlist}%
    \global \let \@toplist \@empty
    \global \let \@botlist \@empty
    \global \@colroom \@colht
    \ifx \@currlist\@empty
    \else
      \@latexerr{Float(s) lost}\@ehb
      \global \let \@currlist \@empty
    \fi
    \@makefcolumn\@deferlist
    \@whilesw\if@fcolmade \fi{\@opcol\@makefcolumn\@deferlist}%
    \if@twocolumn
      \if@firstcolumn
        \xdef\@deferlist{\@dbltoplist\@dblbotlist\@deferlist}%
        \global \let \@dbltoplist \@empty
        \global \let \@dblbotlist \@empty
        \global \@colht \textheight
        \begingroup
          \@dblfloatplacement
          \@makefcolumn\@deferlist
          \@whilesw\if@fcolmade \fi{\@outputpage
                                    \@makefcolumn\@deferlist}%
        \endgroup
      \else
        \vbox{}\clearpage
      \fi
    \fi
    \ifx\@deferlist\@empty \else\clearpage \fi
  \else
    \setbox\@cclv\vbox{\box\@cclv\vfil}%
    \@makecol\@opcol
    \clearpage
  \fi
}

\endinput