%D \module
%D   [       file=supp-mat,
%D        version=1998.09.10,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Math,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D For practical reasons, I decided to move some math things to
%D a support module. There is nothing spectacular here. It may move
%D back to math-ini.

\writestatus{loading}{ConTeXt Support Macros / Math}

\unprotect

%D \macros
%D   {mathematics, math, nomathematics, startmathmode}
%D
%D The \type{$} can be both an begin and end math character.
%D This can lead to confusing and errorprone situations when
%D macros insert \type{$}. When for instance we have defined:
%D
%D \starttyping
%D \def\MyPlus{$\,+\,$}
%D \stoptyping
%D
%D the sequence \type{$x^2 \MyPlus y^2 = r^2$} will expand to:
%D
%D \starttyping
%D $x^2 $\,+\,$ y^2 = r^2$
%D \stoptyping
%D
%D Here the \type{\,} are given outside math mode and \TEX\ will
%D definitely complain about this. A more save definition would
%D have been:
%D
%D \starttyping
%D \def\MyPlus{\mathematics{\,+\,}}
%D \stoptyping
%D
%D Which is implemented (in \type{syst-ini.mkiv}) as:

% \let\normalsuperscript\Usuperscript
% \let\normalsubscript  \Usubscript
% \let\normalstartimath \Ustartmath
% \let\normalstopimath  \Ustopmath
% \let\normalstartdmath \Ustartdisplaymath
% \let\normalstopdmath  \Ustopdisplaymath

% \unexpanded\def\Ustartdisplaymath
%   {\ifinner
%      \ifhmode
%        \normalUstartmath
%        \let\Ustopdisplaymath\normalUstopmath
%      \else
%        \normalUstartdisplaymath
%        \let\Ustopdisplaymath\normalUstopdisplaymath
%      \fi
%    \else
%      \normalUstartdisplaymath
%      \let\Ustopdisplaymath\normalUstopdisplaymath
%    \fi}

\unexpanded\def\Ucheckedstartdisplaymath
  {\ifinner
     \ifhmode
       \normalUstartmath
       \let\Ucheckedstopdisplaymath\normalUstopmath
     \else
       \normalUstartdisplaymath
       \let\Ucheckedstopdisplaymath\normalUstopdisplaymath
     \fi
   \else
     \normalUstartdisplaymath
     \let\Ucheckedstopdisplaymath\normalUstopdisplaymath
   \fi}

\let\Ucheckedstopdisplaymath\relax

\def\normalmathaligntab{&} % \let\normalmathaligntab\aligntab does to work well in a let to & (a def works ok)

\let\normalsuper       \Usuperscript       % obsolete
\let\normalsuber       \Usubscript         % obsolete

\let\startimath        \Ustartmath
\let\stopimath         \Ustopmath
\let\startdmath        \Ustartdisplaymath  % \Ucheckedstartdisplaymath
\let\stopdmath         \Ustopdisplaymath   % \Ucheckedstopdisplaymath

\unexpanded\def\mathematics#1{\relax         \ifmmode#1\else\normalstartimath#1\normalstopimath\fi}
\unexpanded\def\displaymath#1{\noindent      \ifmmode#1\else\normalstartdmath#1\normalstopdmath\fi}
\unexpanded\def\inlinemath #1{\dontleavehmode\ifmmode#1\else\normalstartimath#1\normalstopimath\fi}
\unexpanded\def\textmath   #1{\dontleavehmode\ifmmode#1\else\begingroup\everymath\emptytoks\normalstartimath#1\normalstopimath\endgroup\fi} % \mathsurround\zeropoint

\let\m\mathematics

\let\stopmathmode\relax

\unexpanded\def\startmathmode % nested variant
  {\relax\ifmmode
     \begingroup
     \let\stopmathmode\endgroup
   \else
     \normalstartimath
     \let\stopmathmode\normalstopimath
   \fi}

\unexpanded\def\nomathematics#1%
  {\relax\ifmmode\hbox{#1}\else#1\fi}

\let\math\mathematics

%D \macros
%D   {displaymathematics,inlinemathematics,automathematics}
%D
%D An example of usage of the following can be found in the MathML module:

\ifdefined\strc_formulas_start_formula \else
    \def\strc_formulas_start_formula{\normalstartdmath}
    \def\strc_formulas_stop_formula {\normalstopdmath }
\fi

\unexpanded\def\displaymathematics#1{\relax\ifmmode#1\else\strc_formulas_start_formula{}#1\strc_formulas_stop_formula\fi}
\unexpanded\def\inlinemathematics   {\dontleavehmode\mathematics}
\unexpanded\def\automathematics     {\relax\ifhmode\expandafter\inlinemathematics\else\expandafter\displaymathematics\fi}

% better, esp when used in bTABLE ... eTABLE

\unexpanded\def\automathematics
  {\relax
   \ifhmode
     \expandafter\inlinemathematics
   \else\ifintable
     \doubleexpandafter\inlinemathematics
   \else
     \doubleexpandafter\displaymathematics
   \fi\fi}

%D \macros
%D   {dimension, nodimension}
%D
%D The next few macros are used for typesetting dimensions in
%D such a way that spacing is acceptable. I won't spend much
%D words on these macros, because they will be overloaded in
%D the units module.
%D
%D REPLACED (keep commented):

\newsignal\dimensionsignal

\def\dimensiontypeface  {\tf}
\def\dimensionhalfspace {\,}

\unexpanded\def\dimension#1%
  {\pushmacro\dodimensionsignal
   \gdef\dodimensionsignal{\kern\dimensionsignal}%
   \ifzeropt\lastskip
     \ifzeropt\lastkern
       \ifmmode
         \mathematics{\dimensionhalfspace\dimensionhalfspace\dimensiontypeface#1}%
       \else
         \mathematics{\dimensiontypeface#1}%
       \fi
     \else\ifdim\lastkern=\dimensionsignal
       \mathematics{\dimensionhalfspace\dimensiontypeface#1}%
     \else
       \unkern\mathematics{\dimensionhalfspace\dimensionhalfspace\dimensiontypeface#1}%
     \fi\fi
   \else
     \unskip\mathematics{\dimensionhalfspace\dimensionhalfspace\dimensiontypeface#1}%
   \fi
   \dodimensionsignal
   \popmacro\dodimensionsignal}

\unexpanded\def\nodimension#1%
  {\unskip#1\glet\dodimensionsignal\relax}

%D \macros
%D   {cramped}
%D
%D Cramped math:
%D
%D \startbuffer
%D \ruledhbox{$\left\{{x^2\over y^2}\right\}$}
%D \ruledhbox{$\cramped{\left\{ {x^2\over y^2}\right\}}$}
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\unexpanded\def\cramped
  {\mathpalette\math_cramped}

%D We force a cramped style by issuing a non||existing radical (box).
%D After that we make sure the height is correct. (Comment taken from
%D Morten Høgholm's mathtools package.) Macro suggested by AM and
%D cleaned up a bit by HH.

% original
%
% \def\docramped#1#2%
%   {\setbox\scratchbox\hbox
%     {\nulldelimiterspace\zeropoint
%      $\mathsurround\zeropoint#1\radical\zerocount{#2}$}%
%    \ifx#1\displaystyle
%       \scratchdimen\fontdimen8\textfont3
%       \advance\scratchdimen .25\fontdimen5\textfont2
%    \else
%       \scratchdimen1.25\fontdimen8
%       \ifx#1\textstyle\textfont
%       \else
%         \ifx#1\scriptstyle\scriptfont\else\scriptscriptfont\fi
%       \fi
%       3
%     \fi
%     \advance\scratchdimen-\ht\scratchbox
%     \ht\scratchbox-\scratchdimen
%     \box\scratchbox}

% mkii:
%
% \def\math_cramped#1#2%
%   {\begingroup % added HH, made even more cramped
%    \setbox\scratchbox\hbox
%      {\nulldelimiterspace\zeropoint
%       \normalstartimath\mathsurround\zeropoint#1\radical\zerocount{#2}\normalstopimath}%
%    \ht\scratchbox-\dimexpr
%      \ifx#1\displaystyle
%        \fontdimen\pluseight\textfont\plusthree
%        +.25\fontdimen\plusfive\textfont\plustwo
%      \else
%         1.25\fontdimen\pluseight
%           \ifx#1\textstyle
%             \textfont
%           \else\ifx#1\scriptstyle
%             \scriptfont
%           \else
%             \scriptscriptfont
%           \fi \fi
%           \plusthree
%      \fi
%      -\ht\scratchbox
%    \relax
%    \box\scratchbox
%    \endgroup}

\def\math_cramped#1#2% we have all in one family
  {\begingroup % added HH, made even more cramped
   \setbox\scratchbox\hbox
     {\nulldelimiterspace\zeropoint
      \normalstartimath\mathsurround\zeropoint#1\radical\zerocount{#2}\normalstopimath}%
   \ht\scratchbox-\dimexpr
     \ifx#1\displaystyle
            \fontdimen\pluseight\textfont\plusone
       + .25\fontdimen\plusfive \textfont\plusone
     \else
        1.25\fontdimen\pluseight
         \ifx#1\textstyle  \textfont        \else
         \ifx#1\scriptstyle\scriptfont      \else
                           \scriptscriptfont\fi\fi \plusone
     \fi
     -\ht\scratchbox
   \relax
   \box\scratchbox
   \endgroup}

%D \macros
%D    {cramedllap, crampedrlap, crampedclap}
%D
%D In many cases, one wants to use the \tex {clap} macro in
%D math mode while using subscripts. Normally, subscripts are
%D in cramped mode, and these macros allow you to go to
%D cramped mode. For example:
%D
%D \startbuffer
%D \startformula
%D   \sum_{a^2 < b^2 < c^2} \quad
%D   \sum_{\clap{a^2 < b^2 < c^2}} \quad
%D   \sum_{\crampedclap{a^2 < b^2 < c^2}}
%D \stopformula
%D \stopbuffer
%D
%D \start
%D   \typebuffer
%D   \switchtobodyfont[24pt]
%D   \getbuffer
%D \stop
%D
%D Notice that \type {\clap{\cramped ... }} would also work,
%D but it requires \TEX\ to typeset it arguments 16 times.

\unexpanded\def\crampedllap{\mathpalette\math_cramped_llap}
\unexpanded\def\crampedrlap{\mathpalette\math_cramped_rlap}
\unexpanded\def\crampedclap{\mathpalette\math_cramped_clap}

\def\math_cramped_llap#1#2{{}\dollap{\math_cramped #1{#2}}}
\def\math_cramped_rlap#1#2{{}\dorlap{\math_cramped #1{#2}}}
\def\math_cramped_clap#1#2{{}\doclap{\math_cramped #1{#2}}}

% experiment, not yet to be used

% \unexpanded\def\displaybreak
%   {\ifhmode
%      \removeunwantedspaces
%      \ifcase\raggedstatus\hfill\fi
%      \strut\penalty-9999 % \break fails on case (3)
%    \fi}
%
% \def\startdisplay{\displaybreak\ignorespaces\startpacked}
% \def\stopdisplay {\stoppacked\displaybreak\ignorespaces}

\let\superscript  \Usuperscript
\let\subscript    \Usubscript
\let\nosuperscript\Unosuperscript
\let\nosubscript  \Unosubscript

\protect \endinput
