%% midfloat.sty
%% Copyright 1997 Sigitas Tolu\v sis
%% VTeX Ltd., Akademijos 4, Vilnius, Lithuania
%% e-mail sigitas@vtex.lt
%% http://www.vtex.lt/tex/download/macros/
%%
% This program can redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or (at your option) any later version.
%
% PURPOSE:   Insert some material in full-width on double-column page.
%
% SHORT DESCRIPTION:
%
%   It is denoted to place some material (from the left column)
%   in full-width at any place (but not at the top) on double-column 
%   page (\twocolumn mode). Existing text remains as it was before 
%   insert. Placement is restricted to one page. (It is some kind of
%   dbfloat intext).
%
% Provides new environment:
%
%  \begin{strip}[#1/#2] ... \end{strip}
%  -----------------------------
%
%   Puts \vbox{...} in full-width on double-column page.
%   Parameters #1 and #2 are optional. 
%   By default #1=0pt and #2=0pt.
%   The material on the right column under strip is stretched down 
%   by #1 and below strip is moved down by #2.
%
%   It can be problems with floats and footnotes. In the case of 
%   problems with footnotes it is possible to correct the situation
%   using commands \footnotemark and \footnotetext.
%
% \changes{1997/10/01}{some names}
%
\def\get@CVSdate#1Id: #2,v #3 #4 #5 #6 #7${#4}%$
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{midfloat}[\get@CVSdate$Id: midfloat.sty,v 1.1 2002/06/11 15:12:11 sigitas Exp $]
%
\newif\if@vihproof \@vihprooftrue
\newtoks\juo@sar \juo@sar={}
\def\@ca#1#2#3#4{}
\@ifundefined{stripsep}{\newskip\stripsep\stripsep 15pt}{}
%
\newskip\m@addvipersep
\m@addvipersep\z@
\newskip\c@addvipersep
\c@addvipersep\z@
\newdimen\ht@strip
\newdimen\right@cor
\newdimen\cor@height \cor@height=0pt
\newdimen\juo@pr
\newdimen\juo@ht
\newbox\@viper
\newcount\juo@sk
\newdimen\c@colht
\newbox\@rightcolumn

\def\add@strip#1#2#3#4{\begingroup%
  \xdef\ex@{\global\noexpand\juo@sar{\the\juo@sar\noexpand\@ca{#1}{#2}{#3}{#4}}}\ex@%
\endgroup}

\def\remove@strip#1{\ifx#1\@empty\global\juo@sk=0\else
   \global\advance\juo@sk by-1\expandafter\next@item\the #1\@@#1\fi}
\def\next@item \@ca #1#2#3#4#5\@@#6{\global #6={#5}\global\juo@pr=#1\global\juo@ht=#2%
\global\cor@height=#3\global\m@addvipersep=#4}

\def\strip{\@ifnextsymb[{\@strip}{\@strip[0pt/0pt]}}
\def\@strip[#1/#2]{\global\@tempdima=#1\global\@tempdimb=#2%
  \global \setbox\@viper\vbox\bgroup%
        \hsize\textwidth
        \@parboxrestore
        \col@number \@ne
        \vrule height\topskip width0pt depth0pt
}
\def\endstrip{%
        \egroup
        \if@firstcolumn
                \ifdim\pagetotal>\z@
                        \vskip\stripsep
                        \global\ht@strip\pagegoal
                        \global\advance\ht@strip by-\pagetotal
                        \global\advance\ht@strip by-\stripsep
                        \global\advance\ht@strip by-\ht\@viper
                        \global\advance\ht@strip by-\stripsep
                        \global\juo@ht=\ht\@viper
                \else
                        \global\ht@strip\z@
                \fi
                \ifdim\ht@strip>\z@
                  \unvbox\@viper
                  \vskip\stripsep
                  \add@strip{\the\pagetotal}{\the\juo@ht}{\the\@tempdima}{\the\@tempdimb}%
                  \global\advance\juo@sk by1
                \else
                  \@latex@error{Strip needs more place!}\@ehc
                  \if@vihproof
                    \framebox[\columnwidth]{??? Strip ???}%
                  \fi
                \fi
        \else
          \@latex@error{Strip is used only on left column!}\@ehc
          \if@vihproof
            \framebox[\columnwidth]{??? Strip ???}%
          \fi
        \fi
}
\def\@outputdblcol{%
  \if@firstcolumn
    \global \@firstcolumnfalse
    \global \setbox\@leftcolumn \box\@outputbox
    \global\c@colht\@colht\global\c@addvipersep\z@
    \ifnum\juo@sk>0
        \remove@strip\juo@sar
        \global\@colht\the\juo@pr
        \ifdim\right@cor>\z@
                \global\advance\@colht by\right@cor
        \fi
        \global\advance\@colht by\cor@height
        \global\cor@height=0pt
    \fi
    \global \setbox\@rightcolumn \vbox{}%
  \else
    \ifdim\c@colht>\@colht
            \@tempdima\@colht
            \ifdim\@colht>\z@
            \global\setbox\@rightcolumn \vbox{%
                        \unvbox\@rightcolumn
                        \vbox to\@tempdima{%
                                \vskip\c@addvipersep\unvbox\@outputbox}%
                        \vskip\stripsep
                        \vrule height\the\juo@ht width 0pt depth 0pt
                        \vskip\stripsep
                        }%
            \global\c@addvipersep\m@addvipersep
            \else
            \global\setbox\@rightcolumn \vbox{%
                        \unvbox\@rightcolumn
                        \vrule height\the\juo@ht width 0pt depth 0pt
                        \vskip\stripsep
                        }%
            \fi
            \ifnum\juo@sk>0
                \remove@strip\juo@sar
                \global\@colht\the\juo@pr
                \global\advance\@colht by -\ht\@rightcolumn
                \global\advance\@colht by\cor@height
                \global\cor@height=0pt
            \else                             
                \global\advance\c@colht by-\ht\@rightcolumn
                \global\@colht\c@colht
            \fi
    \else
    \global \@firstcolumntrue
    \global\setbox\@outputbox\vbox to\ht\@leftcolumn{%
                        \unvbox\@rightcolumn
                        \vskip\c@addvipersep
                        \unvbox\@outputbox
                        }%
    \setbox\@outputbox \vbox {%
                         \hb@xt@\textwidth {%
                           \hb@xt@\columnwidth {%
                             \box\@leftcolumn \hss}%
                           \hfil
                           \vrule \@width\columnseprule
                           \hfil
                           \hb@xt@\columnwidth {%
                             \box\@outputbox \hss}%
                                             }%
                              }%
    \@combinedblfloats
    \@outputpage
    \begingroup
      \@dblfloatplacement
      \@startdblcolumn
      \@whilesw\if@fcolmade \fi
        {\@outputpage
         \@startdblcolumn}%
    \endgroup
    \global\juo@sk=0\global\juo@sar={}%
    \global\cor@height\z@\global\c@addvipersep\z@
    \fi
  \fi
}

\def \@cflt{%
    \let \@elt \@comflelt
    \setbox\@tempboxa \vbox{}%
    \@toplist
    \global\right@cor\ht\@tempboxa
    \ifdim\right@cor>\z@
            \global\advance\right@cor by-\floatsep
            \global\advance\right@cor by\textfloatsep
    \fi
    \setbox\@outputbox \vbox{%
                             \boxmaxdepth \maxdepth
                             \unvbox\@tempboxa
                             \vskip -\floatsep
                             \topfigrule
                             \vskip \textfloatsep
                             \unvbox\@outputbox
                             }%
    \let\@elt\relax
    \xdef\@freelist{\@freelist\@toplist}%
    \global\let\@toplist\@empty
}

\def\@ifnextsymb#1#2#3{%
  \let\reserved@d=#1%
  \def\reserved@a{#2}\def\reserved@b{#3}%
  \futurelet\@let@token\@ifnsymb}
\def\@ifnsymb{%
  \ifx\@let@token\@sptoken
    \let\reserved@c\reserved@b
  \else
    \ifx\@let@token\reserved@d
      \let\reserved@c\reserved@a
    \else
      \let\reserved@c\reserved@b
    \fi
  \fi
  \reserved@c}

\endinput
