%D \module
%D   [      file=t-lttab,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility tabular environments.
%D         author=Brooks Moses,
%D           date=\currentdate,
%D      copyright={Brooks Moses; The LaTeX3 Project}]

%D This \ConTeXt\ module is part of the \LaTeX-compatibility
%D module set.  It provides various tabular environments,
%D including math arrays.

\usemodule[ltbase]
\unprotect
\catcode`\|=12

%d from ltpictur

\newcount\@multicnt

%D \section{Math Commands}
%D
%D Hopefully, some of these can get moved to \type{m-newmat} in the 
%D future, as they are more generally useful than just in \LaTeX-
%D compatibility mode.

%D The array environment.  This is currently defined for full
%D compatibility with LaTeX, rather than for the improved spacing that
%D AMSmath.dtx suggests would be appropriate.

% Provided by ConTeXt
%\def\m@th{\mathsurround\z@}
%\newdimen\p@ \p@=1pt
%\newdimen\z@ \z@=0pt % can be used both for 0pt and 0

%D LaTeX's tabbing is ignored

%D Arrays

\def\extracolsep#1{\tabskip #1\relax}

\def\startarray%
  {\let\@acol\@arrayacol
   \let\@classz\@arrayclassz
   \let\@classiv\@arrayclassiv
   \let\\\@arraycr
   \let\@halignto\@empty
   \@tabarray }

\def\stoparray{\crcr\egroup\egroup}
\def\stoptabular{\crcr\egroup\egroup $\egroup} % $
\letvalue{stoptabular*}\stoptabular

%D Tabular stuff, added by TH

\def\starttabular{\let\@halignto\@empty\@tabular}

\setvalue{starttabular*}#1%
  {\setlength\dimen@{#1}%
   \edef\@halignto{to\the\dimen@}\@tabular}

\def\@tabular
  {\leavevmode \hbox \bgroup $% $
   \let\@acol\@tabacol 
   \let\@classz\@tabclassz
   \let\@classiv\@tabclassiv 
   \let\\\@tabularcr
   \catcode`\|=12
   \@tabarray}

