summaryrefslogtreecommitdiff
path: root/tex/context/base/math-ini.mkiv
diff options
context:
space:
mode:
Diffstat (limited to 'tex/context/base/math-ini.mkiv')
-rw-r--r--tex/context/base/math-ini.mkiv544
1 files changed, 527 insertions, 17 deletions
diff --git a/tex/context/base/math-ini.mkiv b/tex/context/base/math-ini.mkiv
index 062631b39..b87096661 100644
--- a/tex/context/base/math-ini.mkiv
+++ b/tex/context/base/math-ini.mkiv
@@ -1,8 +1,8 @@
%D \module
%D [ file=math-ini,
%D version=2008.01.02,
-%D title=\CONTEXT\ Lua Macros,
-%D subtitle=Math Initializations,
+%D title=\CONTEXT\ Math Macros,
+%D subtitle=Initializations,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright=PRAGMA]
@@ -11,39 +11,549 @@
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.
+\writestatus{loading}{ConTeXt Math Macros / Initializations}
+
+%D This module provides namespaces for math fonts, thereby
+%D permitting mixed usage of math fonts. Although not strictly
+%D needed, we also provide a family name mapping mechanism as
+%D used in the (original) AMS math definition files, but here
+%D these names can recursively be remapped and if needed,
+%D dynamically be changed. We've tried to minimize the number
+%D of definition commands and use plain \TEX\ definitions as
+%D fallback. We've tried to follow a couple of conventions
+%D from plain and AMS math in order to achieve backward
+%D compatinility. We also kept an eye on future usage of these
+%D modules in the perspective of MathML and unicode fonts.
+
\unprotect
+\ifx\v!compact\undefined \def\v!compact{compact} \fi
+
%D We move these definitions into the format:
% test [[\char948 \ctxlua{tex.sprint(utf.char(948))}]]
% test $[[\char948 \ctxlua{tex.sprint(utf.char(948))}]]$
\registerctxluafile{math-ini}{1.001}
+\registerctxluafile{math-dim}{1.001}
\registerctxluafile{math-ent}{1.001}
+\registerctxluafile{math-ext}{1.001}
+\registerctxluafile{math-vfu}{1.001}
+\registerctxluafile{math-map}{1.001}
+\registerctxluafile{math-noa}{1.001}
+
+\definesystemattribute[mathalph]
+\definesystemattribute[mathsize]
+\definesystemattribute[mathpunc]
+
+% todo: only in mmode
+
+% \def\setmathattribute#1#2{\dosetattribute{mathalph}{\ctxlua{tex.sprint(mathematics.sync_a_both (\number\dogetattribute{mathalph},"#1","#2"))}}}
+% \def\setmathalphabet #1{\dosetattribute{mathalph}{\ctxlua{tex.sprint(mathematics.sync_a_name (\number\dogetattribute{mathalph},"#1"))}}}
+% \def\setmathstyle #1{\dosetattribute{mathalph}{\ctxlua{tex.sprint(mathematics.sync_a_style(\number\dogetattribute{mathalph},"#1"))}}}
+
+\def\setmathattribute#1#2{\ctxlua{mathematics.sync_a_both ("#1","#2")}}
+\def\setmathalphabet #1{\ctxlua{mathematics.sync_a_name ("#1")}}
+\def\setmathstyle #1{\ctxlua{mathematics.sync_a_style("#1")}}
+
+\unexpanded\def\mr {\setmathattribute{regular}{tf}}
+
+\unexpanded\def\mathdefault {\setmathattribute{regular}{it}}
+\unexpanded\def\mathscript {\setmathalphabet{script}}
+\unexpanded\def\mathfraktur {\setmathalphabet{fraktur}}
+\unexpanded\def\mathblackboard{\setmathalphabet{blackboard}}
+
+\unexpanded\def\mathrm{\setmathattribute{rm}{tf}}
+\unexpanded\def\mathss{\setmathattribute{ss}{tf}}
+\unexpanded\def\mathtt{\setmathattribute{tt}{tf}}
+
+\unexpanded\def\mathtf{\setmathstyle{tf}}
+\unexpanded\def\mathbf{\setmathstyle{bf}}
+\unexpanded\def\mathsl{\setmathstyle{sl}}
+\unexpanded\def\mathit{\setmathstyle{it}}
+\unexpanded\def\mathbs{\setmathstyle{bs}}
+\unexpanded\def\mathbi{\setmathstyle{bi}}
+
+\let\tfmath\mathtf % maybe a grouped command
+\let\bfmath\mathbf
+\let\slmath\mathsl
+\let\itmath\mathit
+\let\bsmath\mathbs
+\let\bimath\mathbi
-% \registerctxluafile{math-def}{1.001}
-% \ctxlua{mathematics.traditional()}
+\let\Bbb\mathblackboard
-\ctxlua{mathematics.define()}
-\ctxlua{mathematics.register_xml_entities()}
+\unexpanded\def\frak {\ifmmode\expandafter\mathfraktur \fi}
+\unexpanded\def\cal {\ifmmode\expandafter\mathscript \fi}
+\unexpanded\def\bbd {\ifmmode\expandafter\mathblackboard\fi}
+\unexpanded\def\blackboard{\ifmmode\expandafter\mathblackboard\fi}
+\unexpanded\def\fraktur {\ifmmode\expandafter\mathfraktur \fi}
+\unexpanded\def\gothic {\ifmmode\expandafter\mathfraktur \fi}
+
+\unexpanded\def\mathcal #1{{\setmathalphabet{script}#1}} % for AMS compatibility
+\unexpanded\def\mathfrak#1{{\setmathalphabet{fraktur}#1}} % for AMS compatibility
+\unexpanded\def\mathbb #1{{\setmathalphabet{blackboard}#1}} % for AMS compatibility
+
+\let\normalmr\mr
+
+\prependtoks
+ \let\mr\normalmr
+ \let\rm\mathrm \let\ss\mathss \let\tt\mathtt
+ \let\tf\mathtf \let\bf\mathbf \let\it\mathit \let\sl\mathsl \let\bi\mathbi \let\bs\mathbs
+ \let\frak\mathfraktur \let\cal\mathscript \let\bbd\mathblackboard
+ \mathdefault
+\to \everymathematics
+
+%D \macros
+%D {boldsymbol}
+%D
+%D The math definition is inspired by amsmath.
+%D
+%D \startbuffer
+%D \definetypeface [boldmath] [mm] [boldmath] [latin-modern] [modern] [encoding=texnansi]
+%D
+%D $a \times b$ $a \boldsymbol{\times} b$
+%D \stopbuffer
+%D
+%D \typebuffer \start \getbuffer \stop
+
+\let\mathboldsymbol\relax % yet unsupported, will be
+
+\def\boldsymbol
+ {\mathortext\mathboldsymbol\bold}
+
+%D Helpers:
\def\utfmathclass #1{\ctxlua{tex.sprint(mathematics.utfmathclass ("#1"))}}
\def\utfmathstretch#1{\ctxlua{tex.sprint(mathematics.utfmathstretch("#1"))}}
\def\utfmathcommand#1{\ctxlua{tex.sprint(mathematics.utfmathcommand("#1"))}}
\def\utfmathfiller #1{\ctxlua{tex.sprint(mathematics.utfmathfiller ("#1"))}}
-\def\utfmathclassdefault #1#2{\ctxlua{
- tex.sprint(mathematics.utfmathclass("#1","#2"))
-}}
+% \def\utfmathclassdefault #1#2{\ctxlua{
+% tex.sprint(mathematics.utfmathclass("#1","#2"))
+% }}
+%
+% \def\utfmathcommanddefault#1#2#3{\ctxlua{
+% local cmd = mathematics.utfmathcommand("#1","") or ""
+% if cmd == "" then
+% commands.cs("#2","#3")
+% else
+% commands.cs(cmd)
+% end}}
+
+% % %
+
+\def\@@mathlimopcomm #1{\mathop{#1}} %no \limits
+\def\@@mathnolopcomm #1{\mathop{#1}\nolimits}
+\def\@@mathboxcomm #1{\dontleavehmode\hbox{$\mathsurround\zeropoint#1$}}
+\def\@@mathchoicecomm#1{[todo #1]}
+
+\chardef\mathordcode = 0 \let\mathordcomm \mathord
+\chardef\mathopcode = 1 \let\mathopcomm \mathop
+\chardef\mathbincode = 2 \let\mathbincomm \mathbin
+\chardef\mathrelcode = 3 \let\mathrelcomm \mathrel
+\chardef\mathopencode = 4 \let\mathopencomm \mathopen
+\chardef\mathclosecode = 5 \let\mathclosecomm \mathclose
+\chardef\mathpunctcode = 6 \let\mathpunctcomm \mathpunct
+\chardef\mathalphacode = 7 \let\mathalphacomm \firstofoneargument
+\chardef\mathinnercode = 0 \let\mathinnercomm \mathinner
+\chardef\mathnothingcode= 0 \let\mathnothingcomm \firstofoneargument
+\chardef\mathlimopcode = 1 \let\mathlimopcomm \@@mathlimopcomm
+\chardef\mathnolopcode = 1 \let\mathnolopcomm \@@mathnolopcomm
+\chardef\mathchoicecode = 0 \let\mathchoicecomm \@@mathchoicecomm
+\chardef\mathboxcode = 0 \let\mathboxcomm \@@mathboxcomm
+
+\chardef\mathaccentcode = 8
+\chardef\mathradicalcode= 9
+
+\def\puremathcode#1{\the\csname math#1code\endcsname}
+\def\puremathcomm#1{\csname math#1comm\endcsname}
+
+% \startlines
+% $\mathopnolimits{\rm d}x$
+% $\mathopnolimits{\kern\zeropoint \rm d}x$
+% $\puremathcomm{nolop}{\rm d}x$
+% $\puremathcomm{nolop}{\kern\zeropoint\rm d}x$
+% \blank
+% $\puremathcomm{nolop}{\mr d}x$
+% $\puremathcomm{nolop}{\kern\zeropoint\mr d}x$
+% $\mathop{\kern\zeropoint\mr d}x$
+% $\mathopnolimits{\kern\zeropoint d}x$
+% \stoplines
+
+\newif\iftracemathcollection
+
+% this will be sorted out:
+
+\let\mathcharacter \getvalue
+\let\textcharacter \getvalue
+\def\definefamilysynonym {\dotripleempty\dodefinefamilysynonym}
+\def\dodefinefamilysynonym [#1][#2][#3]{}
+\def\definemathsymbol {\dosixtupleempty\dodefinemathsymbol}
+\def\dodefinemathsymbol [#1][#2][#3][#4][#5][#6]{}
+\def\definemathcharacter {\dosixtupleempty\dodefinemathcharacter}
+\def\redefinemathcharacter {\dosixtupleempty\dodefinemathcharacter}
+\def\dodefinemathcharacter [#1][#2][#3][#4][#5][#6]{}
+\def\startmathcollection [#1]{}
+\def\setmathcollection #1{}
+\def\stopmathcollection {}
+\def\startrawmathcollection {}
+\def\stoprawmathcollection {}
+\def\setmathtoks {}
+\let\currentmathcollection \s!default
+\let\nomathcollection \s!default
+\let\mathcollection \s!default
+\def\enablemathcollection [#1]{}
+\def\usemathcollection {\dodoubleempty\dousemathcollection}
+\def\dousemathcollection [#1][#2]{}
+\let\mathclass \nomathcollection
+\let\autoenablemathcollection\relax
+\def\resetmathcollection [#1]{}
+
+\def\definemathcommand
+ {\dotripleempty\dodefinemathcommand}
+
+\def\dodefinemathcommand[#1][#2][#3]#4% command class args meaning
+ {\ifthirdargument
+ \processaction
+ [#3]
+ [one=>\unexpanded\setvalue{#1}##1{\puremathcomm{#2}{#4{##1}}},
+ two=>\unexpanded\setvalue{#1}##1##2{\puremathcomm{#2}{#4{##1}{##2}}}]%
+ \else\ifsecondargument
+ \unexpanded\setvalue{#1}{\puremathcomm{#2}{#4}}%
+ \else
+ \unexpanded\setvalue{#1}{\puremathcomm{nothing}{#4}}%
+ \fi\fi}
+
+%D Moved from font-ini.mkiv:
+%D
+%D \macros
+%D {mf,mbox,enablembox,mathop}
+%D
+%D Todo:
+
+\unexpanded\def\mf
+ {\csname\fontalternative\endcsname}
+
+\let\normalmathop\mathop
+
+\unexpanded\def\mathop
+ {\normalmathop
+ \bgroup
+ \let\rm\mf
+ \let\next=}
+
+\def\normalmbox
+ {\normalhbox\bgroup\mf
+ \dowithnextbox{\flushnextbox\egroup}\normalhbox}
+
+\def\mbox
+ {\ifmmode\normalmbox\else\normalhbox\fi}
+
+\def\enablembox
+ {\appendtoks
+ \ifx\normalhbox\undefined\let\normalhbox\hbox\fi
+ \let\hbox\mbox
+ \to\everymathematics}
+
+%D needed for sin, cos etc
+
+\def\mfunction#1{{\mr#1}}
+
+% \def\mlimitsfunction #1{\mathlimopcomm{{\mr#1}}
+% \def\mnolimitsfunction#1{\mathnolopcomm{{\mr#1}}
+
+%D Taco posted this solution as response to a mail by Olivier, so
+%D let's integrate it here.
+
+\def\currentmscaledstyle{rm} % will be plugged into the typeface text=ss option
+
+\def\setmathfunctionstyle#1% rm ss tt
+ {\doifsomething{#1}
+ {\def\currentmscaledstyle{#1}%
+ \def\mathopnolimits##1{\mathop{\mscaledtext{##1}}\nolimits}%
+ \def\mfunction##1{\mscaledtext{##1}}}}
+
+\def\mscaledtext#1%
+ {\mathchoice
+ {\hbox{\csname\currentmscaledstyle\endcsname\tf #1}}
+ {\hbox{\csname\currentmscaledstyle\endcsname\tf #1}}
+ {\hbox{\csname\currentmscaledstyle\endcsname\tfx #1}}
+ {\hbox{\csname\currentmscaledstyle\endcsname\tfxx#1}}}
+
+%D We can force the way functions are typeset by manipulating the text
+%D option:
+%D
+%D \starttyping
+%D \definetypeface[iwona][ss][sans][iwona][default][encoding=texnansi]
+%D \definetypeface[iwona][mm][math][iwona][default][encoding=texnansi,text=ss]
+%D \stoptyping
+%D
+%D This hooks into the math handler with:
+
+\appendtoks
+ \setmathfunctionstyle\currentmathtextstyle
+\to \everybodyfont
+
+%D Usage:
+%D
+%D \starttyping
+%D \setmathfunctionstyle\fontstyle % or {rm} or {ss} or ..
+%D \rm test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
+%D \ss test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
+%D \tt test $\sin{(x^{\sin(x^{\sin(x)})})}$ test
+%D \stoptyping
+
+%D Some goodies:
+
+\def\Angstrom{\nomathematics{\Aring}}
+
+%D \macros
+%D {nonknuthmode, donknuthmode}
+%D
+%D The underscore is frequently used in manuals but unfortunately \TEX\ prefers
+%D it to be a math specific character. And since computer modern fonts didn't
+%D have an underscore, one had to use commands to fake one. Nowadays we do
+%D have underscores in latin modern, and since all other fonts have them, we
+%D decided to get away from the restriction to use the underscore character in
+%D text mode.
+%D
+%D \starttyping
+%D \def\test#1{#1}
+%D
+%D \nonknuthmode $x_2$ x_2 \test{$x_2$} \test{x_2}
+%D
+%D \donknuthmode $x_2$ x_2 \test{$x_2$} \test{x_2}
+%D \stoptyping
+%D
+%D The result is as expected: the first line typesets ok, while the second
+%D one triggers an error message.
+
+\bgroup
+
+ \ifx\normalsuber\undefined \def\normalsuber{_} \fi
+ \ifx\normalsuper\undefined \def\normalsuper{^} \fi
+
+ \catcode`_=\active
+ \catcode`^=\active
+
+ \gdef\nonknuthmode
+ {\appendtoks\let_\normalsuber\let^\normalsuper\to\everymathematics
+ \mathcode`_="8000
+ \mathcode`^="8000
+ \catcode`_=\@@other
+ \catcode`^=\@@other
+ \let\nonknuthmode\relax}
+
+ \gdef\donknuthmode
+ {\catcode`_=\@@subscript
+ \catcode`^=\@@superscript}
+
+\egroup
+
+%D Needed for unicode:
+
+\def\nulloperator{\mathortext{\mathop{\null}}{\null}}
+
+%D To be dealt with ...
+
+\mathcode`\ ="8000 % \space
+\mathcode`\'="8000 % ^\prime
+\mathcode`\_="8000 % \_
+
+%D \macros
+%D {\setupmathematics}
+%D
+%D Configuration for integrals. (If needed we can speed this up and make it
+%D installable; no processaction is needed then).
+
+\newtoks\everysetupmathematics
+
+\def\setupmathematics
+ {\dosingleargument\dosetupmathematics}
+
+\def\dosetupmathematics[#1]%
+ {\getparameters[\??mo][#1]%
+ \the\everysetupmathematics}
+
+\def\mathematicsparameter#1{\ifcsname\??mo#1\endcsname\csname\??mo#1\endcsname\fi}
+
+%D Memory saver:
+
+\appendtoks
+ \doifelse{\mathematicsparameter\v!compact}\v!yes
+ {\ctxlua{fonts.vf.math.optional=true}}
+ {\ctxlua{fonts.vf.math.optional=false}}%
+\to \everysetupmathematics
+
+\setupmathematics
+ [\v!compact=no]
+
+%D \macros
+%D {enablemathpunctuation,disablemathpunctuation}
+%D
+%D \startbuffer
+%D \enablemathpunctuation$(1,2) (1, 2) (1{,}2) \hbox{foo, not bar}$
+%D \stopbuffer
+%D
+%D \typebuffer
+%D
+%D \blank{\getbuffer}\blank
+
+\setfalse \automathpunctuation
+
+\def\enablemathpunctuation {\settrue \automathpunctuation}
+\def\disablemathpunctuation{\setfalse\automathpunctuation}
+
+\ifx\v!autopunctuation\undefined \def\v!autopunctuation{autopunctuation} \fi
+
+\appendtoks
+ \doifelse{\mathematicsparameter\v!autopunctuation}\v!yes\enablemathpunctuation\disablemathpunctuation
+\to \everysetupmathematics
+
+\appendtoks
+ \ifconditional\automathpunctuation\dosetattribute{mathpunc}\plusone\fi
+\to \everymathematics
+
+\setupmathematics
+ [\v!autopunctuation=\v!no]
+
+%D \macros
+%D {mathstyle}
+%D
+%D If one want to be sure that something is typeset in the
+%D appropriate style, \type {\mathstyle} can be used:
+%D
+%D \starttyping
+%D \mathstyle{something}
+%D \stoptyping
+
+% \def\mathstyle#1%
+% {\mathchoice
+% {\displaystyle #1}%
+% {\textstyle #1}%
+% {\scriptstyle #1}%
+% {\scriptscriptstyle#1}}
+%
+% We now have a primitive operation for this. As the
+% macro overloads a new primitive introduced in \LUATEX,
+% we need to use \type {\normalmathstyle} when we consult
+% the current math style.
+%
+% \let \mathstyle \Ustack % spoils cramped
+%
+% \let \mathstyle \firstofoneargument
+%
+% 0 = display
+% 1 = crampeddisplay
+% 2 = text
+% 3 = crampedtext
+% 4 = script
+% 5 = crampedscript
+% 6 = scriptscript
+% 7 = crampedscriptscript
+
+\def\uncramped#1%
+ {{\ifcase\normalmathstyle
+ \or \displaystyle \or
+ \or \textstyle \or
+ \or \scriptstyle \or
+ \or \scriptscriptstyle \fi
+ #1}}
+
+\def\cramped#1%
+ {{\ifcase\normalmathstyle
+ \crampeddisplaystyle \or \or % 0 -> 1
+ \crampedtextstyle \or \or % 2 -> 3
+ \crampedscriptstyle \or \or % 4 -> 5
+ \crampedscriptscriptstyle \fi % 6 -> 7
+ #1}}
+
+\def\triggermathstyle#1% #1 is number
+ {\ifcase#1\relax
+ \displaystyle \or
+ \crampeddisplaystyle \or
+ \textstyle \or
+ \crampedtextstyle \or
+ \scriptstyle \or
+ \crampedscriptstyle \or
+ \scriptscriptstyle \or
+ \crampedscriptscriptstyle \else
+ % error
+ \fi}
+
+\def\cramped#1%
+ {{\ifcase\normalmathstyle
+ \crampeddisplaystyle \or \or % 0 -> 1
+ \crampedtextstyle \or \or % 2 -> 3
+ \crampedscriptstyle \or \or % 4 -> 5
+ \crampedscriptscriptstyle \fi % 6 -> 7
+ #1}}
+
+%D Something similar can be used in the (re|)|definition
+%D of \type {\text}. This version is a variation on the one
+%D in the math module (see \type{m-math} and|/|or \type
+%D {m-newmat}).
+
+\unexpanded\def\mathtext
+ {\mathortext\domathtext\hbox}
+
+\def\domathtext#1%
+ {\mathchoice
+ {\dodomathtext\displaystyle\textface {#1}}%
+ {\dodomathtext\textstyle \textface {#1}}%
+ {\dodomathtext\textstyle \scriptface {#1}}%
+ {\dodomathtext\textstyle \scriptscriptface{#1}}}
+
+\def\dodomathtext#1#2#3% no \everymath !
+ %{\hbox{\everymath{#1}\switchtobodyfont [#2]#3}} % 15 sec
+ {\hbox{\everymath{#1}\setcurrentfontbody{#2}#3}} % 3 sec (no math)
+
+%D Because we may overload \type {\text} in other (structuring)
+%D macros, we say:
+
+\appendtoks \let\text\mathtext \to \everymathematics
+
+%D The next code is derived from plain \TEX.
+
+\newcount\interdisplaylinepenalty \interdisplaylinepenalty=100
+
+\newif\ifdt@p
+
+\def\displ@y
+ {\global\dt@ptrue
+ \openup\displayopenupvalue % was \openup\jot
+ \everycr
+ {\noalign
+ {\ifdt@p
+ \global\dt@pfalse
+ \ifdim\prevdepth>-\thousandpoint
+ \vskip-\lineskiplimit
+ \vskip\normallineskiplimit
+ \fi
+ \else
+ \penalty\interdisplaylinepenalty
+ \fi}}}
+
+\let\normaldispl@y\displ@y
+
+\def\displ@y{\resetdisplaymatheq\normaldispl@y}
+
+\def\m@th{\mathsurround\zeropoint} % obsolete
+
+%D Text in math:
+
+\def\mathortext
+ {\ifmmode
+ \expandafter\firstoftwoarguments
+ \else
+ \expandafter\secondoftwoarguments
+ \fi}
+
+% \defineactivecharacter _ {\mathortext{_}{\_}} text_text $a^2$
-\def\utfmathcommanddefault#1#2#3{\ctxlua{
- local cmd = mathematics.utfmathcommand("#1","") or ""
- if cmd == "" then
- commands.cs("#2","#3")
- else
- commands.cs(cmd)
- end}}
+% force text mode, will be overloaded later
-% \let\math@normal@int\int \def\int{\math@normal@int\intlimits}
+\ifx\text\undefined \let\text\hbox \fi
\protect \endinput