% Style file `paracol
% Copyright (C) 2005-2011
%   Hiroshi Nakashima <h.nakashima@DOMAIN;  DOMAIN=media.kyoto-u.ac.jp>
%   (Kyoto University)
% This program can be 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 any later version.

\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{paracol}[2011/09/16 v0.91 ]

\newcount\pcol@currcol
\newcount\pcol@nextcol
\newcount\pcol@ncol
\newcount\pcol@page
\newcount\pcol@maxpage
\newcount\pcol@basepage
\newif\ifpcol@textonly
\newif\ifpcol@sync
\newif\ifpcol@clear
\newif\ifpcol@stopoutput
\newdimen\pcol@prevdepth
\newdimen\pcol@colht
\newskip\pcol@topskip

\def\pcol@output{\let\par\@@par
  \ifnum\outputpenalty<-\@M
    \pcol@specialoutput
  \else
    \@makecol
    \pcol@opcol
    \pcol@startcolumn
    \@whilesw\if@fcolmade\fi{\pcol@opcol \pcol@startcolumn}%
  \fi
  \ifnum\outputpenalty>-\@Miv
    \ifdim\@colroom<1.5\baselineskip
      \ifdim\@colroom<\textheight
        \@latex@warning@no@line{Text page \thepage\space
                                contains only floats}%
        \@emptycol
      \else
        \global\vsize\@colroom
      \fi
    \else
      \global\vsize\@colroom
    \fi
  \else
    \global\vsize\maxdimen
  \fi}



\def\pcol@ovf{%
  \PackageError{paracol}{Too many unprocessed columns/floats}\@ehb}
\def\pcol@opcol{%
  \@next\@currbox\@freelist{\global\setbox\@currbox\box\@outputbox}\pcol@ovf
  \expandafter\@cons\csname pcol@shipped\number\pcol@currcol\endcsname\@currbox
  \ifnum\pcol@currcol=\z@ \pcol@setpageno \fi
  \pcol@nextpage
  \pcol@checkshipped
  \if@tempswa \pcol@outputpage\z@ \fi
  \global\@colht\textheight
  \ifnum\pcol@page>\pcol@maxpage \pcol@startpage
  \else                          \pcol@getcurrpage
  \fi
  \pcol@floatplacement}

\def\pcol@checkshipped{\@tempswatrue
  \@tempcnta\z@ \@whilenum\@tempcnta<\pcol@ncol\do{%
    \expandafter\ifx\csname pcol@shipped\number\@tempcnta\endcsname\@empty
      \@tempswafalse \fi
   \advance\@tempcnta\@ne}}

\def\pcol@getcurrpinfo#1{%
  \edef#1{\expandafter\@cdr\pcol@currpage\@nil}%
  \pcol@getpinfo#1}

\def\pcol@setpageno{\begingroup
  \@tempcnta\pcol@page \advance\@tempcnta-\pcol@basepage
  \edef\reserved@a{\pcol@pages\pcol@currpage}%
  \global\let\pcol@pages\@empty \global\let\pcol@currpage\@empty
  \let\@elt\pcol@setpnoelt \reserved@a
  \endgroup}
\def\pcol@setpnoelt#1{%
  {\let\@elt\relax \xdef\pcol@pages{\pcol@pages\pcol@currpage}}%
  \pcol@textonlytrue
  \ifnum#1<\z@         \gdef\pcol@currpage{\@elt{#1}}%
  \else\ifnum#1>\@cclv \gdef\pcol@currpage{\@elt{#1}}%
  \else                \gdef\pcol@currpage{\@elt#1}\pcol@textonlyfalse
  \fi\fi
  \ifnum\@tempcnta>\z@\else
    \ifpcol@textonly \pcol@settextpage
    \else            \global\count#1\c@page
    \fi
    \advance\c@page\@ne
  \fi
  \advance\@tempcnta\m@ne}
\def\pcol@nextpage{\begingroup
  \@tempcnta\pcol@page \advance\@tempcnta-\pcol@basepage
  \@tempswatrue
  \let\@elt\pcol@nextpelt \pcol@pages\pcol@currpage
  \global\advance\pcol@page\@ne
  \endgroup}
\def\pcol@nextpelt#1{%
  \ifnum\@tempcnta<\z@
    \ifnum#1<\z@ \@tempswafalse
    \else\ifnum#1>\@cclv \@tempswafalse
    \else\ifdim\dimen#1<\z@
      \if@tempswa \global\advance\pcol@page\@ne \fi
    \else \@tempswafalse
    \fi\fi\fi
  \fi
  \advance\@tempcnta\m@ne}
\def\pcol@getcurrpage{\begingroup
  \@tempcnta\pcol@page \advance\@tempcnta-\pcol@basepage
  \let\@elt\pcol@getpelt \pcol@pages\pcol@currpage
  \endgroup}
\def\pcol@getpelt#1{%
  \ifnum\@tempcnta=\z@
    \pcol@getpinfo{#1}{\global\c@page}{\global\@colht}{\global\topskip}\fi
  \advance\@tempcnta\m@ne}

\def\pcol@getpinfo#1#2#3#4{\count@#1\relax \pcol@textonlytrue
  \ifnum\count@>\@cclv
    \advance\count@-\@cclvi #2\count@ #3\textheight #4\pcol@topskip
  \else\ifnum\count@<\z@
    #2\count@ #3\textheight #4\pcol@topskip
  \else #2\count\count@  #3\dimen\count@ #4\skip\count@ \pcol@textonlyfalse
  \fi\fi}
\def\pcol@settextpage{\begingroup
  \@tempcnta\c@page
  \ifnum\@tempcnta<\z@\else \advance\@tempcnta\@cclvi \fi
  \let\@elt\relax \xdef\pcol@currpage{\@elt{\number\@tempcnta}}
  \endgroup}

\def\pcol@startpage{%
  \global\pcol@maxpage\pcol@page
  \ifx\pcol@currpage\@empty\else
    \pcol@getcurrpinfo\@currbox{\global\c@page}\@tempdima\@tempskipa
    \stepcounter{page}%
  \fi
  \global\@colht\textheight
  \global\topskip\pcol@topskip
  \xdef\pcol@pages{\pcol@pages\pcol@currpage}%
  \@dblfloatplacement
  \@tryfcolumn\@dbldeferlist
  \@whilesw\if@fcolmade\fi{%
    \@next\@currbox\@freelist{%
      \global\setbox\@currbox\box\@outputbox}\pcol@ovf
    \global\count\@currbox\c@page
    \global\dimen\@currbox-\maxdimen
    \global\skip\@currbox\pcol@topskip
    \@cons\pcol@pages\@currbox
    \stepcounter{page}%
    \global\advance\pcol@page\@ne \global\pcol@maxpage\pcol@page
    \@tryfcolumn\@dbldeferlist}%
  \begingroup
    \let\reserved@b\@dbldeferlist
    \global\let\@dbldeferlist\@empty
    \let\@elt\@sdblcolelt
    \reserved@b
  \endgroup
  \ifx\@dbltoplist\@empty
    \pcol@settextpage
  \else
    \setbox\@tempboxa\vbox{}%
    \begingroup
      \let\@elt\@comdblflelt
      \@dbltoplist
      \let\@elt\relax
      \xdef\@freelist{\@freelist\@dbltoplist}%
      \global\let\@dbltoplist\@empty
      \@next\@currbox\@freelist{%
        \global\setbox\@currbox\vbox{%
          \unvbox\@tempboxa \vskip-\dblfloatsep \dblfigrule
          \vskip\dbltextfloatsep}}%
        \pcol@ovf
      \global\count\@currbox\c@page
      \global\dimen\@currbox\@colht
      \global\skip\@currbox\pcol@topskip
      \xdef\pcol@currpage{\@elt\@currbox}%
    \endgroup
  \fi
}


\def\pcol@outputpage#1{\begingroup
  \def\@elt{\pcol@outputelt#1}\@tempswatrue \pcol@stopoutputfalse
  \let\reserved@b\pcol@pages \gdef\pcol@pages{}%
  \reserved@b
  \endgroup}
\def\pcol@outputelt#1#2{%
  \setbox\@outputbox\box\voidb@x
  \def\@currbox{#2}%
  \pcol@getpinfo\@currbox\c@page\@tempdima\@tempskipa
  \ifdim\@tempdima<\z@
    \ifpcol@stopoutput
      \@cons\pcol@pages\@currbox
    \else
      \setbox\@outputbox\box\@currbox
      \@cons\@freelist\@currbox
    \fi
  \else\if@tempswa
    \ifnum#1=\z@ \@tempswafalse \fi
    \setbox\@outputbox\vbox to\textheight{%
      \ifpcol@textonly\else
        \@cons\@freelist\@currbox
        \unvbox\@currbox
      \fi
      \hb@xt@\textwidth{%
        \let\pcol@hfil\relax
        \@tempcnta\z@ \@whilenum\@tempcnta<\pcol@ncol\do{%
          \expandafter\@next\expandafter\@currbox
            \csname pcol@shipped\number\@tempcnta\endcsname
            \relax{\let\@currbox\voidb@x}%
          \ifvoid\@currbox\else \@cons\@freelist\@currbox \fi
          \pcol@hfil \hb@xt@\columnwidth{\box\@currbox\hss}%
          \let\pcol@hfil\hfil
         \advance\@tempcnta\@ne}}}%
  \else
    \pcol@stopoutputtrue
    \ifpcol@textonly
      \@cons\pcol@pages{{\@currbox}}
    \else
      \@cons\pcol@pages\@currbox
    \fi
  \fi\fi
  \ifvoid\@outputbox\else
    \@outputpage \global\advance\pcol@basepage\@ne
  \fi}

\def\pcol@startcolumn{%
  \global\@colroom\@colht
  \@tryfcolumn\@deferlist
  \if@fcolmade\else \pcol@trynextcolumn \fi
}
\def\pcol@trynextcolumn{\begingroup
  \let\reserved@b\@deferlist
  \global\let\@deferlist\@empty
  \let\@elt\@scolelt
  \reserved@b
  \endgroup}



\let\pcol@latex@specialoutput\@specialoutput
\def\pcol@op@start{-10010}
\def\pcol@op@switch{-10011}
\def\pcol@op@flush{-10012}
\def\pcol@op@clear{-10013}
\def\pcol@op@end{-10014}
\def\pcol@specialoutput{%
  \ifnum\outputpenalty=\pcol@op@start\relax
    \let\reserved@a\pcol@output@start
  \else\ifnum\outputpenalty=\pcol@op@switch\relax
    \let\reserved@a\pcol@output@switch
  \else\ifnum\outputpenalty=\pcol@op@flush\relax
    \let\reserved@a\pcol@output@flush
  \else\ifnum\outputpenalty=\pcol@op@clear\relax
    \let\reserved@a\pcol@output@clear
  \else\ifnum\outputpenalty=\pcol@op@end\relax
    \let\reserved@a\pcol@output@end
  \else \let\reserved@a\pcol@latex@specialoutput
  \fi\fi\fi\fi\fi
  \ifx\reserved@a\pcol@latex@specialoutput\else
    \global\setbox\@holdpg\vbox{\unvbox\@holdpg \unvbox\@cclv
      \setbox\@tempboxa\lastbox \unskip}%
    \outputpenalty-\@M
  \fi
  \reserved@a}

\def\pcol@output@start{%
  \pcol@makenormalcol
  \global\advance\@colht-\ht\@outputbox
  \global\advance\@colht-\dp\@outputbox
  \global\@colroom\@colht
  \@next\@currbox\@freelist{\global\setbox\@currbox\box\@outputbox}\pcol@ovf
  \global\count\@currbox\c@page \global\dimen\@currbox\@colht
  \@tempdima\dp\@currbox \@tempskipa\z@
  \ifdim\ht\@currbox=\z@ \ifdim\dp\@currbox=\z@
    \@tempskipa\topskip
  \fi\fi
  \global\skip\@currbox\@tempskipa \global\topskip\@tempskipa
  \xdef\pcol@currpage{\@elt\@currbox}\global\let\pcol@pages\@empty
  \global\pcol@page\z@ \global\pcol@maxpage\z@ \global\pcol@basepage\z@
  \global\let\@dbldeferlist\@deferlist
  \global\let\@deferlist\@empty
  \pcol@floatplacement
% may fix begin
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
    \@next\@currbox\@freelist{\global\setbox\@currbox\vbox{%
      \ifdim\topskip=\z@ \hrule\@height\z@\@width\z@
%      \kern-\@tempdima
      \fi}}
% may fix end
      \pcol@ovf
    \pcol@setcurrcolnf
    \global\count\@currbox\z@
    \global\dimen\@currbox\@colroom
   \advance\pcol@currcol\@ne}%
  \global\pcol@currcol\z@
  \pcol@getcurrcol
  \@cons\@freelist\@currbox
  \unvbox\@currbox}

\def\pcol@makenormalcol{%
  \ifvoid\footins \setbox\@outputbox\box\@holdpg
  \else
    \setbox\@outputbox\vbox{
      \boxmaxdepth\@maxdepth
      \unvbox\@holdpg
      \vskip\skip\footins
      \color@begingroup
        \normalcolor
        \footnoterule
        \unvbox\footins
      \color@endgroup}%
   \fi
  \let\@elt\relax
  \xdef\@freelist{\@freelist\@midlist}%
  \global\let\@midlist\@empty
  \@combinefloats}

\def\pcol@floatplacement{%
  \global\@mparbottom\z@ \global\@textfloatsheight\z@
  \@floatplacement}



\def\pcol@output@switch{%
  \@next\@currbox\@freelist{\global\setbox\@currbox\box\@holdpg}\pcol@ovf
  \ifvoid\footins
    \def\pcol@currfoot{\voidb@x}%
  \else
    \@next\pcol@currfoot\@freelist{%
      \global\setbox\pcol@currfoot\box\footins
      \global\count\pcol@currfoot\count\footins
      \global\dimen\pcol@currfoot\dimen\footins
      \global\skip\pcol@currfoot\skip\footins}\pcol@ovf
  \fi
  \ifnum\pcol@currcol=\z@ \pcol@setpageno \fi
  \pcol@setcurrcol
  \global\count\@currbox\pcol@page
  \global\dimen\@currbox\@colroom
  \@tempswafalse
  \ifpcol@sync
    \@tempswatrue
    \if@nobreak
      \@temptokena\everypar
      \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
	\pcol@getcurrcol \@nobreaktrue
	\everypar\@temptokena
	\pcol@setcurrcol
       \advance\pcol@currcol\@ne}%
  \fi\fi
  \ifpcol@clear \@tempswatrue \fi
  \if@tempswa \pcol@sync \fi
  \ifpcol@clear\else \pcol@restartcolumn \fi}

\def\pcol@restartcolumn{%
  \global\pcol@currcol\pcol@nextcol
  \pcol@getcurrcol
  \global\pcol@page\count\@currbox
  \global\@colroom\dimen\@currbox
  \@cons\@freelist\@currbox
  \pcol@getcurrpage
  \ifvoid\pcol@currfoot\else
    \insert\footins{\unvbox\pcol@currfoot}\@cons\@freelist\pcol@currfoot
  \fi
  \unvbox\@currbox
  \if@nobreak \nobreak \else \addpenalty\interlinepenalty \fi}

\def\pcol@getcurrcol{%
  \expandafter\expandafter\expandafter\pcol@igetcurrcol
    \csname pcol@col\number\pcol@currcol\endcsname}
\def\pcol@igetcurrcol#1#2#3#4#5#6#7#8{%
  \def\@currbox{#1}\def\pcol@currfoot{#2}\global\pcol@prevdepth#3sp\relax
  \gdef\@toplist{#4}\gdef\@midlist{#5}\gdef\@botlist{#6}\gdef\@deferlist{#7}%
  \pcol@iigetcurrcol#8}
\def\pcol@iigetcurrcol#1#2#3#4#5#6#7#8#9{%
  \global\@textfloatsheight#1sp\relax \global\@mparbottom#2sp\relax
  \global\@topnum#3\relax \global\@toproom#4sp\relax
  \global\@botnum#5\relax \global\@botroom#6sp\relax
  \global\@colnum#7\relax
  \global\@afterindentfalse \@nobreaktrue
  \ifcase#8
    \@nobreakfalse \or
    \global\@afterindenttrue \else
    \relax
  \fi
  \global\everypar{#9}}
\def\pcol@setcurrcol{{\let\@elt\relax
  \@tempcnta\if@nobreak\if@afterindent\@ne\else\tw@\fi\else\z@\fi
  \expandafter\xdef\csname pcol@col\number\pcol@currcol\endcsname{%
    {\@currbox}{\pcol@currfoot}{\number\pcol@prevdepth}%
    {\@toplist}{\@midlist}{\@botlist}{\@deferlist}%
    {{\number\@textfloatsheight}{\number\@mparbottom}%
     {\number\@topnum}{\number\@toproom}{\number\@botnum}{\number\@botroom}%
     {\number\@colnum}{\number\@tempcnta}{\the\everypar}}}}}
\def\pcol@setcurrcolnf{\def\pcol@currfoot{\voidb@x}\pcol@setcurrcol}
\def\pcol@getcurrfoot{%
  \ifvoid\pcol@currfoot \global\setbox\footins\box\voidb@x
  \else
    \global\setbox\footins\box\pcol@currfoot
    \global\count\footins\count\pcol@currfoot
    \global\dimen\footins\dimen\pcol@currfoot
    \global\skip\footins\skip\pcol@currfoot
  \fi}



\def\pcol@sync{%
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do\pcol@flushcolumn
  \pcol@outputpage\@ne
  \pcol@getcurrpinfo\@currbox{\global\c@page}{\global\@colht}{\global\topskip}%
  \@tempdima-\maxdimen \@tempdimb-\maxdimen \pcol@colht-\maxdimen
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do\pcol@measurecolumn
  \ifdim\@tempdima<\z@\else
    \advance\@tempdimb\@tempdima
    \@tempswafalse
    \ifdim\@tempdimb<\@colht \ifpcol@clear\else
      \@tempswatrue
    \fi\fi
    \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do\pcol@synccolumn
  \fi
  \global\pcol@syncfalse}

\def\pcol@flushcolumn{%
  \pcol@getcurrcol
  \ifnum\count\@currbox<\pcol@maxpage
    \pcol@page\count\@currbox
    \setbox\@cclv\box\@currbox
    \pcol@getcurrfoot
    \ifvoid\pcol@currfoot\else \@cons\@freelist\pcol@currfoot \fi
    \pcol@getcurrpage
    \@makecol
    \global\setbox\@currbox\box\@outputbox
    \expandafter\@cons\csname pcol@shipped\number\pcol@currcol\endcsname
      \@currbox
    \advance\pcol@page\@ne
    \ifx\@deferlist\@empty\else
      \@whilenum\pcol@page<\pcol@maxpage\do{%
        \pcol@getcurrpage
        \@makefcolumn\@deferlist
        \if@fcolmade
          \@next\@currbox\@freelist{\global\setbox\@currbox\box\@outputbox}%
            \pcol@ovf
          \expandafter\@cons
            \csname pcol@shipped\number\pcol@currcol\endcsname\@currbox
        \fi
       \advance\pcol@page\@ne}%
    \fi
    \@next\@currbox\@freelist{\global\setbox\@currbox\vbox{}}\pcol@ovf
    \pcol@floatplacement \global\@colroom\@colht
    \ifx\@deferlist\@empty\else
      \pcol@getcurrpage \global\@colroom\@colht
      \pcol@trynextcolumn
    \fi
    \pcol@setcurrcolnf
    \global\count\@currbox\pcol@page
    \global\dimen\@currbox\@colroom
  \fi %\ifnum\count\@currbox<\pcol@maxpage
  \advance\pcol@currcol\@ne}

\def\pcol@magicpenalty{12345}
\def\pcol@measurecolumn{%
  \pcol@getcurrcol
  \@tempswatrue
  \setbox\@tempboxa\vbox{\penalty\pcol@magicpenalty
    \unvcopy\@currbox \xdef\@gtempa{\number\lastpenalty}}%
  \ifnum\@gtempa=\pcol@magicpenalty\relax \ifvoid\pcol@currfoot
  \ifx\@toplist\@empty \ifx\@botlist\@empty
    \@tempswafalse \skip\@currbox\z@ \pcol@prevdepth\z@ \pcol@setcurrcol
  \fi\fi\fi\fi
  \if@tempswa
    \dimen@ii\ht\@currbox \advance\dimen@ii\dp\@currbox
% must fix begin
    \ifnum\@gtempa=\pcol@magicpenalty\relax
      \global\setbox\@currbox\vbox{\hrule\@height\z@ \kern-\topskip}
      \pcol@prevdepth\z@ \pcol@setcurrcol
    \fi
% must fix end
    \dimen@\z@ \pcol@addflhd\@toplist\topfigrule
    \skip\@currbox\dimen@ \advance\dimen@ii\dimen@
    \ifdim\dimen@ii>\@tempdima
      \@tempdima\dimen@ii \@tempdimc\pcol@prevdepth \fi
    \ifvoid\pcol@currfoot \dimen@\z@
    \else
      \dimen@\ht\pcol@currfoot \advance\dimen@\dp\pcol@currfoot
      \advance\dimen@\skip\pcol@currfoot
    \fi
    \pcol@addflhd\@botlist\botfigrule
    \ifdim\dimen@>\@tempdimb \@tempdimb\dimen@ \fi
    \advance\dimen@\dimen@ii
    \ifdim\dimen@>\pcol@colht
      \pcol@colht\dimen@
      \@pagedp\pcol@prevdepth
      \ifvoid\pcol@currfoot\else \@pagedp\dp\pcol@currfoot \fi
      \ifx\@botlist\@empty\else \@pagedp\z@ \fi
    \fi
  \fi %\if@tempswa
  \advance\pcol@currcol\@ne}
\def\pcol@addflhd#1#2{\ifx#1\@empty\else
  \let\@elt\pcol@hdflelt
  #1\advance\dimen@-\floatsep \advance\dimen@\textfloatsep
  \setbox\@tempboxa\vbox{#2}\advance\dimen@\ht\@tempboxa
  \let\@elt\relax
  \fi}
\def\pcol@hdflelt#1{\advance\dimen@\ht#1\advance\dimen@\dp#1%
  \advance\dimen@\floatsep}

\def\pcol@synccolumn{%
  \pcol@getcurrcol
  \if@tempswa
    \@tempdimb\@tempdima \advance\@tempdimb-\@tempdimc
    \advance\@tempdimb\pcol@prevdepth \advance\@tempdimb-\skip\@currbox
    \global\setbox\@currbox\vbox{\vbox to\@tempdimb{\unvbox\@currbox \vfil}}%
  \else
    \global\setbox\@currbox\vbox{\unvbox\@currbox \vfill \penalty-\@M}%
  \fi
  \global\@topnum\z@ \pcol@setcurrcol
  \advance\pcol@currcol\@ne}



\def\pcol@output@end{%
  \pcol@makelastpage\pcol@colht
  \@tempswafalse
  \if@fcolmade
    \setbox\@outputbox\vbox to\textheight{\unvbox\@outputbox \vfil}%
    \@outputpage %\c@page=OK
    \pcol@flushfloats
    \global\pcol@prevdepth\z@
    \@tempswatrue
  \else\ifdim\pcol@colht<\z@
    \unvbox\@outputbox
    \global\pcol@prevdepth\z@
  \else
    \topskip\z@ \box\@outputbox
    \global\pcol@prevdepth\@pagedp %\c@page=OK
  \fi\fi
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
    \pcol@getcurrcol \@cons\@freelist\@currbox
   \advance\pcol@currcol\@ne}%
  \global\@colht\textheight
  \global\@colroom\textheight
  \global\let\@deferlist\@dbldeferlist \gdef\@dbldeferlist{}%
  \pcol@floatplacement
  \if@tempswa \@startcolumn \fi}

\def\pcol@output@flush{%
  \pcol@makelastpage\@colht
  \setbox\@outputbox\vbox to\textheight{\unvbox\@outputbox}%
  \@outputpage %\c@page=OK
  \global\@colht\textheight
  \pcol@freshpage}

\def\pcol@output@clear{%
  \pcol@makelastpage\@colht
  \setbox\@outputbox\vbox to\textheight{\unvbox\@outputbox}%
  \@outputpage %\c@page=OK
  \global\advance\pcol@page\@ne
  \pcol@flushfloats %\c@page=OK
  \global\@colht\textheight
  \begingroup
    \@dblfloatplacement
    \@makefcolumn\@dbldeferlist
    \@whilesw\if@fcolmade\fi{\@outputpage
      \@makefcolumn\@dbldeferlist}%
  \endgroup %\c@page=OK
  \pcol@freshpage}

\def\pcol@makelastpage#1{%
  \pcol@cleartrue \pcol@output@switch \pcol@clearfalse
  \pcol@getcurrpinfo\@currbox{\global\c@page}{\global\@colht}\@tempskipa
  \global\@fcolmadefalse
  \setbox\@outputbox\vbox{%
    \ifpcol@textonly\else
      \@cons\@freelist\@currbox
      \unvbox\@currbox
    \fi
    \ifdim#1<\z@
      \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
	\pcol@getcurrcol
        \ifx\@deferlist\@empty\else \global\@fcolmadetrue \fi
       \advance\pcol@currcol\@ne}%
    \else
      \hb@xt@\textwidth{%
        \let\pcol@hfil\relax \@colht#1\relax
        \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
          \pcol@getcurrcol
          \ifx\@deferlist\@empty\else \global\@fcolmadetrue \fi
          \ifvoid\pcol@currfoot\else \@cons\@freelist\pcol@currfoot \fi
          \setbox\@cclv\box\@currbox
          \pcol@getcurrfoot
          \@makecol
          \pcol@hfil \hb@xt@\columnwidth{\box\@outputbox\hss}%
          \let\pcol@hfil\hfil
          \pcol@setcurrcolnf
         \advance\pcol@currcol\@ne}}
    \fi}}

\def\pcol@flushfloats{%
  \@whilesw\if@fcolmade\fi{%
    \setbox\@outputbox\vbox{\hb@xt@\textwidth{%
      \let\pcol@hfil\relax
      \@tempswafalse
      \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
        \pcol@getcurrcol
        \@makefcolumn\@deferlist
        \pcol@hfil \hb@xt@\columnwidth{\if@fcolmade \box\@outputbox \fi \hss}%
        \ifx\@deferlist\@empty\else \@tempswatrue \fi
        \let\pcol@hfil\hfil
        \pcol@setcurrcolnf
       \advance\pcol@currcol\@ne}%
      \global\@fcolmadefalse
      \if@tempswa \global\@fcolmadetrue \fi}}%
    \@outputpage}}

\def\pcol@freshpage{%
  \global\pcol@page\z@ \global\pcol@maxpage\z@ \global\pcol@basepage\z@
  \gdef\pcol@pages{}\gdef\pcol@currpage{}%
  \pcol@startpage \pcol@colht\@colht
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
    \pcol@getcurrcol \pcol@page\z@ \@colroom\pcol@colht
    \pcol@getcurrpage
    \pcol@floatplacement
    \pcol@startcolumn
    \@whilesw\if@fcolmade\fi{\pcol@opcol \pcol@startcolumn}%
    \global\setbox\@currbox\vbox{}%
    \global\count\@currbox\pcol@page \global\dimen\@currbox\@colroom
    \pcol@setcurrcolnf
   \advance\pcol@currcol\@ne}%
  \global\pcol@currcol\pcol@nextcol
  \pcol@restartcolumn}



\def\pcol@invokeoutput#1{%
  \penalty-\@Miv \@tempdima\prevdepth \vbox{}%
  \global\pcol@prevdepth\@tempdima \penalty#1\relax
  \prevdepth\pcol@prevdepth}

\def\paracol#1{\par
  \global\pcol@ncol#1\relax
  \global\columnwidth\textwidth
  \@tempdima\columnsep \multiply\@tempdima\pcol@ncol
  \advance\@tempdima-\columnsep
  \global\advance\columnwidth-\@tempdima \global\divide\columnwidth\pcol@ncol
  \global\hsize\columnwidth \global\linewidth\columnwidth
  \global\@twocolumntrue \global\@firstcolumntrue \col@number\pcol@ncol
  \global\let\pcol@counters\cl@@ckpt
  \let\@elt\pcol@remctrelt \pcol@gcounters
  \let\@elt\pcol@thectrelt \pcol@counters
  \let\@elt\relax
  \let\clearpage\pcol@clearpage \pcol@topskip\topskip
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
    \expandafter\gdef\csname pcol@shipped\number\pcol@currcol\endcsname{}%
    \pcol@savecounters \advance\pcol@currcol\@ne}%
  \output{\pcol@output}%
  \pcol@invokeoutput\pcol@op@start
  \pcol@nextcol\z@
  \def\column{\pcol@defcolumn\nthcolumn{0}}
  \@namedef{column*}{\pcol@defcolumn\@nameuse{nthcolumn*}{0}}
  \@ifnextchar[%]
    \pcol@mctext\relax}

\def\pcol@gcounters{\@elt{page}}
\def\pcol@remctrelt#1{%
  \expandafter\let\expandafter\reserved@a\csname cl@#1\endcsname
  \expandafter\let\csname pcol@cl@#1\endcsname\reserved@a
  \expandafter\ifx\csname c@#1\endcsname\c@page\else
    \@namedef{cl@#1}{\pcol@stepcounter{#1}}%
  \fi
  \def\reserved@a{#1}\let\reserved@b\pcol@counters
  \global\let\pcol@counters\@empty
  {\let\@elt\pcol@iremctrelt \reserved@b}}
\def\pcol@iremctrelt#1{%
  \def\reserved@b{#1}%
  \ifx\reserved@a\reserved@b\else \@cons\pcol@counters{{#1}}\fi}
\def\pcol@thectrelt#1{%
  \expandafter\let\expandafter\reserved@a\csname the#1\endcsname
  \expandafter\let\csname pcol@thectr@#1\endcsname\reserved@a
  \expandafter\let\expandafter\reserved@a\csname pcol@thectr@#10\endcsname
  \ifx\reserved@a\relax\else
    \expandafter\let\csname the#1\endcsname\reserved@a
  \fi}

\def\pcol@mctext[#1]{%
  \@tempcnta\pcol@nextcol\relax
  \global\pcol@synctrue \pcol@nextcol\z@
  \pcol@switchcol
  \begingroup
    \hsize\textwidth \col@number\@ne #1\par
    \expandafter\global\expandafter\everypar\expandafter{\the\everypar}%
  \endgroup
  \pcol@nextcol\@tempcnta \global\pcol@synctrue \pcol@switchcol}

\def\pcol@switchcol{%
  \pcol@savecounters
  \pcol@invokeoutput\pcol@op@switch
  \ifnum\pcol@currcol=\z@ \global\@firstcolumntrue
  \else                   \global\@firstcolumnfalse
  \fi
  \let\@elt\pcol@setctrelt
  \csname pcol@counters\number\pcol@currcol\endcsname
  \let\@elt\relax}

\def\pcol@savecounters{\begingroup
  \global\let\@gtempa\@empty
  \let\@elt\pcol@savectrelt \pcol@counters
  \let\@elt\relax
  \expandafter\xdef\csname pcol@counters\number\pcol@currcol\endcsname{%
    \@gtempa}%
  \endgroup}
\def\pcol@savectrelt#1{\@cons\@gtempa{{#1}{\number\csname c@#1\endcsname}}}

\def\pcol@setctrelt#1#2{%
  \setcounter{#1}{#2}%
  \expandafter\ifx\csname pcol@thectr@#1\number\pcol@currcol\endcsname\relax
    \expandafter\let\expandafter\reserved@a\csname pcol@thectr@#1\endcsname
  \else
    \expandafter\let\expandafter\reserved@a
      \csname pcol@thectr@#1\number\pcol@currcol\endcsname
  \fi
  \expandafter\let\csname the#1\endcsname\reserved@a}

\def\pcol@stepcounter#1{\begingroup
  \let\@elt\@stpelt \csname pcol@cl@#1\endcsname
  \pcol@currcol\z@ \@whilenum\pcol@currcol<\pcol@ncol\do{%
    \let\@elt\pcol@stpldelt \csname pcol@counters\number\pcol@currcol\endcsname
    \let\@elt\pcol@stpclelt \csname pcol@cl@#1\endcsname
    \pcol@savecounters
   \advance\pcol@currcol\@ne}%
  \endgroup}
\def\pcol@stpldelt#1#2{\csname c@#1\endcsname#2\relax}
\def\pcol@stpclelt#1{\csname c@#1\endcsname\z@}

\def\definethecounter#1#2#3{\@namedef{pcol@thectr@#1#2}{#3}}
\def\globalcounter#1{\@cons\pcol@gcounters{{#1}}}

\def\endparacol{\par
  \pcol@nextcol\z@ \pcol@switchcol
  \pcol@invokeoutput\pcol@op@end
  \global\columnwidth\textwidth
  \global\hsize\columnwidth
  \global\linewidth\columnwidth
  \global\@twocolumnfalse
  \global\topskip\pcol@topskip}

\def\switchcolumn{\par
  \@tempcnta\pcol@currcol \advance\@tempcnta\@ne
  \ifnum\@tempcnta<\pcol@ncol\else \@tempcnta\z@ \fi
  \@ifnextchar[%]
    \pcol@switchcolumn{\pcol@switchcolumn[\@tempcnta]}}
\def\pcol@switchcolumn[#1]{%
  \pcol@nextcol#1\relax
  \ifnum#1<\pcol@ncol\else
    \PackageError{paracol}{%
      Column number \number#1 must be less than\number\pcol@ncol}\@eha
    \pcol@nextcol\z@
  \fi
  \@ifstar\pcol@iswitchcolumn\pcol@switchcol}
\def\pcol@iswitchcolumn{%
  \global\pcol@synctrue
  \@ifnextchar[%]
    \pcol@mctext\pcol@switchcol}

\def\pcol@defcolumn{%
  \gdef\column{\switchcolumn\relax}%
  \global\@namedef{column*}{\switchcolumn*}}
\def\nthcolumn#1{\switchcolumn[#1]\relax}
\@namedef{nthcolumn*}#1{\switchcolumn[#1]*}
\def\leftcolumn{\switchcolumn[0]\relax}
\@namedef{leftcolumn*}{\switchcolumn[0]*}
\def\rightcolumn{\switchcolumn[1]\relax}
\@namedef{rightcolumn*}{\switchcolumn[1]*}

\let\endcolumn\par
\expandafter\let\csname endcolumn*\endcsname\par
\let\endnthcolumn\par
\expandafter\let\csname endnthcolumn*\endcsname\par
\let\endleftcolumn\par
\expandafter\let\csname endleftcolumn*\endcsname\par
\let\endrightcolumn\par
\expandafter\let\csname endrightcolumn*\endcsname\par

\def\flushpage{\par
  \pcol@nextcol\pcol@currcol
  \pcol@invokeoutput\pcol@op@flush}
\def\pcol@clearpage{\par
  \pcol@nextcol\pcol@currcol
  \pcol@invokeoutput\pcol@op@clear}

