%D \module %D [ file=math-fen, %D version=2012.02.18, %D title=\CONTEXT\ Math Macros, %D subtitle=Fences, %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. \writestatus{loading}{ConTeXt Math Macros / Fences} \unprotect % todo: mathstyle % \definemathfence [fancybracket] [bracket] [command=yes,color=blue] % % test $|x|$ test \par % test $||x||$ test (okay) \par % test $a\left|\frac{1}{b}\right|c$ test \par % test $a\left||\frac{1}{b}\right||c$ test (not okay) \par % % \setupmathfences [color=red] % % test $a\fenced[bar]{\frac{1}{b}}c$ test \par % test $a\fenced[doublebar]{\frac{1}{b}}c$ test \par % test $a\fenced[bracket]{\frac{1}{b}}c$ test \par % test $a\fancybracket{\frac{1}{b}}c$ test \par \installcorenamespace{mathfences} \installcommandhandler \??mathfences {mathfence} \??mathfences \let\setupmathfences\setupmathfence \setupmathfences [\c!left=, \c!right=, \c!middle=, \c!mathstyle=, \c!color=, \c!command=] \appendtoks \edef\p_command{\mathfenceparameter\c!command}% \ifx\p_command\v!yes \setuevalue\currentmathfence{\math_fenced_fenced[\currentmathfence]}% \fi \to \everydefinemathfence % we need the direct use of \Udelimiter because of { etc \def\math_fenced_left {\edef\p_left{\mathfenceparameter\c!left}% \math_fenced_color_push \normalleft\ifx\p_left\empty.\else\Udelimiter\plusfour\fam\p_left\relax\fi \math_fenced_color_pop} \def\math_fenced_middle{\edef\p_middle{\mathfenceparameter\c!middle}% \mskip\thinmuskip \math_fenced_color_push \normalmiddle\ifx\p_middle\empty.\else\Udelimiter\plusfour\fam\p_middle\relax\fi \math_fenced_color_pop \mskip\thinmuskip} \def\math_fenced_right {\edef\p_right{\mathfenceparameter\c!right}% \math_fenced_color_push \normalright\ifx\p_right\empty.\else\Udelimiter\plusfive\fam\p_right\relax\fi \math_fenced_color_pop} \def\math_fenced_color_do_push{\pushcolor[\p_math_fenced_color]} \let\math_fenced_color_do_pop \popcolor \let\math_fenced_color_push\donothing \let\math_fenced_color_pop \donothing \let\fence \relax \let\fenced\relax \newcount\c_math_fenced_nesting \unexpanded\def\math_fenced_fenced_start#1% {\advance\c_math_fenced_nesting\plusone \begingroup \edef\currentmathfence{#1}% \startusemathstyleparameter\mathfenceparameter \let\fence\math_fenced_middle \edef\p_math_fenced_color{\mathfenceparameter\c!color}% \ifx\p_math_fenced_color\empty \let\math_fenced_color_push\donothing \let\math_fenced_color_pop \donothing \else \let\math_fenced_color_push\math_fenced_color_do_push \let\math_fenced_color_pop \math_fenced_color_do_pop \fi \math_fenced_left} \unexpanded\def\math_fenced_fenced_stop#1% {\edef\currentmathfence{#1}% \math_fenced_right \stopusemathstyleparameter \endgroup \advance\c_math_fenced_nesting\minusone} \unexpanded\def\math_fenced_fenced[#1]#2% {\math_fenced_fenced_start{#1}% #2% \math_fenced_right \stopusemathstyleparameter \endgroup} \appendtoks \let\fenced\math_fenced_fenced \to \everymathematics % todo: | in mathmode letter % % \appendtoks % \let\bar\letterbar % \to \everymathematics % % but then we don't have it in embedded text too so ... \definemathfence [parenthesis] [\c!left="0028,\c!right="0029] \definemathfence [bracket] [\c!left="005B,\c!right="005D] \definemathfence [brace] [\c!left="007B,\c!right="007D] \definemathfence [bar] [\c!left="007C,\c!right="007C] \definemathfence [doublebar] [\c!left="2016,\c!right="2016] \definemathfence [angle] [\c!left="003C,\c!right="003E] \definemathfence [solidus] [\c!left="2044,\c!right="2044] \definemathfence [nothing] %D A bonus: \unexpanded\def\Lparent {\math_fenced_fenced_start{parenthesis}} \unexpanded\def\Rparent {\math_fenced_fenced_stop{parenthesis}} \unexpanded\def\Lbracket {\math_fenced_fenced_start{bracket}} \unexpanded\def\Rbracket {\math_fenced_fenced_stop{bracket}} \unexpanded\def\Lbrace {\math_fenced_fenced_start{brace}} \unexpanded\def\Rbrace {\math_fenced_fenced_stop{brace}} \unexpanded\def\Langle {\math_fenced_fenced_start{angle}} \unexpanded\def\Rangle {\math_fenced_fenced_stop{angle}} \unexpanded\def\Lbar {\math_fenced_fenced_start{bar}} \unexpanded\def\Rbar {\math_fenced_fenced_stop{bar}} \unexpanded\def\Lsolidus {\math_fenced_fenced_start{solidus}} \unexpanded\def\Rsolidus {\math_fenced_fenced_stop{solidus}} \unexpanded\def\Ldoublebar {\math_fenced_fenced_start{doublebar}} \unexpanded\def\Rdoublebar{\math_fenced_fenced_stop{doublebar}} \unexpanded\def\Lnothing {\math_fenced_fenced_start{nothing}} \unexpanded\def\Rnothing {\math_fenced_fenced_stop{nothing}} %D And another one: % \setupmathfences[color=darkgreen] % % \startformula % \left{ \frac{1}{a} \right} % \left[ \frac{1}{b} \right] % \left( \frac{1}{c} \right) % \left< \frac{1}{d} \right> % \left| \frac{1}{e} \right| % \stopformula \installcorenamespace{mathleft} \installcorenamespace{mathright} \installcorenamespace{mathmiddle} \unexpanded\def\left {\afterassignment\math_left \let\nexttoken} \unexpanded\def\right {\afterassignment\math_right \let\nexttoken} \unexpanded\def\middle{\afterassignment\math_middle\let\nexttoken} \newconditional\c_math_fenced_done \newconditional\c_math_fenced_unknown \settrue\c_math_fenced_unknown \def\math_left {\settrue\c_math_fenced_done \edef\m_math_left{\meaning\nexttoken}% \csname\??mathleft\ifcsname\??mathleft\m_math_left\endcsname\m_math_left\else\s!unknown\fi\endcsname} \def\math_right {\settrue\c_math_fenced_done \edef\m_math_right{\meaning\nexttoken}% \csname\??mathright\ifcsname\??mathright\m_math_right\endcsname\m_math_right\else\s!unknown\fi\endcsname} \def\math_middle {\settrue\c_math_fenced_done \edef\m_math_middle{\meaning\nexttoken}% \csname\??mathmiddle\ifcsname\??mathmiddle\m_math_middle\endcsname\m_math_middle\else\s!unknown\fi\endcsname} \setvalue{\??mathleft \s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalleft \nexttoken\fi} \setvalue{\??mathright \s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalright \nexttoken\fi} \setvalue{\??mathmiddle\s!unknown}{\setfalse\c_math_fenced_done\ifconditional\c_math_fenced_unknown\normalmiddle\nexttoken\fi} \unexpanded\def\installmathfencepair#1#2#3#4% {\expandafter\let\csname\??mathleft \meaning#1\endcsname#2% \expandafter\let\csname\??mathright\meaning#3\endcsname#4} \expandafter\let\csname\??mathleft\meaning [\endcsname\Lbracket \expandafter\let\csname\??mathleft\meaning (\endcsname\Lparent \expandafter\let\csname\??mathleft\meaning <\endcsname\Langle \expandafter\let\csname\??mathleft\meaning {\endcsname\Lbrace \expandafter\let\csname\??mathleft\meaning |\endcsname\Lbar \expandafter\let\csname\??mathleft\meaning /\endcsname\Lsolidus \expandafter\let\csname\??mathleft\meaning ‖\endcsname\Ldoublebar \expandafter\let\csname\??mathleft\meaning .\endcsname\Lnothing \expandafter\let\csname\??mathright\meaning ]\endcsname\Rbracket \expandafter\let\csname\??mathright\meaning )\endcsname\Rparent \expandafter\let\csname\??mathright\meaning >\endcsname\Rangle \expandafter\let\csname\??mathright\meaning }\endcsname\Rbrace \expandafter\let\csname\??mathright\meaning |\endcsname\Rbar \expandafter\let\csname\??mathright\meaning /\endcsname\Rsolidus \expandafter\let\csname\??mathright\meaning ‖\endcsname\Rdoublebar \expandafter\let\csname\??mathright\meaning .\endcsname\Rnothing \let\lbrack\lbracket \let\rbrack\rbracket \installmathfencepair \lbrace \Lbrace \rbrace \Rbrace \installmathfencepair \lbracket \Lbracket \rbracket \Rbracket \installmathfencepair \lparent \Lparent \rparent \Rparent \installmathfencepair \langle \Langle \rangle \Rangle %installmathfencepair \lbar \Lbar \rbar \Rbar \installmathfencepair \vert \Lbar \vert \Rbar \installmathfencepair \solidus \Lsolidus \solidus \Rsolidus \unexpanded\def\{{\mathortext\lbrace \letterleftbrace } % or maybe a chardef \unexpanded\def\}{\mathortext\rbrace \letterrightbrace } % or maybe a chardef \unexpanded\def\[{\mathortext\lbracket\letterleftbracket } % or maybe a chardef \unexpanded\def\]{\mathortext\rbracket\letterrightbracket} % or maybe a chardef \unexpanded\def\({\mathortext\lparent \letterleftparent } % or maybe a chardef \unexpanded\def\){\mathortext\rparent \letterrightparent } % or maybe a chardef \unexpanded\def\|{\mathortext\vert \letterbar } % or maybe a chardef %unexpanded\def\/{\mathortext\solidus \letterslash } % or maybe a chardef \installmathfencepair \{ \Lbrace \} \Rbrace \installmathfencepair \[ \Lbracket \] \Rbracket \installmathfencepair \( \Lparent \) \Rparent \installmathfencepair \< \Langle \> \Rangle \installmathfencepair \| \Lbar \| \Rbar %D As we have overloaded \type {\left} and \type {\right} we also need a more %D clever version of the following: % methods: % % 1: none % 2: lua % 3: tex % variants: % % 1: step 1 % 2: step 2 % 3: htdp * 1.33^n % 4: size * 1.33^n \setnewconstant\bigmathdelimitermethod \plusone \setnewconstant\bigmathdelimitervariant\plusthree \unexpanded\def\plainbigdelimiters % traditional method {\bigmathdelimitermethod\plustwo} \plainbigdelimiters % is default for the moment but not so nice % \setconstant\bigmathdelimitermethod\plusone \installcorenamespace{mathbig} \unexpanded\def\choosemathbig#1#2% so we accept \big{||} as well {{\hbox\bgroup \startimath \ifcase\bigmathdelimitermethod \math_fenced_step#2\relax \or \attribute\mathsizeattribute\numexpr\bigmathdelimitervariant*\plushundred+#1\relax \math_fenced_step#2\relax \else \math_fenced_step#2{\vbox to\getvalue{\??mathbig\number#1}\bodyfontsize{}}% \fi \nulldelimiterspace\zeropoint\relax \mathsurround\zeropoint \stopimath \egroup}} \def\math_fenced_step#1#2% {\setfalse\c_math_fenced_unknown \setfalse\c_math_fenced_done \left#1\relax \ifconditional\c_math_fenced_done #2% \right.\relax \else \left.\relax #2% \setfalse\c_math_fenced_done \right#1\relax \ifconditional\c_math_fenced_done \else \right.\relax \fi \fi} \unexpanded\def\mathdelimiterstep#1#2% {\begingroup \attribute\mathsizeattribute\numexpr\plushundred+#1\relax \math_fenced_step#2\relax \endgroup} \setvalue{\??mathbig1}{0.85} \setvalue{\??mathbig2}{1.15} \setvalue{\??mathbig3}{1.45} \setvalue{\??mathbig4}{1.75} \definemathcommand [big] {\choosemathbig\plusone } \definemathcommand [Big] {\choosemathbig\plustwo } \definemathcommand [bigg] {\choosemathbig\plusthree} \definemathcommand [Bigg] {\choosemathbig\plusfour } \definemathcommand [bigl] [open] [one] {\big} \definemathcommand [bigm] [rel] [one] {\big} \definemathcommand [bigr] [close] [one] {\big} \definemathcommand [Bigl] [open] [one] {\Big} \definemathcommand [Bigm] [rel] [one] {\Big} \definemathcommand [Bigr] [close] [one] {\Big} \definemathcommand [biggl] [open] [one] {\bigg} \definemathcommand [biggm] [rel] [one] {\bigg} \definemathcommand [biggr] [close] [one] {\bigg} \definemathcommand [Biggl] [open] [one] {\Bigg} \definemathcommand [Biggm] [rel] [one] {\Bigg} \definemathcommand [Biggr] [close] [one] {\Bigg} %definemathfence [fancybracket] [bracket] [command=yes,color=red] % experimental accents: % % \definemathoverextensible [top] [hoed] ["FE302] % \definemathoverextensible [top] [slang] ["FE303] %D This is needed for mathml (used in mrow, so it gets reset): \let\math_fences_saved_left \left \let\math_fences_saved_middle\middle \let\math_fences_saved_right \right \unexpanded\def\math_fences_checked_left {\math_fences_saved_left} \unexpanded\def\math_fences_checked_middle {\ifcase\c_math_fenced_nesting \expandafter\math_fences_saved_middle \else \expandafter\firstofoneargument \fi} \unexpanded\def\math_fences_checked_right {\ifcase\c_math_fenced_nesting \expandafter\firstofoneargument \else \expandafter\math_fences_saved_right \fi} % \unexpanded\def\math_fences_checked_left_or_middle % {\ifcase\c_math_fenced_nesting % \expandafter\math_fences_saved_left % \else % \expandafter\math_fences_saved_middle % \fi} \newconditional\c_math_checked_done % only bars \unexpanded\def\math_fences_checked_left_or_right {\ifcase\c_math_fenced_nesting \settrue\c_math_checked_done \expandafter\math_fences_saved_left \else\ifconditional\c_math_checked_done \setfalse\c_math_checked_done \doubleexpandafter\math_fences_saved_right \else \doubleexpandafter\math_fences_saved_middle \fi\fi} \unexpanded\def\math_fences_checked_start {\c_math_fenced_nesting\zerocount} \unexpanded\def\math_fences_checked_stop {\ifcase\c_math_fenced_nesting\else \right.\relax % error, todo: nil spacing \expandafter\math_fences_checked_stop \fi} \unexpanded\def\startcheckedfences {\begingroup \let\left \math_fences_checked_left \let\middle\math_fences_checked_middle \let\right \math_fences_checked_right \math_fences_checked_start} \unexpanded\def\stopcheckedfences {\math_fences_checked_stop \endgroup} \let\leftorright\math_fences_checked_left_or_right % for bars %D The next characters were used for constructing nicer extensibles but %D nowadays we have real characters. \Umathchardef\braceld=0 \defaultmathfamily "FF07A \Umathchardef\bracerd=0 \defaultmathfamily "FF07B \Umathchardef\bracelu=0 \defaultmathfamily "FF07C \Umathchardef\braceru=0 \defaultmathfamily "FF07D \protect