math-oddities.tex /size: 4284 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/math
2
3\environment math-layout
4
5\startcomponent math-oddities
6
7\startchapter[title=Things you might forget]
8
9\startsection[title=Ampersands]
10
11You can skip this, but if you continue reading, here is some low level plain code
12(don't use this in \CONTEXT):
13
14\starttyping
15\def\matrix#1%
16  {\null
17   \,
18   \vcenter
19     {\normalbaselines
20      \ialign{\hfil$##$\hfil && \quad\hfil$##$\hfil\crcr
21      \mathstrut\crcr
22      \noalign{\kern-\baselineskip}
23      #1\crcr
24      \mathstrut\crcr
25      \noalign{\kern-\baselineskip}}}%
26   \,}
27\stoptyping
28
29You see the \type {&} here and it's the alignment cell separator. The special
30meaning of these characters is determined by the so called catcode. Here we have:
31
32\starttyping
33\catcode"26=4
34\stoptyping
35
36Character \type {0x26} is the ampersand. In \CONTEXT\ this character can be used
37in text mode because we never use it as alignment character, which is something
38typical \TEX. The same is true for \type {^} and \type {_}. So, effectively we
39have (for instance):
40
41\starttyping
42\catcode"26=12
43\stoptyping
44
45In order to still get this \type {&} supported as alignment character in math
46mode, we have to jump through some hoops. Think of this (again, don't do this in
47\CONTEXT):
48
49\starttyping
50\bgroup
51    \global\mathcode"26="8000
52
53    \catcode"26=4
54
55    \xdef\normalmathaligntab{&}
56
57    \catcode"26=13
58
59    \global\everymath{\def&{\normalmathaligntab}}
60\egroup
61\stoptyping
62
63Before we go on you should realize that we never use the \type {&} in \CONTEXT\
64as separator. The sole reason for dealing with this issue is that users can have
65their own code that uses the ampersand that way. In \CONTEXT\ we do things like:
66
67\starttyping
68\startformula
69    \startmatrix
70        \NC 1 \NC 2 \NR
71        \NC 3 \NC 4 \NR
72    \stopmatrix
73\stopformula
74\stoptyping
75
76Where \type {\NC} can be more powerful than a \type {&}. Anyhow, the reason for
77discussing this here is that there can be surprises. In a running text you can do
78this:
79
80\starttyping
81A & B
82\stoptyping
83
84Which processes okay and gives the ampersand as glyph. The following is also okay:
85
86\starttyping
87$A \Umathchar"2"0"26 B$
88\stoptyping
89
90However, the next one:
91
92\starttyping
93$A \char"26 B$
94\stoptyping
95
96fails with a \type {Misplaced alignment tab character &}. The reason is that
97where in text mode \TEX's parser will turn the \type {\char} into a character
98node and carry on afterwards, in math mode it will treat this inpout as were it a
99directly input character, so the above is like, where the \type {&} has active
100properties and becomes the sparator ampersand which then triggers the error:
101
102\starttyping
103$A & B$
104\stoptyping
105
106This means that we cannot have a definition like:
107
108\starttyping
109\def\AND{\char"26\relax}
110\stoptyping
111
112that can be used in math mode, which is why the \CWEB\ macros do:
113
114\starttyping
115\def\AND{\def\AND{\mathchar"2026\relax}\AND}
116\stoptyping
117
118Back to the plain example. The \type {\matrix} command has to be wrapped in
119math mode and therefore the \type {&} will adapt, while in most \CONTEXT\
120constructs that use alignment, we're not in math mode at all when we start
121with the alignment. Therefore the \type {&} will be just an ampersand in most
122\CONTEXT\ cases.
123
124So to summarize: don't expect \type {\char"26} to work out well in math mode
125because all kind of magic kicks in. These are the more obscure features and side
126effects of \TEX\ dealing with input and it's really hard to predict how \TEX\
127will see the ampersand you entered. You need to know the internals and even then
128it's non trivial. Take
129
130\starttyping
131\startformula
132\startalign
133    \NC x \NR
134    \NC x \NR
135\stopalign
136\stopformula
137\stoptyping
138
139versus:
140
141\starttyping
142\startformula
143\startalign
144    & x \NR
145    & x \NR
146\stopalign
147\stopformula
148\stoptyping
149
150versus:
151
152\starttyping
153\startformula
154\startalign
155    \NC x & y \NR
156    \NC x & y \NR
157\stopalign
158\stopformula
159\stoptyping
160
161The first case works as expected, the second one treats the \type {&} as text and
162the third one, as we enter math mode with \type {\NC}, depends on circumstances.
163If you use just \CONTEXT\ math coding, you can say:
164
165\starttyping
166\setupmathematics
167  [ampersand=normal]
168\stoptyping
169
170And always render an ampersand (although a math one in math mode).
171
172\stopsection
173
174\stopchapter
175
176\stopcomponent
177