math-grouping.tex /size: 6887 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/math
2
3\environment math-layout
4
5\startbuffer[oldcolor]
6    \disableexperiments[simplegroups]%
7    % when we no longer have it as experiment
8    \unprotect \pushoverloadmode
9    \permanent\protected\def\color[#1]%
10      {\bgroup % \beginsimplegroup
11       \edef\currentcolorname{#1}%
12       \ifempty\currentcolorprefix
13         \colo_helpers_activate_nop
14       \else
15         \colo_helpers_activate_yes
16       \fi
17       \let\nexttoken}%
18    \popoverloadmode \protect
19\stopbuffer
20
21\startbuffer[newcolor]
22    \enableexperiments[simplegroups]%
23\stopbuffer
24
25\startcomponent math-grouping
26
27\startchapter[title=Grouping]
28
29\startsection[title=Some details]
30
31In \TEX\ there are all kind of groups. When you start with a curly brace, you
32often enter a group but when you start a box or table cell you also do that. When
33you enter math a math group is started. Assignments are, unless explicitly done
34global, nearly always local to the group. Here we discuss the following two
35cases:
36
37\starttyping
38{ .... }
39\bgroup .... \egroup
40\begingroup .... \endgroup
41\stoptyping
42
43We say two cases, not three, because the first two are equivalent: the two macros
44in the second line are not primitives but aliases to the curly braces. There is
45however one fundamental difference between them. The verbose \type {\begingroup}
46starts a so called simple group so let's call the other complex. A complex group
47is bounded by equivalents to the two characters (braces) that have catcodes that
48begin and end these groups. So, the following is valid.
49
50\starttyping
51{ .... }
52{ .... \egroup
53\bgroup .... }
54\bgroup .... \egroup
55\stoptyping
56
57This means that a macro like this is okay:
58
59\starttyping
60\def\foo{\bgroup\bf\let\next}  \foo{text}
61\stoptyping
62
63The \type {\foo} will start a complex group, switch font and then pick up the
64brace. The group will be closed by the matching right brace or an equivalent.
65The following two cases are invalid:
66
67\starttyping
68\bgroup .... \endgroup
69\begingroup .... \egroup
70\stoptyping
71
72which means that:
73
74\starttyping
75\def\foo{\begingroup\bf\let\next}  \foo{text}
76\stoptyping
77
78will trigger an error message. This is rather unfortunate because using braces
79to wrap an argument in curly braces is rather convenient. The way out is this:
80
81\starttyping
82\def\foo#1{\begingroup\bf#1\endgroup}  \foo{text}
83\stoptyping
84
85which is perfectly valid apart from the fact that the argument is first picked up
86and then fed back into the input. Apart from a small performance hit and using
87more memory it also adds noise to tracing.
88
89\stopsection
90
91\startsection[title=Side effects]
92
93In math mode matters are complicates by the fact that complex groups (the ones
94started with the curly brace) start a math list. And that has side effects because
95the spacing between math elements depends on what we deal with: math symbol, lists,
96fenced material. The following example shows a whole lot of this:
97
98\startbuffer[example]
99\starttabulate[|j1|j1||]
100    \NC $\sin{\left(xxx\right)}$
101    \NC $f   {\left(xxx\right)}$
102    \NC $x   {\left(xxx\right)}$
103    \NR
104    \NC $\sin\begingroup\left(xxx\right)\endgroup$
105    \NC $f   \begingroup\left(xxx\right)\endgroup$
106    \NC $x   \begingroup\left(xxx\right)\endgroup$
107    \NR
108    \NC $\sin\left(xxx\right)$\par
109    \NC $f   \left(xxx\right)$\par
110    \NC $x   \left(xxx\right)$\par
111    \NR
112    \NC $\sin\color[darkgreen]{(xxx)}$\par
113    \NC $f   \color[darkgreen]{(xxx)}$\par
114    \NC $x   \color[darkgreen]{(xxx)}$\par
115    \NR
116    \NC $\sin\startcolor[darkblue](xxx)\stopcolor$\par
117    \NC $f   \startcolor[darkblue](xxx)\stopcolor$\par
118    \NC $x   \startcolor[darkblue](xxx)\stopcolor$\par
119    \NR
120    \NC $\sin(xxx)$\par
121    \NC $f   (xxx)$\par
122    \NC $x   (xxx)$\par
123    \NR
124    \NC $\sin{\color[darkyellow]{(xxx)}}$\par
125    \NC $f   {\color[darkyellow]{(xxx)}}$\par
126    \NC $x   {\color[darkyellow]{(xxx)}}$\par
127    \NR
128    \NC $\sin\begingroup\color[darkred]{(xxx)}\endgroup$\par
129    \NC $f   \begingroup\color[darkred]{(xxx)}\endgroup$\par
130    \NC $x   \begingroup\color[darkred]{(xxx)}\endgroup$\par
131    \NR
132\stoptabulate
133\stopbuffer
134
135\typebuffer[example]
136
137A valid question is why we would want to add curly braces. One reason is that we
138we want to apply something to a few characters, in this case color the argument
139to the sinus. Now, when a color command is defined as the \type {\foo} before, we
140start a complex group and that influences spacing! This is demonstrated in the
141left part of \in {figure} [fig:grouping:sin] (you can zoom in on the table to
142see the details; we also report the kind of spacing applied).
143
144\startplacefigure[reference=fig:grouping:sin,title=Grouping influencing math spacing (list).]
145    \startcombination[nx=2,ny=1,distance=2em,style=bold]
146        \startcontent
147            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
148            \getbuffer[oldcolor]%
149            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
150        \stopcontent
151        \startcaption
152            pseudo grouping
153        \stopcaption
154        \startcontent
155            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
156            \getbuffer[newcolor]%
157            \scale[width=\dimexpr\measure{combination}-1em\relax]{\getbuffer[example]}%
158        \stopcontent
159        \startcaption
160            grouping
161        \stopcaption
162    \stopcombination
163\stopplacefigure
164
165The right variant in that figure uses a different way of grouping, one that is
166equivalent to:
167
168\starttyping
169\def\foo{\beginsimplegroup\bf\let\next}
170\stoptyping
171
172This time we effectively do \type {\begingroup} but permits both \type
173{\endgroup} or a curly brace (or \type {\egroup} to wrap up the group. That means
174that we don't get the side effect of starting a math list.
175
176\startbuffer[example]
177$ x = y \quad x \color[red]{=} y$
178\stopbuffer
179
180This example shows the effect of coloring a single character (the result is shown
181in \in {figure} [fig:grouping:char]):
182
183\typebuffer[example]
184
185\startplacefigure[reference=fig:grouping:char,title=Grouping influencing math spacing (symbol).]
186    \vkern4ex
187    \startcombination[nx=2,ny=1,distance=4em,style=bold]
188        \startcontent
189            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
190            \getbuffer[oldcolor]%
191            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
192        \stopcontent
193        \startcaption
194            pseudo grouping
195        \stopcaption
196        \startcontent
197            \showmakeup[math,fontkern,italic]% laps into the margin hence the -2em
198            \getbuffer[newcolor]%
199            \scale[width=\dimexpr\measure{combination}-2em\relax]{\getbuffer[example]}%
200        \stopcontent
201        \startcaption
202            grouping
203        \stopcaption
204    \stopcombination
205    \vkern2ex
206\stopplacefigure
207
208\stopsection
209
210\stopchapter
211
212\stopcomponent
213