% supertabular sty
% original idea:  Theo Jurriens 1988
%                 TAJ@hgrrug5        P.O Box 800, 9700 AV Groningen
%
% revised by:     Gabriele Kruljac
%                 kruljac@ds0mpi11   Max-Planck-Institute Stuttgart
%
%
% 06.06.89  Correction: now care is taken of probably existing onecolumn
%           head (title or tables ...) in twocolumn sty.
%
% 10.05.89  Correction: the new \\ definition has been added to the
%           begin of each `sub'-tabular
%           Added: algorithm to produce the tabulars in twocolumn style
%
% 06.04.89  Correction: put \global statement in \end{supertabular}
%           into \noalign
%
% 22.02.89  Correction: restore the original meaning of \\ with
%           \end{supertabular}
%
% (Feb 89) The whole algorithm has been changed, so that I can use
%          the most features of a normal tabular:
%          \\  for new line, including  \\[#1]
%          p{...} in the preamble ...
%          Account is taken to \baselinestretch and \arraystretch
%         -I'm not counting the lines because of too much rounding errors
%          but instead I add the (estimated) used space in pt.
%         -The tablehead is taken into this algorithm of proofing, so
%          I really know how much space the head uses.
%         -When no p-arg is given I add a variable \midlineheight to
%          calculate the used space. To calculate \midlineheight I
%          take the \baselineskip, which is active when the supertabular
%          starts (\baselineskip includes the \value of \baselinestretch)
%          and multiply it with \arraystretch.
%         -When a p-arg is given the text will be stored in a box. So
%          I know the height I have to add. Also I reduce the maximum
%          pagesize, so that the last parbox on a page can get up to
%          max 4 lines without producing an overfull vbox.
%         -To do so I had to make some additions to LaTeX's tabular
%          commands. These new commands got a leading `s'.
%
%          Weak points:
%          -When the material of a normal entry (not a p-arg) becomes
%           larger than the estimated \midlineheight, overfull vboxes
%           will be produced at all.
%          -When the last p-arg on a page gets more than 4 lines
%           (probably even more than 3 lines) it will result in an
%           overfull vbox.
%           Also some combinations of \baselinestretch \arraystretch and
%           a large font may lead to one line too much.
%          -if accidentally the last line of the tabular produces
%           a newpage, on the next page the tabletail will be written
%           immediately after the tablehead. Depending on the contents
%           this may result in an error message regarding misplaced
%           \noalign.
%
%           A quick but not very elegant solution: shrink \maxsize by
%           \noalign{\global\maxsize=...pt} after the first \\ of the
%           supertabular.
%
 
