Debian Codegraph @

texmf-dist/tex/optex/demo/op-mathalign.tex

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 % Math alignment examples inspired by https://www.ntg.nl/maps/34/06.pdf
4
5 \fontfam[newcm]
6 \margins/1 a4 (2,2,2,2)cm
7 \hyperlinks\Blue\Blue
8
9 \refdecl{
10 \def\Xpos#1#2#3{\sxdef{pos:#1}{{#2}{#3}\_currpage}}
11 }
12 \def\setpos[#1]{\openref\pdfsavepos
13 \_ewref\Xpos{{#1}\unexpanded{{\the\pdflastxpos}{\the\pdflastypos}}}}
14
15 \def\posx [#1]{\_ea \posi \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}sp}
16 \def\posy [#1]{\_ea \posii \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}sp}
17 \def\pospg[#1]{\_ea \posiii \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}}
18
19 \def\posi #1#2#3#4{#1}
20 \def\posii #1#2#3#4{#2}
21 \def\posiii #1#2#3#4{#3}
22
23 \newcount\tomarginno
24 \def\toright#1{\_incr\tomarginno {\setpos[tr:\the\tomarginno]%
25 \rlap{\kern-\posx[tr:\the\tomarginno]\kern\hoffset\kern\hsize\llap{#1}}}}
26 \def\toleft#1{\_incr\tomarginno {\setpos[tr:\the\tomarginno]%
27 \rlap{\kern-\posx[tr:\the\tomarginno]\kern\hoffset\rlap{#1}}}}
28
29 \def\eqm{\toright\eqmark}
30
31 \def\\{\begingroup
32 \_setverb \obeylines \scanlatex
33 }
34 \def\scanlatex#1//{\tt #1\endgroup \scanoptex}
35 \long\def\scanoptex#1$${\begingroup
36 \_setverb \obeylines \scanoptexA
37 }
38 \ea\def\ea\scanoptexA\ea#\ea1\detokenize{$$}{\bigskip \tt
39 \detokenize{$$}#1\detokenize{$$}\endgroup
40 $$\catcode`\^^M=9 \scantextokens{#1}$$
41 \bigskip
42 }
43
44 \tit Math alignment examples
45
46 The document \url{https://www.ntg.nl/maps/34/06.pdf} shows examples how to
47 do special math alignments in display mode in \ConTeXt/ (and in \LaTeX/ for
48 comparison). We present the same examples here. They are created in
49 \OpTeX/ and the \LaTeX/ source is shown for comparison.
50
51 Note that several examples here use the macro \code{\\eqm} for placing
52 an equation mark. The macro is defined~by
53
54 \begtt
55 \def\eqm{\toright\eqmark}
56 \endtt
57 %
58 and the \code{\\toright} macro is defined in
59 \ulink[http://petr.olsak.net/optex/optex-tricks.html#torighteq]{\OpTeX/ trick 0028}
60 which is based on
61 \ulink[http://petr.olsak.net/optex/optex-tricks.html#setpos]{\OpTeX/ trick 0020}.
62 I.e. the following macros are used here:
63
64 \begtt
65 \refdecl{
66 \def\Xpos#1#2#3{\sxdef{pos:#1}{{#2}{#3}\_currpage}}
67 }
68 \def\setpos[#1]{\openref\pdfsavepos
69 \_ewref\Xpos{{#1}\unexpanded{{\the\pdflastxpos}{\the\pdflastypos}}}}
70
71 \def\posx [#1]{\_ea \posi \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}sp}
72 \def\posy [#1]{\_ea \posii \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}sp}
73 \def\pospg[#1]{\_ea \posiii \romannumeral-`\.\trycs{pos:#1}{{0}{0}{0}{0}}}
74
75 \def\posi #1#2#3#4{#1}
76 \def\posii #1#2#3#4{#2}
77 \def\posiii #1#2#3#4{#3}
78
79 \newcount\tomarginno
80 \def\toright#1{\_incr\tomarginno {\setpos[tr:\the\tomarginno]%
81 \rlap{\kern-\posx[tr:\the\tomarginno]\kern\hoffset\kern\hsize\llap{#1}}}}
82 \def\toleft#1{\_incr\tomarginno {\setpos[tr:\the\tomarginno]%
83 \rlap{\kern-\posx[tr:\the\tomarginno]\kern\hoffset\rlap{#1}}}}
84 \endtt
85 %
86 and we have to run \TeX/ twice.
87
88 \notoc\nonum\sec Contents
89
90 \centerline{\vbox{\hsize=.5\hsize
91 \maketoc
92 }}
93
94 \vfil\break
95
96 \let\_firstnoindent=\relax
97 \mathsboff \catcode`\_=12 \everytable{\catcode`\_=11}
98
99 \sec Gather
100
101 \\
102 \begin{gather}
103 v = u + at, \\
104 d = ut + \frac12 at^2.
105 \end{gather}
106 //
107
108 $$
109 \displaylines{
110 v = u + at, \eqm \cr
111 d = ut + {1\over2} at^2. \eqm
112 }
113 $$
114
115 \sec Left gather
116
117 \\
118 \begin{align}
119 & v = u + at, \\
120 & d = ut + \frac12 at^2.
121 \end{align}
122 //
123
124 $$
125 \eqalignno{
126 & v = u + at, & \eqmark \cr
127 & d = ut + {1\over2} at^2. & \eqmark
128 }
129 $$
130
131 \sec Right gather
132
133 \\
134 \begin{align}
135 v = u + at , & \\
136 d = ut + \frac12 atˆ2. &
137 \end{align}
138 //
139
140 $$
141 \eqalignno{
142 v = u + at, && \eqmark \cr
143 d = ut + {1\over2} at^2. && \eqmark
144 }
145 $$
146
147 \sec Align
148
149 \\
150 \begin{align}
151 v &= u + at, \\
152 d &= ut + \frac12 at^2.
153 \end{align}
154 //
155
156 $$
157 \eqalignno{
158 v &= u + at, & \eqmark \cr
159 d &= ut + {1\over2} at^2. & \eqmark
160 }
161 $$
162
163 \sec Split
164
165 \\
166 \begin{equation} \begin{split}
167 (x+1)^8 ={} & x^8 + 8 x^7 + 28 x^6 + 56 x^5 + 70 x^4 \\
168 & + 56 x^3 + 28 x^2 + 8 x + 1.
169 \end{split} \end{equation}
170 //
171
172 $$
173 \eqalign{
174 (x+1)^8 = {}& x^8 + 8 x^7 + 28 x^6 + 56 x^5 + 70 x^4 \cr
175 & + 56 x^3 + 28 x^2 + 8 x + 1.
176 } \eqmark
177 $$
178
179 \sec Alignat
180
181 \\
182 \begin{alignat}{2}
183 \nabla\cdot \mathbf E &= \frac{\rho}{\varepsilon_0}, \qquad
184 & \nabla\times \mathbf E &= -\frac{\partial \mathbf B}{\partial t},\\
185 \nabla\cdot \mathbf B &= 0,
186 & \nabla\times \mathbf B &= \mu_0{\mathbf j}+\varepsilon_0\mu_0
187 \frac{\partial \mathbf E}{\partial t}.
188 \end{alignat}
189 //
190
191 $$
192 \eqalign{
193 \nabla\cdot {\bf E} &= {\rho\over\varepsilon_0}, \qquad
194 && \nabla\times {\bf E} &= -{\partial {\bf B}\over\partial t}, \eqm\cr
195 \nabla\cdot {\bf B} &= 0,
196 && \nabla\times {\bf B} &= \mu_0{\bf j}+\varepsilon_0\mu_0
197 {\partial {\bf E}\over \partial t}. \eqm
198 }
199 $$
200
201 \sec Flalign
202
203 \\
204 \begin{flalign*}
205 \nabla\cdot \mathbf E &= \frac{\rho}{\varepsilon_0},
206 & \nabla\times \mathbf E &= -\frac{\partial \mathbf B}{\partial t}.\\
207 \nabla\cdot \mathbf B &= 0,
208 & \nabla\times \mathbf B &= \mu_0{\mathbf j}+\varepsilon_0\mu_0
209 \frac{\partial \mathbf E}{\partial t}.
210 \end{flalign*}
211 //
212
213 $$
214 \eqspace=10em
215 \eqalign{
216 \nabla\cdot {\bf E} &= {\rho\over\varepsilon_0}, \qquad
217 && \nabla\times {\bf E} &= -{\partial {\bf B}\over\partial t}, \cr
218 \nabla\cdot {\bf B} &= 0,
219 && \nabla\times {\bf B} &= \mu_0{\bf j}+\varepsilon_0\mu_0
220 {\partial {\bf E}\over \partial t}.
221 }
222 $$
223
224 \sec Intertext
225
226 \\
227 \begin{align*}
228 \cos 2\theta &= \cos^2 \theta + \sin^2 \theta \\
229 \intertext{replace $\sin^2 \theta$ by $1 - \cos^2 \theta$}
230 &= 2\cos^2 \theta - 1
231 \end{align*}
232 //
233
234 $$
235 \eqalignno{
236 \cos 2\theta &= \cos^2 \theta + \sin^2 \theta \cr
237 \noalign{\hbox{replace $\sin^2 \theta$ by $1 - \cos^2 \theta$}}
238 &= 2\cos^2 \theta - 1
239 }
240 $$
241
242 \sec Linear equations
243
244 \\
245 \begin{alignat}{5}
246 x_1 & {} + {}& x_2 &{} + {}& 6x_3 &{} = {}& 170, \\
247 3x_1 & {} - {}& 110x_2 &{} - {}& x_3 &{} = {}& 4, \\
248 14x_1 & {} + {}& 13x_2 &{} + {}& 10x_3 &{} = {}& 25.
249 \end{alignat}
250 //
251
252 $$
253 \thistable{\tablinespace=0pt \tabiteml={${}}\tabitemr={{}$}
254 \tabstrut={\lower1.5ex\vbox to3.5ex{}}}
255 \table{3{rc}r}{
256 x_1 &+& x_2 &+& 6x_3 &=& 170, \eqm \cr
257 3x_1 &-& 110x_2 &-& x_3 &=& 4, \eqm \cr
258 14x_1 &+& 13x_2 &+& 10x_3 &=& 25. \eqm
259 }
260 $$
261
262 \sec Matrix and Arrays
263
264 \\
265 \begin{equation*}
266 \setlength{\arraycolsep}{1em}
267 \begin{array}{ccc}
268 A & B & C \\
269 AA & BB & CC \\
270 AAA & BBB & CCC
271 \end{array}
272 \end{equation*}
273 //
274
275 $$
276 \matrix{
277 A & B & C \cr
278 AA & BB & CC \cr
279 AAA & BBB & CCC
280 }
281 $$
282
283 \\
284 \begin{equation*}
285 \setlength{\arraycolsep}{1em}
286 \begin{array}{lcr}
287 A & B & C \\
288 AA & BB & CC \\
289 AAA & BBB & CCC
290 \end{array}
291 \end{equation*}
292 //
293
294 $$
295 \thistable{\tabstrut{}\tabiteml={\kern.5em${}}\tabitemr={{}$\kern.5em}}
296 \table{lcr}{
297 A & B & C \cr
298 AA & BB & CC \cr
299 AAA & BBB & CCC
300 }
301 $$
302
303 \sec Pmatrix
304
305 \\
306 \begin{equation*}
307 A = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}
308 \end{equation*}
309 //
310
311 $$
312 \pmatrix {1\cr 2\cr 3}
313 $$
314
315 \sec Delarray package
316
317 \\
318 \begin{equation*}
319 \begin{array}[b]({c}) 1 \\ 2 \\ 3 \end{array}
320 \begin{array}[c]({c}) 1 \\ 2 \\ 3 \end{array}
321 \begin{array}[t]({c}) 1 \\ 2 \\ 3 \end{array}
322 \end{equation*}
323 //
324
325 $$
326 \def\mybox#1{\hbox{$\displaystyle{#1}$}}
327 \raise3ex\mybox{\pmatrix {1\cr 2\cr 3}}
328 \pmatrix {1\cr 2\cr 3}
329 \lower3ex\mybox{\pmatrix {1\cr 2\cr 3}}
330 $$
331
332 \sec Cases
333
334 \\
335 \begin{equation*}
336 |x| =
337 \begin{cases}
338 x, & \text{if $x \ge 0$;} \\
339 -x, & \text{otherwise.}
340 \end{cases}
341 \end{equation*}
342 //
343
344 $$
345 |x| = \cases { x & if $x \ge 0$; \cr
346 -x & otherwise }
347 $$
348
349 \\
350 \begin{equation*}
351 f(x) =
352 \begin{dcases}
353 \int_0ˆx g(y)\,dy, & \text{if $x \ge 0$;} \\
354 \int_{-x}ˆ0 g(y)\,dy, & \text{otherwise.}
355 \end{dcases}
356 \end{equation*}
357 //
358
359 $$
360 \let\ds=\displaystyle
361 f(x) = \cases { \ds \int_0^x g(y)\,dy, & if $x \ge 0$; \cr \noalign{\medskip}
362 \ds \int_{-x}^0 g(y)\,dy, & otherwise. }
363 $$
364
365
366
367 \end