summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/math/math-grouping.tex
blob: cc79ff198432e01cba1374639c7992eed2830a21 (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
203
204
205
206
207
208
209
210
211
212
% language=uk

\environment math-layout

\startbuffer[oldcolor]
    \disableexperiments[simplegroups]%
    % when we no longer have it as experiment
    \unprotect \pushoverloadmode
    \permanent\protected\def\color[#1]%
      {\bgroup % \beginsimplegroup
       \edef\currentcolorname{#1}%
       \ifempty\currentcolorprefix
         \colo_helpers_activate_nop
       \else
         \colo_helpers_activate_yes
       \fi
       \let\nexttoken}%
    \popoverloadmode \protect
\stopbuffer

\startbuffer[newcolor]
    \enableexperiments[simplegroups]%
\stopbuffer

\startcomponent math-grouping

\startchapter[title=Grouping]

\startsection[title=Some details]

In \TEX\ there are all kind of groups. When you start with a curly brace, you
often enter a group but when you start a box or table cell you also do that. When
you enter math a math group is started. Assignments are, unless explicitly done
global, nearly always local to the group. Here we discuss the following two
cases:

\starttyping
{ .... }
\bgroup .... \egroup
\begingroup .... \endgroup
\stoptyping

We say two cases, not three, because the first two are equivalent: the two macros
in the second line are not primitives but aliases to the curly braces. There is
however one fundamental difference between them. The verbose \type {\begingroup}
starts a so called simple group so let's call the other complex. A complex group
is bounded by equivalents to the two characters (braces) that have catcodes that
begin and end these groups. So, the following is valid.

\starttyping
{ .... }
{ .... \egroup
\bgroup .... }
\bgroup .... \egroup
\stoptyping

This means that a macro like this is okay:

\starttyping
\def\foo{\bgroup\bf\let\next}  \foo{text}
\stoptyping

The \type {\foo} will start a complex group, switch font and then pick up the
brace. The group will be closed by the matching right brace or an equivalent.
The following two cases are invalid:

\starttyping
\bgroup .... \endgroup
\begingroup .... \egroup
\stoptyping

which means that:

\starttyping
\def\foo{\begingroup\bf\let\next}  \foo{text}
\stoptyping

will trigger an error message. This is rather unfortunate because using braces
to wrap an argument in curly braces is rather convenient. The way out is this:

\starttyping
\def\foo#1{\begingroup\bf#1\endgroup}  \foo{text}
\stoptyping

which is perfectly valid apart from the fact that the argument is first picked up
and then fed back into the input. Apart from a small performance hit and using
more memory it also adds noise to tracing.

\stopsection

\startsection[title=Side effects]

In math mode matters are complicates by the fact that complex groups (the ones
started with the curly brace) start a math list. And that has side effects because
the spacing between math elements depends on what we deal with: math symbol, lists,
fenced material. The following example shows a whole lot of this:

\startbuffer[example]
\starttabulate[|j1|j1||]
    \NC $\sin{\left(xxx\right)}$
    \NC $f   {\left(xxx\right)}$
    \NC $x   {\left(xxx\right)}$
    \NR
    \NC $\sin\begingroup\left(xxx\right)\endgroup$
    \NC $f   \begingroup\left(xxx\right)\endgroup$
    \NC $x   \begingroup\left(xxx\right)\endgroup$
    \NR
    \NC $\sin\left(xxx\right)$\par
    \NC $f   \left(xxx\right)$\par
    \NC $x   \left(xxx\right)$\par
    \NR
    \NC $\sin\color[darkgreen]{(xxx)}$\par
    \NC $f   \color[darkgreen]{(xxx)}$\par
    \NC $x   \color[darkgreen]{(xxx)}$\par
    \NR
    \NC $\sin\startcolor[darkblue](xxx)\stopcolor$\par
    \NC $f   \startcolor[darkblue](xxx)\stopcolor$\par
    \NC $x   \startcolor[darkblue](xxx)\stopcolor$\par
    \NR
    \NC $\sin(xxx)$\par
    \NC $f   (xxx)$\par
    \NC $x   (xxx)$\par
    \NR
    \NC $\sin{\color[darkyellow]{(xxx)}}$\par
    \NC $f   {\color[darkyellow]{(xxx)}}$\par
    \NC $x   {\color[darkyellow]{(xxx)}}$\par
    \NR
    \NC $\sin\begingroup\color[darkred]{(xxx)}\endgroup$\par
    \NC $f   \begingroup\color[darkred]{(xxx)}\endgroup$\par
    \NC $x   \begingroup\color[darkred]{(xxx)}\endgroup$\par
    \NR
\stoptabulate
\stopbuffer

\typebuffer[example]

A valid question is why we would want to add curly braces. One reason is that we
we want to apply something to a few characters, in this case color the argument
to the sinus. Now, when a color command is defined as the \type {\foo} before, we
start a complex group and that influences spacing! This is demonstrated in the
left part of \in {figure} [fig:grouping:sin] (you can zoom in on the table to
see the details; we also report the kind of spacing applied).

\startplacefigure[reference=fig:grouping:sin,title=Grouping influencing math spacing (list).]
    \startcombination[nx=2,ny=1,distance=2em,style=bold]
        \startcontent
            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
            \getbuffer[oldcolor]%
            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
        \stopcontent
        \startcaption
            pseudo grouping
        \stopcaption
        \startcontent
            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
            \getbuffer[newcolor]%
            \scale[width=\dimexpr\measure{combination}-1em\relax]{\getbuffer[example]}%
        \stopcontent
        \startcaption
            grouping
        \stopcaption
    \stopcombination
\stopplacefigure

The right variant in that figure uses a different way of grouping, one that is
equivalent to:

\starttyping
\def\foo{\beginsimplegroup\bf\let\next}
\stoptyping

This time we effectively do \type {\begingroup} but permits both \type
{\endgroup} or a curly brace (or \type {\egroup} to wrap up the group. That means
that we don't get the side effect of starting a math list.

\startbuffer[example]
$ x = y \quad x \color[red]{=} y$
\stopbuffer

This example shows the effect of coloring a single character (the result is shown
in \in {figure} [fig:grouping:char]):

\typebuffer[example]

\startplacefigure[reference=fig:grouping:char,title=Grouping influencing math spacing (symbol).]
    \vkern4ex
    \startcombination[nx=2,ny=1,distance=4em,style=bold]
        \startcontent
            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
            \getbuffer[oldcolor]%
            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
        \stopcontent
        \startcaption
            pseudo grouping
        \stopcaption
        \startcontent
            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
            \getbuffer[newcolor]%
            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
        \stopcontent
        \startcaption
            grouping
        \stopcaption
    \stopcombination
    \vkern2ex
\stopplacefigure

\stopsection

\stopchapter

\stopcomponent