\typeout{Document Substyle `SUPERTAB' by MPI FKF,  Version 1.2, 10.05.89}
 
                             % \tablehead activates the new tabular \cr
                             % commands
\def\@tablehead{}
\def\tablehead#1{\gdef\@tablehead{#1}}
 
\def\@tabletail{}
\def\tabletail#1{\gdef\@tabletail{#1}}
 
\newdimen\maxsize            % maximum pagesize
\newdimen\actsize            % actual pagesize
\newdimen\twocolsize         % needed for correct max height if twocolumn
\newdimen\parboxheight       % height plus depth of a parbox-argument
\newdimen\addspace           % stores the value of \\[#1]
\newdimen\midlineheight      % estimated size of a normal line
\newdimen\pargcorrection     % to set page height tolerance if p-arg
\newdimen\computedimens      % computation variable
\newbox\tabparbox
 
         %%%%  Redefine original LaTeX tabular \cr commands. %%%%
         %%%%  New tabular \cr commands get a leading `s'    %%%%
 
                             % Insert  \nextline command for counting
\def\@stabularcr{{\ifnum0=`}\fi\@ifstar{\@sxtabularcr}{\@sxtabularcr}    }
\def\@sxtabularcr{\@ifnextchar[{\@sargtabularcr}%
                 {\ifnum0=`{\fi}\cr\nextline}}
 
                             % contents of command unchanged
\def\@sargtabularcr[#1]{\ifnum0=`{\fi}\ifdim #1>\z@
    \unskip\@sxargarraycr{#1}\else \@syargarraycr{#1}\fi}
 
                             % here copy the value #1 of [ ] of \\
                             % to \addspace
\def\@sxargarraycr#1{\@tempdima #1\advance\@tempdima \dp \@arstrutbox%
    \vrule \@height\z@ \@depth\@tempdima \@width\z@ \cr%
    \noalign{\global\addspace=#1}\nextline}
 
                             % command will be called when \\[0pt]
\def\@syargarraycr#1{\cr\noalign{\vskip #1\global\addspace=#1}\nextline}
 
         %%%%  Redefine original LaTeX p-arg commands.       %%%%
         %%%%  New commands get a leading `s'                %%%%
 
                                      % reduce maximum pagesize to have
                                      % a small tolerance for last entry
\def\@sstartpbox#1{\global\advance\maxsize by -\pargcorrection
                   \global\pargcorrection=0pt
                                      % put text into box to save height
                   \setbox\tabparbox%
                          \vtop\bgroup\hsize#1\@arrayparboxrestore}
 
\def\@sendpbox{\par\vskip\dp\@arstrutbox\egroup%
               \computedimens=\ht\tabparbox%
               \advance\computedimens by \dp\tabparbox%
               \ifnum\parboxheight<\computedimens
                  \global\parboxheight=\computedimens
               \fi
               \computedimens=0pt
               \box\tabparbox\hfil}
 
         %%%%  Here start really new supertabular commands   %%%%
 
                                 % estimate height of normal line
                                 % regarding \array- and \baselinestretch
\def\calmidlineheight{\midlineheight=\arraystretch \baslineskp
                      \global\advance\midlineheight by 1pt
                      \global\pargcorrection=4\midlineheight}
 
\def\calpage{\global\actsize=\pagetotal  % where am I on the actual page?
             \twocolsize=\textheight            %  added 06.06.89
             \advance\twocolsize by -\@colroom  %        "
             \advance\actsize by \twocolsize    %        "
             \global\advance\actsize by \midlineheight
             \maxsize=\textheight        % start a new page when 90% of
             \multiply \maxsize by 9     % the page are used
             \divide\maxsize by 10
             \ifnum\actsize > \maxsize
                   \clearpage
                   \global\actsize=\pagetotal
             \fi
             \maxsize=\textheight       % now set \maxsize with tolerance
             \global\advance\maxsize by -\midlineheight}   % of one lines
                             % Here is the definition of supertabular
\newenvironment{supertabular}[1]{%
                                 % before it was \edef\tableformat,
                                 % but gave error with @{\hspace{..}} !
                \def\tableformat{\string#1} % store preamble
 
                \def\baslineskp{\baselineskip}
                \calmidlineheight% estimate height of a normal line
                \calpage         % calculate max. pagesize and startpoint
 
 
                                 % save old \\
                \global\let\@oldcr=\\
 
                                 % activate new parbox algorithm
                \let\@@startpbox=\@sstartpbox
                \let\@@endpbox=\@sendpbox
 
                                 % start normal tabular environment
                \begin{tabular}{\tableformat}%
%
                \noalign{\global\let\\=\@stabularcr}
                \@tablehead}%
                                 % this is \end{supertabular}
                {\@tabletail\noalign{\global\let\\=\@oldcr}%
%               \let\@@startpbox=\@startpbox%
%               \let\@@endpbox=\@endpbox%
                \end{tabular}}
 
\def\nextline{%           %%% algorithm to calculate the pagebreaks %%%
    \noalign{\ifnum\parboxheight=0
                                 % if no p-arg add `normal' line height
                   \advance\actsize by \midlineheight
                                      % \addspace is value #1 of \\[#1]
                   \global\advance\actsize by \addspace
             \else
                                 % if p-arg add height of box and more
                   \global\advance\actsize by \parboxheight
                   \divide\parboxheight by 11
                   \global\advance\actsize by \parboxheight
                   \global\parboxheight=0pt
             \fi
             \global\addspace=0pt}
                                      % when page becomes full:
    \ifnum\actsize<\maxsize%
    \else
                                      % output \tabletail, close tabular
                                      % output all material and start a
                                      % fresh new page
         \@tabletail%
         \end{tabular}%
         \if@twocolumn%                        % added 10.05.89
            \if@firstcolumn%                   %
               \newpage%                       %
            \else%                             %
               \clearpage%                     %
            \fi%                               %
            \twocolsize=\textheight%             % added 06.06.89
            \advance\twocolsize by -\@colroom%   %       "
            \global\actsize=\twocolsize%         %       "
         \else                                 %
            \clearpage                         %
            \global\actsize=\midlineheight%
         \fi                                   %
         \begin{tabular}{\tableformat}%
         \noalign{\global\let\\=\@stabularcr}   % added 10.05.89
         \@tablehead%
    \fi}%
