%D \module
%D   [      file=t-ltmath,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility math commands,
%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 math commands.  (Note that
%D the array environment is in lttab.)

\usemodule[ltbase]
\usemodule[lterr]
\unprotect

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

%D According to the \type{ltmath.dtx comments:
%D This file contains a lot of the original plain \TeX\ code, as well
%D as the \LaTeX\ environments for math. It still needs sorting out.

%D The standard operators: \type{\log}, etc., are already defined in 
%D \type{math-tex.tex}, along with the \type{\bigl} family.

%D \type{\bmod}, \type{\pmod} are in \type{math-pln.tex}.

%D \LaTeX\ has a different value of \interdisplaylinepenalty than
%D \ConTeXt.  (See \type{ltmath.dtx}, line 56.)

\interdisplaylinepenalty=100

%D \subsection{Math Environments}
%D
%D Produces \type{$...$} with checks that \type{\(} isn't used in math mode, and
%D that \type{\)} is only used in math mode begun with \type{\(}.

\def\({\relax\ifmmode\@badmath\else$\fi}
\def\){\relax\ifmmode\ifinner$\else\@badmath\fi\else \@badmath\fi}

%D Here, we go backwards from \type{\[} and \type{\]} to the \type{displaymath}
%D environment, since \ConTeXt has its own version of that.

\let\[=\startdisplaymath
\let\]=\stopdisplaymath

%D Disguises for \type{\(...\)}.

\let\startmath=\(
\let\stopmath=\)

%D The equation environment needs some work to properly deal with the numbering,
%D but for now this works.
%D
%D ### To Do: make this work properly with labels and things!

\def\startequation{\placeformula \startformula}
\def\stopequation{\stopformula}

%D A disguise for plain \TeX's buildrel.
%D
%D This should go in \type{m-newmat}.  \type{\frac} is already there.

\def\stackrel#1#2{\mathrel{\mathop{#2}\limits^{#1}}}

%D We add an extra, optional argument to \sqrt (overriding the one defined in
%D \ConTeXt, which may be bad if one is using a math symbol set other than
%D the default.)
%D
%D [This is copied from \type{math-tex.tex}, in the version Hans emailed
%D me on 2005-07-31.]

\let\normalsqrt     \sqrt       % just set to: \dohandlemathtoken {sqrt}
\def\notsosqrt [#1]{\root#1\of}
\def\sqrt{\doifnextcharelse[\notsosqrt\normalsqrt}

%D Some \type{let}s, in case anyone is foolish enough to be using the \LaTeX\
%D internal versions.

\let\sqrtsign=\normalsqrt
\let\@sqrt=\notsosqrt

%D Here's the eqnarray environment:
%D Default is for left-hand side of equations to be flushright.
%D To make them flushleft, \type{\let\@eqnsel = \hfil}.

\newcount\@eqcnt
\newcount\@eqpen
\newif\if@eqnsw\@eqnswtrue
\newskip\@centering
\@centering = 0pt plus 1000pt

%D To get a proper \cs{@currentlabel} we have to redefine it for the
%D whole display. Note that we can't use \cs{refstepcounter} as this
%D results in \type{\@currentlabel} getting restored at the wrong and
%D thus always writing the first label to the \type{.aux} file.
%D
%D ### To Do: Make this work with \ConTeXt's equation numbers!

\def\starteqnarray{%
   \stepcounter{equation}%
   \def\@currentlabel{\p@equation\theequation}%
   \global\@eqnswtrue
   \m@th
   \global\@eqcnt\z@
   \tabskip\@centering
   \let\\\@eqncr
   \startformula
      \everycr{}\halign to\displaywidth\bgroup
       \hskip\@centering$\displaystyle\tabskip\z@skip{##}$\@eqnsel
      &\global\@eqcnt\@ne\hskip \tw@\arraycolsep \hfil${##}$\hfil
      &\global\@eqcnt\tw@ \hskip \tw@\arraycolsep 
         $\displaystyle{##}$\hfil\tabskip\@centering
      &\global\@eqcnt\thr@@ \hb@xt@\z@\bgroup\hss##\egroup
         \tabskip\z@skip
      \cr
}

\def\stopeqnarray{%
      \@@eqncr
      \egroup
      \global\advance\c@equation\m@ne
   \stopformula\@ignoretrue
}

\let\@eqnsel=\relax

%D Switches off equation numbering.

\def\nonumber{\global\@eqnswfalse}

\def\@eqncr{%
   {\ifnum0=`}\fi
   \@ifstar{%
      \global\@eqpen\@M\@yeqncr
   }{%
      \global\@eqpen\interdisplaylinepenalty \@yeqncr
   }%
}

\def\@yeqncr{\@testopt\@xeqncr\z@skip}

\def\@xeqncr[#1]{%
   \ifnum0=`{\fi}%
   \@@eqncr
   \noalign{\penalty\@eqpen\vskip\jot\vskip #1\relax}%
}

\def\@@eqncr{\let\reserved@a\relax
    \ifcase\@eqcnt \def\reserved@a{& & &}\or \def\reserved@a{& &}%
     \or \def\reserved@a{&}\else
       \let\reserved@a\@empty
       \@latex@error{Too many columns in eqnarray environment}\@ehc\fi
     \reserved@a \if@eqnsw\@eqnnum\stepcounter{equation}\fi
     \global\@eqnswtrue\global\@eqcnt\z@\cr}

%D Here's the eqnarray* environment:

\let\@seqncr=\@eqncr

\@namedef{starteqnarray*}{\def\@eqncr{\nonumber\@seqncr}\starteqnarray}

\@namedef{stopendeqnarray*}{\nonumber\stopeqnarray}

%D \type{\lefteqn{FORMULA}} typesets \type{FORMULA} in display math style
%D flushleft in a box of width zero.
%D
%D ### To Do: candidate for \type{m-newmat}.

\def\lefteqn#1{\rlap{$\displaystyle #1$}}

%D There is already an equivalent to \type{\ensuremath} in \ConTeXt.

\let\ensuremath\mathematics

%D We leave out the \type{fleqn} and \type{leqno} optional parts.


\protect


\protect