\unprotect 

% clean up footnotes to notes 

% We need to set the \dimen globally since we are in the 
% OTR. Unfortunately this interferes with local settings, 
% although we may assume that they will not cross page 
% boundaries. 

\def\OTRSETcheckcontent
  {\bgroup
   \donefalse
   \def\OTRSETcheckcontent##1%
     {\setbox\scratchbox##1\recurselevel
      \setbox\scratchbox\vbox{\unvbox\scratchbox}%
      \ifdim\ht\scratchbox>\zeropoint\donetrue\fi}%
   \dorecurse{\nofcolumns}
     {\OTRSETcheckcontent\columngettextbox
      \OTRSETcheckcontent\columngetfootbox
      \OTRSETcheckcontent\columngettopbox 
      \OTRSETcheckcontent\columngetbotbox}%
   \ifdone\egroup\donefalse\else\egroup\donetrue\fi} 

\def\OTRSETgetcolumntextheight#1% max - boven - top
  {\scratchdimen=\OTRSETgetparameter\c!maxhoogte#1%
   \advance\scratchdimen -\OTRSETgetparameter\c!bovenoffset#1%
   \advance\scratchdimen -\ht\columntopbox#1%
   \advance\scratchdimen -\getvalue{\??mc\number\mofcolumns-f}%
   \advance\scratchdimen -\ht\columnbotbox#1} % not used 

\def\OTRSETgetcolumnnaturalheight#1% max - boven - top
  {\setbox\scratchbox\vbox{\unvcopy\columntextbox#1}%
   \scratchdimen\ht\scratchbox
   \advance\scratchdimen \OTRSETgetparameter\c!bovenoffset#1%
   \advance\scratchdimen \ht\columntopbox#1%
   \advance\scratchdimen -\getvalue{\??mc\number\mofcolumns-f}%
   \advance\scratchdimen \ht\columnbotbox#1} % not used 

\def\OTRSETdobalance% splitten in met en zonder footnotes
  {\bgroup
   \maxdeadcycles=1000
   % collect content and notes
   \bgroup
   \OTRSEToutput
     {\global\setbox1\vbox{\unvbox\normalpagebox}%
      \global\setbox3\vbox{\unvbox\footins}}%
   \verticalstrut\vskip-\struttotal % makes footnotes flush  
   \eject
   \global\collectingcontentfalse
   % check for footnotes only 
   \ifdim\ht1<\topskip % real dirty 
     \global\setbox1=\vbox{}
     % fix height of first line 
     \ifdim\ht3>\!!zeropoint
       \global\setbox3\vbox
         {\setfootnotebodyfont
          \kern-\strutht
          \kern\topskip
          \unvbox3}
   \fi
   % prepare trial box 
   \global\setbox5\vbox
     {\ifdim\ht1>\zeropoint 
        \unvcopy1 \ifdim\ht3>\zeropoint \kern\skip\footins \fi
      \fi
      \ifdim\ht3>\zeropoint \unvcopy3 \fi}
   \egroup
   % erase old stuff 
   \columnerasetextboxes
   \columnerasefootboxes
   % prepare floats
   \OTRSETdotopinsertions
   \OTRSETdobotinsertions % not used can be removed 
   % calculate available space
   \!!heighta\zeropoint % available total height
   \dorecurse{\nofcolumns}
     {\OTRSETgetcolumntextheight\recurselevel
      \OTRSETcalculatelines\scratchdimen
      \advance\!!heighta \scratchdimen}
   % quick check
   \ifdim\ht5>\!!heighta
     % use normal routine
     \columnerasetextboxes
     \columnerasefootboxes
     % TEMP, TODO, FORCE NEXT PASS ! 
     \unvbox1
     \unvbox3 
     \doglobal\addtocommalist{-\realfolio}\OTRSETbalancinglist
     \donefalse
    %\writestatus\m!columns{no balancing, text overflows height}%
   \else\ifdim\ht5>\zeropoint \relax
     % some text and/or notes
     \donetrue
   \else      
     \donefalse
     \dorecurse\nofcolumns
       {\ifdim\ht\columntopbox\recurselevel>\zeropoint \donetrue \fi}%
     \ifdone 
       % no text and notes, but figures  
     \else
       % no text, no notes, no figures 
       \doglobal\addtocommalist{-\realfolio}\OTRSETbalancinglist
       \writestatus\m!columns{no balancing, nothing to be placed}%
     \fi
   \fi\fi
   \ifdone
     \ifdim\ht5>\zeropoint \relax
       % balancing text and notes 
      %\writestatus\m!columns{text may fit, balancing}%
       \newcounter\loopcounter
       \newcounter\balancinglines
       \doloop
         {\increment\loopcounter\relax
          % initialize
          \columnerasetextboxes
          \columnerasefootboxes
          \setbox0=\copy5 
          \splittopskip\topskip
          % pre-split loop and quality calculation
         %\writestatus\m!columns{pass \loopcounter: correction -\balancinglines}
          \dorecurse\nofcolumns
            {\OTRSETgetcolumntextheight\recurselevel
             \OTRSETcalculatelines\scratchdimen
             \!!heightc\scratchdimen
             \ifnum\recurselevel<\nofcolumns
               \advance\!!heightc -\balancinglines\lineheight
             \fi
             \columnsettextbox\recurselevel\vsplit0 to \!!heightc}
          % just one method 
          \OTRSETgetcolumnnaturalheight1           \dimen4\scratchdimen
          \OTRSETgetcolumnnaturalheight\nofcolumns \dimen6\scratchdimen
         %\writestatus\m!columns{first column: \the\dimen4}%
         %\writestatus\m!columns{last  column: \the\dimen6}%
          \ifdim\dimen4=\dimen6
            \donetrue  % perfect balance
          \else\ifdim\dimen4>\dimen6
            \donefalse % not yet good enough
            \increment\balancinglines % try again
            \edef\balancingcount{\the\!!counta}
          \else
            \donetrue  % worse balance
            \ifnum\balancinglines>0 % take previous
              \decrement\balancinglines
            \fi
          \fi\fi
          % extra check  
          % \ifdim\ht0>\zeropoint\relax \donefalse \fi
          % another check 
          \ifdone
           %\writestatus\m!columns{balancing finished in pass \loopcounter}%
          \else \ifnum\loopcounter>100 \donetrue
           %\writestatus\m!columns{balancing aborted after pass \loopcounter}%
          \else
           %\writestatus\m!columns{balancing continued after pass \loopcounter}%
          \fi \fi
          % final balancing pass
          \ifdone
            \setbox0\copy1
            \setbox2\copy3
            \columnerasetextboxes
            \columnerasefootboxes
            \dorecurse\nofcolumns
              {\OTRSETgetcolumntextheight\recurselevel
               \OTRSETcalculatelines\scratchdimen
               \!!heightc\scratchdimen
               \ifnum\recurselevel<\nofcolumns
                 \advance\!!heightc -\balancinglines\lineheight
               \fi
               % split off text 
               \ifdim\ht0>\zeropoint
                 \columnsettextbox\recurselevel\vsplit0 to \!!heightc
                 \setbox4\vbox{\unvcopy\columntextbox\recurselevel}
                 \advance\!!heightc -\ht4
                 \ifdim\ht0>\zeropoint
                   \columnsettextbox\recurselevel\box4
                   \advance\!!heightc \skip\footins
                 \fi
               \fi
               % split off footnotes
               \ifdim\ht0>\zeropoint\relax \else 
                 \ifdim\ht2>\zeropoint\relax
                   \setbox4\vsplit2 to \!!heightc
                   \setbox\footins=\vbox{\unvbox4} % ugly and dirty trick
                   \ifdim\ht\columntextbox\recurselevel=\zeropoint\relax
                     \columnsettextbox\recurselevel\hbox
                       {\raise\strutdp\hbox % ugly but needed
                          {\setfootnotebodyfont % both these moves
                           \lower\strutdp\hbox{\placebottomnotes}}}
                   \else
                     \columnsetfootbox\recurselevel\hbox{\placebottomnotes}
                   \fi
                 \fi
               \fi}
            \exitloop
          \fi}
       \else
         % no reason to balance floats 
       \fi
     \fi
     \doglobal\addtocommalist{\realfolio}\OTRSETbalancinglist
     \OTRSETdoflush
   \fi
   \egroup}

\protect \endinput 
