%D \module
%D   [       file=m-newmat,
%D        version=2000.11.16,
%D          title=\CONTEXT\ Math Module,
%D       subtitle=AMS-like math extensions,
%D         author={Taco Hoekwater \& Hans Hagen},
%D           date=\currentdate,
%D      copyright={PRAGMA / Taco Hoekwater}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See licen-en.pdf for
%C details.

%D This module collects macros that \TEX\ users kind of expect
%D to be available when typesetting math. Most of them
%D originate in the \AMS\ macro packages. We have taken the
%D freedom to adapt them to \CONTEXT. This module is derived
%D from the \type {m-math} module by Taco Hoekwater (partially
%D derived from AMS math modules) and adapted|/|extended by
%D Hans Hagen.

%D Here we will add code on demand. So, just let us know what
%D should go in here.

%M \usemodule[newmat]

\unprotect

%D \macros
%D   {qedsymbol}
%D
%D [HH] The general Quod Erat Domonstrandum symbol is defined
%D in such a way that we can configure it. Because this symbol
%D is also used in text mode, we make it a normal text symbol
%D with special behavior.

\def\qedsymbol#1%
  {\ifhmode
     \unskip~\hfill#1\par
   \else\ifmmode
     \eqno#1\relax % Do we really need the \eqno here?
   \else
     \leavevmode\hbox{}\hfill#1\par
   \fi\fi}

\definesymbol [qed] [\qedsymbol{\mathematics{\square}}]

%D \macros
%D   {QED}
%D
%D [HH] For compatbility reasons we also provide the \type
%D {\QED} command. In case this command is overloaded, we still
%D have the symbol available. \symbol[qed]

\def\QED{\symbol[qed]}

%D \macros
%D   {genfrac}
%D
%D [TH] The definition of \type {\genfrac} \& co. is not
%D trivial, because it allows some flexibility. This is
%D supposed to be a user||level command, but will fail quite
%D desparately if called outside math mode (\CONTEXT\ redefines
%D \type {\over})
%D
%D [HH] We clean up this macro a bit and (try) to make it
%D understandable. The expansion is needed for generating
%D the second argument to \type {\dogenfrac}, which is to
%D be a control sequence like \type {\over}.

\unexpanded\def\genfrac#1#2#3#4%
  {\edef\!!stringa
     {#1#2}%
   \expanded
     {\dogenfrac{#4}%
      \csname
        \ifx @#3@%
          \ifx\!!stringa\empty
            \strippedcsname\normalover
          \else
            \strippedcsname\normaloverwithdelims
          \fi
        \else
          \ifx\!!stringa\empty
            \strippedcsname\normalabove
          \else
            \strippedcsname\normalabovewithdelims
          \fi
        \fi
      \endcsname}%
     {#1#2#3}}

\def\dogenfrac#1#2#3#4#5%
  {{#1{\begingroup#4\endgroup#2#3\relax#5}}}

%D \macros
%D   {dfrac, tfrac, frac, dbinom, tbinom, binom}
%D
%D [TH] No need to make these \type {\unexpanded} as well.

%\def\dfrac {\genfrac\empty\empty\empty\displaystyle}
%\def\tfrac {\genfrac\empty\empty\empty\textstyle}
%\def\frac  {\genfrac\empty\empty\empty\donothing}

\def\dfrac {\genfrac\empty\empty{}\displaystyle}
\def\tfrac {\genfrac\empty\empty{}\textstyle}
\def\frac  {\genfrac\empty\empty{}\donothing}

\def\dbinom{\genfrac()\zeropoint\displaystyle}
\def\tbinom{\genfrac()\zeropoint\textstyle}
\def\binom {\genfrac()\zeropoint\donothing}

\def\xfrac {\genfrac\empty\empty{}\scriptstyle}
\def\xxfrac{\genfrac\empty\empty{}\scriptscriptstyle}

%D Better:

\unexpanded\def\frac#1#2{\mathematics{\genfrac\empty\empty{}\donothing{#1}{#2}}}

%D [HH] This shows up as:
%D
%D \startbuffer
%D $\dfrac {1}{2} \tfrac {1}{2} \frac {1}{2}$
%D $\dbinom{1}{2} \tbinom{1}{2} \binom{1}{2}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer

%D \macros
%D   {text}
%D
%D [TH] \type {\text} is a command to typeset more or less
%D ordinary text inside of super- and sub|-|scripts. It has to
%D do a full font switch to get the sides right, so it will be
%D quite slow. \type {\text} kind of replaces \CONTEXT's \type
%D {\mathstyle} command.

%D [HH] This macro is now also moved to the core, but we
%D keep it here as well for completeness.
%D
%D \starttyping
%D \unexpanded\def\mathtext
%D   {\mathortext\domathtext\hbox} % {\ifmmode\@EA\dotext\else\@EA\hbox\fi}
%D
%D \def\domathtext#1%
%D   {\mathchoice
%D      {\dodomathtext\displaystyle\textface        {#1}}%
%D      {\dodomathtext\textstyle   \textface        {#1}}%
%D      {\dodomathtext\textstyle   \scriptface      {#1}}%
%D      {\dodomathtext\textstyle   \scriptscriptface{#1}}}
%D
%D \def\dodomathtext#1#2#3% no \everymath !
%D  %{\hbox{\everymath{#1}\switchtobodyfont  [#2]#3}} % 15 sec
%D   {\hbox{\everymath{#1}\setcurrentfontbody{#2}#3}} %  3 sec (no math)
%D \stoptyping

%D [HH] We use the following indirectness because \type {\text}
%D is a natural candidate for user macros (actually, it is
%D used in some modules).
%D
%D \starttyping
%D \let\text\mathtext
%D \stoptyping

%D [HH] Actually, the font switch is not that slow when
%D typefaces are used. If needed this macro can be sped up.
%D
%D \startbuffer
%D ordinary text $x^{\text{extra ordinary text}}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer

%D \macros
%D  {mathhexbox}
%D
%D [TH] \type {\mathhexbox} is also user||level (already
%D defined in Plain \TEX). It allows to get a math character
%D inserted as if it was a text character.

\gdef\mathhexbox#1#2#3{\mathtext{$\mathsurround\zeropoint\mathchar"#1#2#3$}}

%D \macros
%D   {boxed}
%D
%D [HH] Another macro that users expect (slightly adapted):

\def\boxed
  {\ifmmode\expandafter\mframed\else\expandafter\framed\fi}

%D \macros
%D   {cfrac}
%D
%D [HH] Now let us see what this one does:
%D
%D \startbuffer
%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer

\definecomplexorsimple\cfrac

\def\simplecfrac
  {\complexcfrac[c]}

\def\complexcfrac[#1]#2#3%
  {{\displaystyle
    \frac
      {\strut\ifx r#1\hfill\fi#2\ifx l#1\hfill\fi}%
      {#3}}%
    \kern-\nulldelimiterspace}

%D [HH] The next alternative is nicer:

\def\simplecfrac     {\docfrac[cc]}
\def\complexcfrac[#1]{\docfrac[#1cc]}

\def\docfrac[#1#2#3]#4#5%
  {{\displaystyle
    \frac
      {\strut
       \ifx r#1\hfill\fi#4\ifx l#1\hfill\fi}%
      {\ifx r#2\hfill\fi#5\ifx l#2\hfill\fi}%
    \kern-\nulldelimiterspace}}

%D [HH] Now we can align every combination we want:
%D
%D \startbuffer
%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
%D $\cfrac[cl]{12}{3} \cfrac[cc]{12}{3} \cfrac[cr]{12}{3}$
%D $\cfrac[lc]{1}{23} \cfrac[cc]{1}{23} \cfrac[rc]{1}{23}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer

%D \macros
%D   {splitfrac, splitdfrac}
%D
%D Occasionally one needs to typeset multi||line fractions.
%D These commands use \tex{genfrac} to create such fractions.
%D
%D \startbuffer
%D \startformula
%D      a=\frac{
%D          \splitfrac{xy + xy + xy + xy + xy}
%D                    {+ xy + xy + xy + xy}
%D        }
%D        {z}
%D      =\frac{
%D          \splitdfrac{xy + xy + xy + xy + xy}
%D                    {+ xy + xy + xy + xy}
%D        }
%D        {z}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D These macros are based on Michael J.~Downes posting on
%D comp.text.tex on 2001/12/06

\def\splitfrac#1#2%
  {\genfrac\empty\empty\zeropoint\textstyle%
     {\textstyle#1\quad\hfill}%
     {\textstyle\hfill\quad\mathstrut#2}}

\def\splitdfrac#1#2%
  {\genfrac\empty\empty\zeropoint\displaystyle%
     {#1\quad\hfill}
     {\hfill\quad\mathstrut #2}}

\protect \endinput

%D \macros
%D   {startsubarray,substack,startsmallmatrix}
%D
%D [HH] I wonder what these are supposed to do. An example
%D will be inserted later. Contrary to the original we
%D support an optional argument between either \type {{}} or
%D \type {[]}.

\def\startsubarray
  {\doifnextcharelse\bgroup
     \simplestartsubarray{\dosingleempty\complexstartsubarray}}

\def\complexstartsubarray[#1]%
  {\simplestartsubarray{#1}}

\def\simplestartsubarray#1%
  {\vcenter\bgroup
   \baselineskip\fontdimen10 \scriptfont\plustwo
   \advance\baselineskip\fontdimen12 \scriptfont\plustwo
   \lineskip\plusthree\fontdimen8 \scriptfont\plusthree
   \lineskiplimit\lineskip
   \ialign\bgroup\ifx c#1\hfil\fi$\mathsurround\zeropoint\scriptstyle##$\hfil\crcr}

\def\stopsubarray
  {\crcr\egroup
   \egroup}

\def\startsubstack
  {\doifnextcharelse\bgroup
     \simplestartsubstack{\dosingleempty\complexstartsubstack}}

\def\complexstartsubstack[#1]%
  {\simplestartsubstack{#1}}

\def\simplesubstack#1%
  {\startsubarray[c]#1\stopsubarray}

\def\startsmallmatrix
  {\null
   \,%
   \vcenter\bgroup
   \baselineskip6\ex@
   \lineskip1.5\ex@
   \lineskiplimit\lineskip
   \ialign\bgroup\hfil$\mathsurround\zeropoint\scriptstyle##$\hfil&&\thickspace\hfil
     $\mathsurround\zeropoint\scriptstyle##$\hfil\crcr}

\def\stopsmallmatrix
  {\crcr\egroup
   \egroup
   \,}

\protect \endinput
