summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/math/math-oddities.tex
blob: 015a3012672b545d6f9a809fa53425eb7bf4cc21 (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
% language=uk

\environment math-layout

\startcomponent math-oddities

\startchapter[title=Things you might forget]

\startsection[title=Ampersands]

You can skip this, but if you continue reading, here is some low level plain code
(don't use this in \CONTEXT):

\starttyping
\def\matrix#1%
  {\null
   \,
   \vcenter
     {\normalbaselines
      \ialign{\hfil$##$\hfil && \quad\hfil$##$\hfil\crcr
      \mathstrut\crcr
      \noalign{\kern-\baselineskip}
      #1\crcr
      \mathstrut\crcr
      \noalign{\kern-\baselineskip}}}%
   \,}
\stoptyping

You see the \type {&} here and it's the alignment cell separator. The special
meaning of these characters is determined by the so called catcode. Here we have:

\starttyping
\catcode"26=4
\stoptyping

Character \type {0x26} is the ampersand. In \CONTEXT\ this character can be used
in text mode because we never use it as alignment character, which is something
typical \TEX. The same is true for \type {^} and \type {_}. So, effectively we
have (for instance):

\starttyping
\catcode"26=12
\stoptyping

In order to still get this \type {&} supported as alignment character in math
mode, we have to jump through some hoops. Think of this (again, don't do this in
\CONTEXT):

\starttyping
\bgroup
    \global\mathcode"26="8000

    \catcode"26=4

    \xdef\normalmathaligntab{&}

    \catcode"26=13

    \global\everymath{\def&{\normalmathaligntab}}
\egroup
\stoptyping

Before we go on you should realize that we never use the \type {&} in \CONTEXT\
as separator. The sole reason for dealing with this issue is that users can have
their own code that uses the ampersand that way. In \CONTEXT\ we do things like:

\starttyping
\startformula
    \startmatrix
        \NC 1 \NC 2 \NR
        \NC 3 \NC 4 \NR
    \stopmatrix
\stopformula
\stoptyping

Where \type {\NC} can be more powerful than a \type {&}. Anyhow, the reason for
discussing this here is that there can be surprises. In a running text you can do
this:

\starttyping
A & B
\stoptyping

Which procces okay and gives the ampersand as glyph. The following is also okay:

\starttyping
$A \Umathchar"2"0"26 B$
\stoptyping

However, the next one:

\starttyping
$A \char"26 B$
\stoptyping

fails with a \type {Misplaced alignment tab character &}. The reason is that
where in text mode \TEX's parser will turn the \type {\char} into a character
node and carry on afterwards, in math mode it will treat this inpout as were it a
directly input character, so the above is like, where the \type {&} has active
properties and becomes the sparator ampersand which then triggers the error:

\starttyping
$A & B$
\stoptyping

This means that we cannot have a definition like:

\starttyping
\def\AND{\char"26\relax}
\stoptyping

that can be used in math mode, which is why the \CWEB\ macros do:

\starttyping
\def\AND{\def\AND{\mathchar"2026\relax}\AND}
\stoptyping

Back to the plain example. The \type {\matrix} command has to be wrapped in
math mode and therefore the \type {&} will adapt, while in most \CONTEXT\
constructs that use alignment, we're not in math mode at all when we start
with the alignment. Therefore the \type {&} will be just an ampersand in most
\CONTEXT\ cases.

So to summarize: don't expect \type {\char"26} to work out well in math mode
because all kind of magic kicks in. These are the more obscure features and side
effects of \TEX\ dealing with input and it's really hard to predict how \TEX\
will see the ampersand you entered. You need to know the internals and even then
it's non trivial. Take

\starttyping
\startformula
\startalign
    \NC x \NR
    \NC x \NR
\stopalign
\stopformula
\stoptyping

versus:

\starttyping
\startformula
\startalign
    & x \NR
    & x \NR
\stopalign
\stopformula
\stoptyping

versus:

\starttyping
\startformula
\startalign
    \NC x & y \NR
    \NC x & y \NR
\stopalign
\stopformula
\stoptyping

The first case works as expected, the second one treats the \type {&} as text and
the third one, as we enter math mode with \type {\NC}, depends on circumstances.
If you use just \CONTEXT\ math coding, you can say:

\starttyping
\setupmathematics
  [ampersand=normal]
\stoptyping

And always render an ampersand (although a math one in math mode).

\stopsection

\stopchapter

\stopcomponent