\def\@tabarray{\m@th\@ifnextchar[\@array{\@array[c]}}

\def\@array[#1]#2{%
  \if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi
  \bgroup
  \setbox\@arstrutbox\hbox{%
    \vrule \@height\arraystretch\ht\strutbox
           \@depth\arraystretch \dp\strutbox
           \@width\z@}%
  \@mkpream{#2}%
  \edef\@preamble{%
    \ialign \noexpand\@halignto
      \bgroup \@arstrut \@preamble \tabskip\z@skip \cr}%
  \let\@startpbox\@@startpbox \let\@endpbox\@@endpbox
  \let\tabularnewline\\%
    \let\par\@empty
    \let\@sharp##%
    \set@typeset@protect
    \lineskip\z@skip\baselineskip\z@skip
    \ifhmode \@preamerr\z@ \@@par\fi
    \@preamble}

\def\@arraycr{%
  ${\ifnum0=`}\fi\@ifstar\@xarraycr\@xarraycr} % $
\def\@xarraycr{\@ifnextchar[\@argarraycr{\ifnum0=`{\fi}${}\cr}} % $
\def\@argarraycr[#1]{%
  \ifnum0=`{\fi}${}\ifdim #1>\z@ \@xargarraycr{#1}\else % $
   \@yargarraycr{#1}\fi}

\let\tabularnewline\relax

\def\@tabularcr{%
  {\ifnum0=`}\fi\@ifstar\@xtabularcr\@xtabularcr}

\def\@xtabularcr{\@ifnextchar[\@argtabularcr{\ifnum0=`{\fi}\cr}}

\def\@argtabularcr[#1]{%
  \ifnum0=`{\fi}%
    \ifdim #1>\z@
      \unskip\@xargarraycr{#1}%
    \else
      \@yargarraycr{#1}%
    \fi}

\def\@xargarraycr#1{\@tempdima #1\advance\@tempdima \dp \@arstrutbox
   \vrule \@height\z@ \@depth\@tempdima \@width\z@ \cr}

\def\@yargarraycr#1{\cr\noalign{\vskip #1}}

\long\def\multicolumn#1#2#3{\multispan{#1}\begingroup
  \@mkpream{#2}%
  \def\@sharp{#3}\set@typeset@protect
  \let\@startpbox\@@startpbox\let\@endpbox\@@endpbox
  \@arstrut \@preamble\hbox{}\endgroup\ignorespaces}

\def\@xexnoop #1\@@{}

\def\@expast#1{\@xexpast #1*0x\@@}
\def\@xexpast#1*#2#3#4\@@{%
  \edef\reserved@a{#1}%
  \@tempcnta#2\relax
  \ifnum\@tempcnta>\z@
    \@whilenum\@tempcnta>\z@\do
       {\edef\reserved@a{\reserved@a#3}\advance\@tempcnta \m@ne}%
    \let\reserved@b\@xexpast
  \else
    \let\reserved@b\@xexnoop
  \fi
  \expandafter\reserved@b\reserved@a #4\@@}


\newif\if@firstamp


\def\@addamp{%
  \if@firstamp
    \@firstampfalse
  \else
    \edef\@preamble{\@preamble &}%
  \fi}

\def\@arrayacol{\edef\@preamble{\@preamble \hskip \arraycolsep}}

\def\@tabacol{\edef\@preamble{\@preamble \hskip \tabcolsep}}

\def\@ampacol{\@addamp \@acol}

\def\@acolampacol{\@acol\@addamp\@acol}

\def\@mkpream#1{\@firstamptrue\@lastchclass6
  \let\@preamble\@empty
  \let\latexprotect\@unexpandable@protect
  \let\@sharp\relax
  \let\@startpbox\relax\let\@endpbox\relax
  \@expast{#1}%
  \expandafter\@tfor \expandafter
    \@nextchar \expandafter:\expandafter=\reserved@a\do
       {\@testpach\@nextchar
    \ifcase \@chclass \@classz \or \@classi \or \@classii \or \@classiii
      \or \@classiv \or\@classv \fi\@lastchclass\@chclass}%
  \ifcase \@lastchclass \@acol
      \or \or \@preamerr \@ne\or \@preamerr \tw@\or \or \@acol \fi}



\def\@arrayclassz{%
  \ifcase \@lastchclass \@acolampacol \or \@ampacol \or
          \or \or \@addamp \or
          \@acolampacol \or \@firstampfalse
    \@acol
  \fi
  \edef\@preamble{%
    \@preamble
    \ifcase \@chnum
      \hfil$\relax\@sharp$\hfil \or $\relax\@sharp$\hfil
      \or \hfil$\relax\@sharp$%
    \fi%
  }%
}

\def\@tabclassz{%
  \ifcase\@lastchclass
    \@acolampacol
  \or
    \@ampacol
  \or
  \or
  \or
    \@addamp
  \or
    \@acolampacol
  \or
    \@firstampfalse\@acol
  \fi
  \edef\@preamble{%
    \@preamble{%
      \ifcase\@chnum
        \hfil\ignorespaces\@sharp\unskip\hfil
      \or
        \hskip1sp\ignorespaces\@sharp\unskip\hfil
      \or
        \hfil\hskip1sp\ignorespaces\@sharp\unskip
      \fi}}}

\def\@classi{%
  \ifcase\@lastchclass
    \@acol\@arrayrule
  \or
    \@addtopreamble{\hskip \doublerulesep}\@arrayrule
  \or
  \or
  \or
    \@arrayrule
  \or
    \@acol\@arrayrule
  \or
    \@arrayrule
  \fi}


\def\@classii{%
  \ifcase\@lastchclass
  \or
    \@addtopreamble{\hskip .5\arrayrulewidth}%
  \fi}

\def\@classiii{\ifcase \@lastchclass \@acolampacol \or
   \@addamp\@acol \or
   \or \or \@addamp \or
   \@acolampacol \or \@ampacol \fi}

\def\@tabclassiv{\@addtopreamble\@nextchar}

\def\@arrayclassiv{\@addtopreamble{$\@nextchar$}}

\def\@classv{\@addtopreamble{\@startpbox{\@nextchar}\ignorespaces
\@sharp\@endpbox}}

\def\@addtopreamble#1{\edef\@preamble{\@preamble #1}}

\newcount\@chclass
\newcount\@lastchclass
\newcount\@chnum

\newdimen\arraycolsep
\newdimen\tabcolsep
\newdimen\arrayrulewidth
\newdimen\doublerulesep
\def\arraystretch{1}    % Default value.

\newbox\@arstrutbox

\def\@arstrut{%
  \relax\ifmmode\copy\@arstrutbox\else\unhcopy\@arstrutbox\fi}

\def\@testpach#1{\@chclass \ifnum \@lastchclass=\tw@ 4 \else
    \ifnum \@lastchclass=3 5 \else
     \z@ \if #1c\@chnum \z@ \else
                              \if #1l\@chnum \@ne \else
                              \if #1r\@chnum \tw@ \else
          \@chclass \if #1|\@ne \else
                    \if #1@\tw@ \else
                    \if #1p3 \else \z@ \@preamerr 5\fi
  \fi  \fi  \fi  \fi  \fi  \fi
\fi}

\def\@arrayrule{\@addtopreamble{\hskip -.5\arrayrulewidth
   \vrule \@width \arrayrulewidth\hskip -.5\arrayrulewidth}}

\def\hline{%
  \noalign{\ifnum0=`}\fi\hrule \@height \arrayrulewidth \futurelet
   \reserved@a\@xhline}

\def\@xhline{\ifx\reserved@a\hline
               \vskip\doublerulesep
               \vskip-\arrayrulewidth
             \fi
      \ifnum0=`{\fi}}

\def\vline{\vrule \@width \arrayrulewidth}

\def\cline#1{\@cline#1\@nil}

\def\@cline#1-#2\@nil{%
  \omit
  \@multicnt#1%
  \advance\@multispan\m@ne
  \ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
  \@multicnt#2%
  \advance\@multicnt-#1% 
  \advance\@multispan\@ne
  \leaders\hrule\@height\arrayrulewidth\hfill
  \cr
  \noalign{\vskip-\arrayrulewidth}}

% multicol & such ...

\def\multispan{\omit\@multispan}

\def\@multispan#1{%
  \@multicnt#1\relax
  \loop\ifnum\@multicnt>\@ne \sp@n\repeat}

\def\sp@n{\span\omit\advance\@multicnt\m@ne}

\def\@startpbox#1{\vtop\bgroup \setlength\hsize{#1}\@arrayparboxrestore}

\def\@endpbox{\@finalstrut\@arstrutbox\par\egroup\hfil}

\let\@@startpbox=\@startpbox

\let\@@endpbox=\@endpbox

%D End lttab

%D Back to Brooks

%\let\@typeset@protect\relax
%\def\set@typeset@protect{\let\latexprotect\@typeset@protect}

%\newcount\@tempcnta
%\newcount\@tempcntb
%\newif\if@tempswa
%\newdimen\@tempdima
%\newdimen\@tempdimb
%\newdimen\@tempdimc

\gdef\@preamerr#1{%
  \begingroup
     \houstonwehaveaproblem
	\message{\meaning #1}%
%    \let\latexprotect\relax
%    \@latex@error{\ifcase #1 Illegal character\or
%     Missing @-exp\or Missing p-arg\fi\space
%     in array arg}\@ehd
  \endgroup}

\iffalse
\def\@fornoop#1\@@#2#3{}
\long\def\@for#1:=#2\do#3{%
  \expandafter\def\expandafter\@fortmp\expandafter{#2}%
  \ifx\@fortmp\@empty \else
    \expandafter\@forloop#2,\@nil,\@nil\@@#1{#3}\fi}
\long\def\@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@iforloop #3\@@#4{#5}\fi\fi}
\long\def\@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \expandafter\@fornoop \else
      #4\relax\expandafter\@iforloop\fi#2\@@#3{#4}}
\def\@tfor#1:={\@tf@r#1 }
\long\def\@tf@r#1#2\do#3{\def\@fortmp{#2}\ifx\@fortmp\space\else
    \@tforloop#2\@nil\@nil\@@#1{#3}\fi}
\long\def\@tforloop#1#2\@@#3#4{\def#3{#1}\ifx #3\@nnil
       \expandafter\@fornoop \else
      #4\relax\expandafter\@tforloop\fi#2\@@#3{#4}}
\fi

\catcode`\|=13

\protect