-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.do.txt
713 lines (601 loc) · 14.2 KB
/
math_test.do.txt
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
TITLE: How various formats can deal with LaTeX math
AUTHOR: Hans Petter Langtangen at Simula Research Laboratory & University of Oslo
DATE: today
__Summary.__
The purpose of this document is to test LaTeX math in DocOnce with
various output formats. Most LaTeX math constructions are renedered
correctly by MathJax in plain HTML, but some combinations of
constructions may fail. Unfortunately, only a subset of what works in
html MathJax also works in sphinx MathJax. The same is true for
markdown MathJax expresions (e.g. Jupyter notebooks). Tests and
examples are provided to illustrate what may go wrong.
The recommendation for writing math that translates to MathJax in
html, sphinx, and markdown is to stick to the environments `\[
... \]`, `equation`, `equation*`, `align`, `align*`, `alignat`, and
`alignat*` only. Test the math with sphinx output; if it works in that
format, it should work elsewhere too.
The current version of the document is translated from DocOnce source
to the format _${FORMAT}_.
======= Test of equation environments =======
===== Test 1: Inline math =====
We can get an inline equation
`$u(t)=e^{-at}$` rendered as $u(t)=e^{-at}$.
===== Test 2: A single equation with label =====
An equation with number,
!bc latexcod
|bt
\begin{equation} u(t)=e^{-at} label{eq1a}\end{equation}
|et
!ec
looks like
!bt
\begin{equation} u(t)=e^{-at} label{_eq1a}\end{equation}
!et
Maybe this multi-line version is what we actually prefer to write:
!bc latexcod
|bt
\begin{equation}
u(t)=e^{-at}
label{eq1b}
\end{equation}
|et
!ec
The result is the same:
!bt
\begin{equation}
u(t)=e^{-at} label{_eq1b}
\end{equation}
!et
We can refer to this equation through its label `eq1b`: (ref{_eq1b}).
===== Test 3: Multiple, aligned equations without label and number =====
MathJax has historically had some problems with rendering many LaTeX
math environments, but the `align*` and `align` environments have
always worked.
!bc latexcod
|bt
\begin{align*}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align*}
|et
!ec
Result:
!bt
\begin{align*}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align*}
!et
===== Test 4: Multiple, aligned equations with label =====
Here, we use `align` with user-prescribed labels:
!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
label{eq2b}\\
v(t) - 1 &= \frac{du}{dt}
label{eq3b}
\end{align}
|et
!ec
Result:
!bt
\begin{align}
u(t)&=e^{-at}
label{_eq2b}\\
v(t) - 1 &= \frac{du}{dt}
label{_eq3b}
\end{align}
!et
We can refer to the last equations as the system (ref{_eq2b})-(ref{_eq3b}).
# #if FORMAT == "sphinx"
!bwarning Note: align/alignat environments with labels are anti-aligned in sphinx!
Actually, *sphinx does not support the align environment with labels*,
such as we write above,
but DocOnce splits in this case the equations into separate, single equations
with labels. Hence the user can write one code with align and labels
and have it automatically
to work in latex, html, sphinx, notebooks, and other formats.
The generated sphinx code in the present case is
!bc rst
.. math::
:label: eq2b
u(t)=e^{-at}
.. math::
:label: eq3b
v(t) - 1 = \frac{du}{dt}
!ec
!ewarning
If DocOnce had not rewritten the equation it would be rendered in
${FORMAT} as nicely aligned equations without numbers (i.e., as if
we had used the `align*` environment):
## NOTE: These math blocks, not inside !bt-!et directives will result
## in error messages from DocOnce. But if we neglect them, the code works
## as intended.
.. math::
\begin{align}
u(t)&=e^{-at}
\label{__eq2b}\\
v(t) - 1 &= \frac{du}{dt}
\label{__eq3b}
\end{align}
# #endif
===== Test 5: Multiple, aligned equations without label =====
In LaTeX, equations within an `align` environment is automatically
given numbers. To ensure that an html document with MathJax gets the
same equation numbers as its latex/pdflatex companion, DocOnce
generates labels in equations where there is no label prescribed. For
example,
!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
\\
v(t) - 1 &= \frac{du}{dt}
\end{align}
|et
!ec
is edited to something like
!bc latexcod
|bt
\begin{align}
u(t)&=e^{-at}
label{_auto5}\\
v(t) - 1 &= \frac{du}{dt}
label{_auto6}
\end{align}
|et
!ec
and the output gets the two equation numbered.
# #if FORMAT == "sphinx"
Note that in sphinx the alignment is removed and separate `equation`
environments are used to get numbered equations in equation systems, cf. the
box above.
# #endif
!bt
\begin{align}
u(t)&=e^{-at}\\
v(t) - 1 &= \frac{du}{dt}
\end{align}
!et
===== Test 6: Multiple, aligned equations with multiple alignments =====
The `align` environment can be used with two `&` alignment characters, e.g.
!bc latexcod
|bt
\begin{align}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{align}
|et
!ec
The result in ${FORMAT} becomes
!bt
\begin{align}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{align}
!et
# #if FORMAT == "sphinx"
In ${FORMAT}, all alignments are removed, so this double use of `&`
results in ugly typesetting!
# #endif
A better solution is usually to use an `alignat` environment:
!bc latexcod
|bt
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
|et
!ec
with the rendered result
!bt
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
!et
# #if FORMAT in ("sphinx", "ipynb")
!bwarning align/alignat environments with equation numbers are anti-aligned!
In the `sphinx`, `ipynb`, and `pandoc` output formats, DocOnce rewrites
the equations in an `alignat` environment as individual equations in
`equation` environments (or more precisely, `sphinx` can work with
`alignat*` so only numbered `alignat` equations get rewritten as individual
equations). If the alignment is somewhat important, try the best with a
manual rewrite in terms of separate `equation` environments, and stick to
`align*` and `alignat*` in `sphinx`.
!ewarning
# #endif
# #if FORMAT == "sphinx"
With `alignat*` in ${FORMAT}, the equations above are typeset nicely as
!bt
\begin{alignat*}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat*}
!et
# #endif
# #if FORMAT in ("pandoc", "ipynb")
If DocOnce had not rewritten the above equations, they would be
rendered in ${FORMAT} as
$$
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
$$
# #elif FORMAT in ("sphinx")
Note that if DocOnce had not rewritten of the above equations, they would be
rendered similarly in ${FORMAT} as
.. math::
\begin{alignat}{2}
\frac{\partial u}{\partial t} &= \nabla^2 u, & x\in (0,L),
\ t\in (0,T]\\
u(0,t) &= u_0(x), & x\in [0,L]
\end{alignat}
That is, the equation numbers are gone.
# #endif
===== Test 7: Multiple, aligned eqnarray equations without label =====
Let us try the old `eqnarray*` environment.
!bc latexcod
|bt
\begin{eqnarray*}
u(t)&=& e^{-at}\\
v(t) - 1 &=& \frac{du}{dt}
\end{eqnarray*}
|et
!ec
which results in
!bt
\begin{eqnarray*}
u(t)&=& e^{-at}\\
v(t) - 1 &=& \frac{du}{dt}
\end{eqnarray*}
!et
===== Test 8: Multiple, eqnarrayed equations with label =====
Here we use `eqnarray` with labels:
!bc latexcod
|bt
\begin{eqnarray}
u(t)&=& e^{-at}
label{eq2c}\\
v(t) - 1 &=& \frac{du}{dt}
label{eq3c}
\end{eqnarray}
|et
!ec
which results in
!bt
\begin{eqnarray}
u(t)&=& e^{-at} label{_eq2c}\\
v(t) - 1 &=& \frac{du}{dt} label{_eq3c}
\end{eqnarray}
!et
Can we refer to the last equations as the system (ref{_eq2c})-(ref{_eq3c})
in the ${FORMAT} format?
# #if FORMAT == "sphinx"
No, unfortunately not. Sphinx cannot deal with equation numbers in
`eqnarray` environments and typeset them as if they were `eqnarray*`.
But MathJax supports `eqnarray` with labels.
The rule of thumb is to use `align` and not `eqnarray`!
# #endif
===== Test 9: The `multiline` environment with label and number =====
The LaTeX code
!bc latexcod
|bt
\begin{multline}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
label{multiline:eq1}
\end{multline}
|et
!ec
gets rendered as
!bt
\begin{multline}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
label{_multiline:eq1}
\end{multline}
!et
and we can hopefully refer to the Trapezoidal rule
as the formula (ref{_multiline:eq1}).
# #if FORMAT == "sphinx"
This equation will not render in sphinx unless we remove the `multiline`
environment, which means that it was typeset here without any multiline
information.
The best cross-format solution is to use `align` instead of `multiline`
with `\nonumber` in the first equation!
# #endif
===== Test 10: Splitting equations using a split environment =====
Although `align` can be used to split too long equations, a more obvious
command is `split`:
!bc latexcod
|bt
\begin{equation}
\begin{split}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
\end{split}
\end{equation}
|et
!ec
The result becomes
!bt
\begin{equation}
\begin{split}
\int_a^b f(x)dx = \sum_{j=0}^{n} \frac{1}{2} h(f(a+jh) +
f(a+(j+1)h)) \\
=\frac{h}{2}f(a) + \frac{h}{2}f(b) + \sum_{j=1}^n f(a+jh)
\end{split}
\end{equation}
!et
===== Test 11: Newcommands and boldface bm vs pmb =====
First we use the plain old pmb package for bold math. The formula
!bc latexcod
|bt
\[ \frac{\partial\u}{\partial t} +
\u\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
|et
!ec
and the inline expression `$\nabla\u (\x)\cdot\normalvec$`
(with suitable newcommands using pmb)
get rendered as
!bt
\[ \frac{\partial\u}{\partial t} +
\u\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
!et
and $\nabla\u (\x)\cdot\normalvec$.
# #if FORMAT == "sphinx"
DocOnce replaces newcommands by the actual latex code when requesting
the sphinx output format.
# #endif
Somewhat nicer fonts may appear with the more modern `\bm` command:
!bc latexcod
|bt
\[ \frac{\partial\ubm}{\partial t} +
\ubm\cdot\nabla\ubm = \nu\nabla^2\ubm -
\frac{1}{\varrho}\nabla p,\]
|et
!ec
(backslash `ubm` is a newcommand for bold math $u$), for which we get
!bt
\[ \frac{\partial\ubm}{\partial t} +
\ubm\cdot\nabla\ubm = \nu\nabla^2\ubm -
\frac{1}{\varrho}\nabla p.\]
!et
Moreover,
!bc
$\nabla\bm{u}(\bm{x})\cdot\bm{n}$
!ec
becomes $\nabla\bm{u}(\bm{x})\cdot\bm{n}$.
# #if FORMAT in ("html", "sphinx", "pandoc", "ipynb")
!bwarning
Note: for the ${FORMAT} format, `\bm` was substituted by DocOnce
to `\boldsymbol`.
!ewarning
# #endif
======= Problematic equations =======
Finally, we collect some problematic formulas in MathJax. They all work
fine in LaTeX. Most of them look fine in html too, but some fail in
sphinx, ipynb, or markdown.
===== Colored terms in equations =====
The LaTeX code
!bc latexcod
|bt
\[ {\color{blue}\frac{\partial\u}{\partial t}} +
\nabla\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
|et
!ec
results in
!bt
\[ {\color{blue}\frac{\partial\u}{\partial t}} +
\nabla\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
!et
# #if FORMAT in ('sphinx', 'ipynb')
but correct rendering in ${FORMAT} requires omitting the `\color` command:
!bt
\[ \frac{\partial\u}{\partial t} +
\nabla\cdot\nabla\u = \nu\nabla^2\u -
\frac{1}{\varrho}\nabla p,\]
!et
# #endif
===== Bar over symbols =====
Sometimes one must be extra careful with the LaTeX syntax to get sphinx MathJax
to render a formula correctly. Consider the combination of a bar over a
bold math symbol:
!bc latexcod
|bt
\[ \bar\f = f_c^{-1}\f,\]
|et
!ec
which for ${FORMAT} output results in
!bt
\[ \bar\f = f_c^{-1}\f.\]
!et
With sphinx, this formula is not rendered. However, using curly braces for the bar,
!bc latexcod
|bt
\[ \bar{\f} = f_c^{-1}\f,\]
|et
!ec
makes the output correct also for sphinx:
!bt
\[ \bar{\f} = f_c^{-1}\f,\]
!et
===== Matrix formulas =====
Here is an `align` environment with a label and the `pmatrix`
environment for matrices and vectors in LaTeX.
!bc latexcod
|bt
\begin{align}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots
label{mymatrixeq}\\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align}
|et
!ec
which becomes
!bt
\begin{align}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots
label{_mymatrixeq}\\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align}
!et
# #if FORMAT == 'ipynb'
The first equation do not render correctly in ipynb.
# #endif
The same matrices without labels in an `align*` environment:
!bc latexcod
|bt
\begin{align*}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots \\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align*}
|et
!ec
The rendered result becomes
!bt
\begin{align*}
\begin{pmatrix}
G_2 + G_3 & -G_3 & -G_2 & 0 \\
-G_3 & G_3 + G_4 & 0 & -G_4 \\
-G_2 & 0 & G_1 + G_2 & 0 \\
0 & -G_4 & 0 & G_4
\end{pmatrix}
&=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix}
+ \cdots \\
\begin{pmatrix}
C_5 + C_6 & -C_6 & 0 & 0 \\
-C_6 & C_6 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0
\end{pmatrix}
\frac{d}{dt} &=
\begin{pmatrix}
v_1 \\
v_2 \\
v_3 \\
v_4
\end{pmatrix} =
\begin{pmatrix}
0 \\
0 \\
0 \\
-i_0
\end{pmatrix}
\end{align*}
!et