summaryrefslogtreecommitdiff
path: root/tex/context/base/math-fen.mkiv
blob: 45f6288629090bb1e8e9993b888b2e6d53be3fc1 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
%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\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\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\fi
                        \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_start#1%
  {\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}

\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 [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\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}

\unexpanded\def\left {\afterassignment\math_left \let\next=}
\unexpanded\def\right{\afterassignment\math_right\let\next=}

% \def\math_left
%   {\csname\??mathleft
%      \ifcsname\??mathleft\meaning\next\endcsname
%        \meaning\next
%      \else
%        \s!unknown
%      \fi
%    \endcsname}

% \def\math_right
%   {\csname\??mathright
%      \ifcsname\??mathright\meaning\next\endcsname
%        \meaning\next
%      \else
%        \s!unknown
%      \fi
%    \endcsname}

\def\math_left
  {\edef\m_math_left{\meaning\next}%
   \csname\??mathleft \ifcsname\??mathleft \m_math_left \endcsname\m_math_left \else\s!unknown\fi\endcsname}

\def\math_right
  {\edef\m_math_right{\meaning\next}%
   \csname\??mathright\ifcsname\??mathright\m_math_right\endcsname\m_math_right\else\s!unknown\fi\endcsname}

\setvalue{\??mathleft \s!unknown}{\normalleft \next}
\setvalue{\??mathright\s!unknown}{\normalright\next}

\bgroup

\let\next[ \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Lbracket   \let\next] \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rbracket
\let\next( \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Lparent    \let\next) \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rparent
\let\next< \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Langle     \let\next> \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rangle
\let\next{ \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Lbrace     \let\next} \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rbrace
\let\next| \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Lbar       \let\next| \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rbar
\let\next‖ \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Ldoublebar \let\next‖ \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rdoublebar
\let\next. \global\expandafter\let\csname\??mathleft\meaning\next\endcsname\Lnothing   \let\next. \global\expandafter\let\csname\??mathright\meaning\next\endcsname\Rnothing

\egroup

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

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

\protect