summaryrefslogtreecommitdiff
path: root/tex/context/base/math-fen.mkiv
blob: 640821bad5c327e55f96e4b2d52ef4b23fa57f51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%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.

% 0x -> "

\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=0x2E,
   \c!right=0x2E,
   \c!middle=0x2E,
   \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

% no Uchar here:

\def\math_fenced_left  {\math_fenced_color_push\normalleft  \utfchar{\mathfenceparameter\c!left  }\math_fenced_color_pop}
\def\math_fenced_middle{\mskip\thinmuskip
                        \math_fenced_color_push\normalmiddle\utfchar{\mathfenceparameter\c!middle}\math_fenced_color_pop
                        \mskip\thinmuskip}
\def\math_fenced_right {\math_fenced_color_push\normalright \utfchar{\mathfenceparameter\c!right }\math_fenced_color_pop}

\def\math_fenced_color_do_push{\pushcolor[\p_math_fenced_color]}
\let\math_fenced_color_do_pop  \popcolor

\let\fence \relax
\let\fenced\relax

\unexpanded\def\math_fenced_fenced[#1]#2%
  {\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
   #2%
   \math_fenced_right
   \stopusemathstyleparameter
   \endgroup}

\appendtoks
    \let\fenced\math_fenced_fenced
\to \everymathematics

\definemathfence [parenthesis] [\c!left=0x0028,\c!right=0x0029]
\definemathfence [bracket]     [\c!left=0x005B,\c!right=0x005D]
\definemathfence [braces]      [\c!left=0x007B,\c!right=0x007D]
\definemathfence [bar]         [\c!left=0x007C,\c!right=0x007C]
\definemathfence [doublebar]   [\c!left=0x2016,\c!right=0x2016]
\definemathfence [angle]       [\c!left=0x003C,\c!right=0x003E]

%definemathfence [fancybracket] [bracket] [command=yes,color=red]

% experimental accents:
%
% \definemathoverextensible [top] [hoed]  ["FE302]
% \definemathoverextensible [top] [slang] ["FE303]

\protect