-
Notifications
You must be signed in to change notification settings - Fork 3
/
mathml.pl
3313 lines (2521 loc) · 70.7 KB
/
mathml.pl
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
:- module(mathml, [mathml/4, mmlm//1, mmlm//2, colors/2]).
:- discontiguous mathml/0, math/2, math/3, math/4, current/3, paren/3, prec/3.
:- discontiguous type/3, denoting/3, ml/3, jax/3.
:- use_module(library(http/html_write)).
% Hook to defined own macros
%
% Example
% assert(math_hook(t0, subscript(t, 0))).
%
% From R, the hook is installed by
% mathml::hook(t0, subscript(t, 0))
%
:- dynamic math_hook/2.
:- multifile math_hook/2.
% Low-level functions (see, e.g. nthroot.pl)
%
% Example
% see nthroot.pl
%
:- multifile mlx/3. % translate term to mathml
:- multifile jaxx/3. % translate to LaTeX
:- multifile precx/3. % operator precedence
:- multifile parenx/3. % count parentheses
:- multifile typex/3. % some type information
% R interface: Translate R expression to MathML string
%
% Example
% r2mathml(sin(pi/2), M).
%
r2mathml(R, S)
=> r2mathml(R, S, []).
% The flags allow for context-dependent translation
%
% Examples
% see vignette of R package mathml
%
r2mathml(R, S, Flags)
=> mathml(R, M, Flags),
html(M, H, []),
maplist(atom_string, H, S).
% R interface: Translate R expression to MathJax string
r2mathjax(R, S)
=> r2mathjax(R, S, []).
r2mathjax(R, S, Flags)
=> mathjax(R, S, Flags).
% Legacy code from mcclass
mmlm(A) -->
mmlm([], A).
mmlm(Flags, A) -->
{ member(denote(false), Flags),
mathml(A, M, _With, Flags)
},
html(M).
mmlm(Flags, A) -->
{ colors(A, Colors),
append(Flags, Colors, Flags1),
mathml(A, M, With, Flags1)
},
html([M, With]).
colors(Expr, Flags) :-
bugs(Expr, Bugs),
findall(C, color(C), Colors),
findall(color(B, C), (nth0(N, Bugs, B), N10 is N mod 10, nth0(N10, Colors, C)), Flags0),
fmt(Flags0, Expr, Res),
sort(Res, Flags).
% Bugs
bugs(Expr, Bugs) :-
bugs_(Expr, List),
sort(List, Bugs).
bugs_(instead(Bug, Wrong, _Correct), List)
=> bugs_(Wrong, Bugs),
List = [Bug | Bugs].
bugs_(instead(Bug, Wrong, _Correct0, _Correct), List)
=> bugs_(Wrong, Bugs),
List = [Bug | Bugs].
bugs_(omit_left(Bug, Expr), List)
=> Expr =.. [_Op, _L, R],
bugs_(R, Bugs),
List = [Bug | Bugs].
bugs_(omit_right(Bug, Expr), List)
=> Expr =.. [_Op, L, _R],
bugs_(L, Bugs),
List = [Bug | Bugs].
bugs_(omit(Bug, Expr), List)
=> bugs_(Expr, Bugs),
List = [Bug | Bugs].
bugs_(add(Bug, Expr), List)
=> bugs_(Expr, Bugs),
List = [Bug | Bugs].
bugs_(drop_left(Bug, Expr), List)
=> Expr =.. [_Op, _L, R],
bugs_(R, Bugs),
List = [Bug | Bugs].
bugs_(drop_right(Bug, Expr), List)
=> Expr =.. [_Op, L, _R],
bugs_(L, Bugs),
List = [Bug | Bugs].
bugs_(add_left(Bug, Expr), List)
=> bugs_(Expr, Bugs),
List = [Bug | Bugs].
bugs_(add_right(Bug, Expr), List)
=> bugs_(Expr, Bugs),
List = [Bug | Bugs].
bugs_(color(Col, Expr), List),
atom(Col)
=> bugs_(Expr, Bugs),
List = [Col | Bugs].
bugs_(X, Nil),
atomic(X)
=> Nil = [].
bugs_(X, List),
compound(X)
=> X =.. [_ | Args],
maplist(bugs_, Args, Bugs),
append(Bugs, List).
color("dark").
color("$blue"). % #0d6efd
color("$indigo"). % #6610f2
color("$purple"). % #6f42c1
color("$pink"). % #d63384
color("$red"). % #dc3545
color("$orange"). % #fd7e14
color("$yellow"). % #ffc107
color("$green"). % #198754
color("$teal"). % #20c997
color("$cyan"). % #0dcaf0
ml(color(C, A), M, Flags),
atom(C)
=> member(color(C, S), Flags),
ml(color(S, A), M, Flags).
ml(color(C, A), M, Flags),
string(C)
=> ml(A, X, Flags),
M = mstyle(mathcolor(C), X).
fmt(Flags, Expr, F),
atomic(Expr)
=> F = Flags.
fmt(Flags, tstat(Expr), F)
=> fmt([format(tstat) | Flags], Expr, F).
fmt(Flags, hdrs(Expr), F)
=> fmt([format(hdrs) | Flags], Expr, F).
fmt(Flags, chi2ratio(Expr), F)
=> fmt([format(chi2ratio) | Flags], Expr, F).
fmt(Flags, fratio(Expr), F)
=> fmt([format(fratio) | Flags], Expr, F).
fmt(Flags, pval(Expr), F)
=> fmt([format(pval) | Flags], Expr, F).
fmt(Flags, Expr, F),
compound(Expr)
=> compound_name_arguments(Expr, _, Args),
maplist(fmt(Flags), Args, Res),
append(Res, F).
mathml(A, X, With, Flags) :-
ml(A, M, Flags),
!,
X = math(M),
denoting(A, Denoting, Flags),
ml(with(Denoting), With, Flags).
mathml(A, X, _, Flags) :-
ml("Conversion failed: ~w"-A, X, Flags).
%
% Formatting numbers
%
math(tstat(A), X, Flags, Flags1)
=> Flags1 = [digits(2) | Flags],
A = X.
math(hdrs(A), X, Flags, Flags1)
=> Flags1 = [digits(1) | Flags],
A = X.
math(chi2ratio(A), X, Flags, Flags1)
=> Flags1 = [digits(2) | Flags],
A = X.
math(perc(A), X, Flags, Flags1)
=> option(digits(D0), Flags, 2),
D is D0 - 2,
Flags1 = [digits(D), mult(100) | Flags],
X = list("", [A, '%']).
math(pval(A), X, Flags, Flags1)
=> Flags1 = [digits(3) | Flags],
A = X.
% Hyphen
math(hyph(L, R), X)
=> X = list(&('#8209'), [L, R]).
% End legacy code
% Translate R expression to HTML/MathJax term
% mathml(R, M, Flags)
% => ml(R, M0, Flags),
% denoting(R, Denoting, Flags),
% ml(with(Denoting), With, Flags),
% !, M = [math(M0), With].
%
% mathjax(R, M, Flags)
% => jax(R, M0, Flags),
% denoting(R, Denoting, Flags),
% jax(with(Denoting), With, Flags),
% !, format(string(M), "$~w$~w", [M0, With]).
% Check if a macro has been defined in an external module
macro(A, M, Flags, Flags1) :-
nb_current(topic, _),
b_getval(topic, Topic),
Topic:math_hook(A, M0),
!, Flags1 = Flags,
M = M0.
% Translates the R expression to another R expression M, checking for Flags
% and eventually changing Flags to Flags1
%
macro(R, M, Flags, Flags1) :-
math_hook(R, M0), % math hook from R
!, Flags1 = Flags,
M = M0.
macro(R, M, Flags, Flags1) :-
math(R, M, Flags, Flags1), % math/4 macro changing Flags
dif(Flags-R, Flags1-M).
macro(R, M, Flags, Flags) :-
math(R, M, Flags), % math/3 only reading Flags
dif(R, M).
macro(R, M, Flags, Flags) :-
math(R, M), % math/2 ignoring the flags
dif(R, M).
% Main MathML translation
%
% R: R expression
% M: HTML term
% Flags: to control some aspects of the output
%
% This predicate only checks if a macro can be applied. Add ml/3 predicates for
% R expressions with their translation below.
%
ml(R, M, Flags),
macro(R, R1, Flags, Flags1)
=> ml(R1, M, Flags1).
ml(R, M, Flags),
mlx(R, R1, Flags) % R hook into ml/3
=> M = R1.
% Same for MathJax/LaTeX
jax(R, M, Flags),
macro(R, R1, Flags, Flags1)
=> jax(R1, M, Flags1).
jax(R, M, Flags),
jaxx(R, R1, Flags) % R hook
=> M = R1.
% Return precedence of an R expression, to decide if parentheses are
% needed. Uses the usual Prolog precendence.
prec(R, Prec, Flags),
macro(R, R1, Flags, Flags1)
=> prec(R1, Prec, Flags1).
prec(R, Prec, Flags),
precx(R, Prec1, Flags)
=> Prec = Prec1.
% Return parentheses counter of an R expression. Needed to decide
% which shape is chosen (), [], {}, and restarting again with ().
paren(R, Paren, Flags),
macro(R, R1, Flags, Flags1)
=> paren(R1, Paren, Flags1).
paren(R, Paren, Flags),
parenx(R, Paren1, Flags)
=> Paren = Paren1.
% Return some extra type information as a list.
type(R, Type, Flags),
macro(R, R1, Flags, Flags1)
=> type(R1, Type, Flags1).
type(R, Type, Flags),
typex(R, Type1, Flags)
=> Type = Type1.
% Suppress the names of function arguments from R
%
% For instance, the R expression dbinom(x=5, size=20, prob=0.6) is
% handed over to mathml as dbinom(name(x) = 5, name(size) = ...). This
% macro removes the name of the arguments.
math(name(_) = R, M)
=> M = R.
% These two predicate are only used for ad hoc testing from within
% Prolog.
%
% Examples
% mathml(sin(x)).
% mathjax(sin(x)).
%
% mathml :-
% mathml(sin(x)).
%
mathml(R) :-
r2mathml(R, M),
atomic_list_concat(M, S),
writeln(R-S).
mathjax(R) :-
r2mathjax(R, M),
atomic_list_concat(M, S),
writeln(R-S).
% Performance can be a bit improved by putting Flags at the end of the
% list of arguments and having the R term as the first argument.
% However, some rules below use maplist. There it is convenient to have
% Flags in the beginning.
ml_(Flags, R, M)
=> ml(R, M, Flags).
jax_(Flags, R, M)
=> jax(R, M, Flags).
paren_(Flags, R, Paren)
=> paren(R, Paren, Flags).
denoting_(Flags, R, Den)
=> denoting(R, Den, Flags).
% Summation sign, product sign
%
% Sigma_range Arg
% Sigma_from^to Arg
%
% Same for product and Pi
%
math(sum_over(Arg, Range), M)
=> M = fn(subscript(sum, Range), [Arg]).
math(sum_over(Arg, From, To), M)
=> M = fn(subsupscript(sum, From, To), [Arg]).
mathml :-
mathml(sum_over('['(x, i), i)).
mathml :-
mathml(sum_over('['(x, i), i=1, n)).
math(prod_over(Arg, Range), M)
=> M = fn(subscript(prod, Range), [Arg]).
math(prod_over(Arg, From, To), M)
=> M = fn(subsupscript(prod, From, To), [Arg]).
mathml :-
mathml(prod_over('['(x, i), i)).
mathml :-
mathml(prod_over('['(x, i), i=1, n)).
% Subscripts like x[i]
%
% Terms like x[i] are first translated to subscript(x, i). Then, it is
% tested if the base is actually a power, and cases with simultaneous
% index and power are translated to subsubscript(x, index, power). This
% is necessary to avoid extra space in terms like x_i^2.
%
base(A, Base, Flags) :-
type(A, Type, Flags),
member(base(Base), Type).
index(A, Idx, Flags) :-
type(A, Type, Flags),
member(index(Idx), Type).
power(A, Pwr, Flags) :-
type(A, Type, Flags),
member(power(Pwr), Type).
math(A, M, _Flags),
compound(A),
compound_name_arguments(A, '[', [Base | Idx])
=> M = subscript(Base, list("", Idx)).
math(subscript(A, Idx), M, Flags),
power(A, Pwr, Flags),
base(A, Base, Flags)
=> M = subsupscript(Base, Idx, Pwr).
ml(subscript(Base, Idx), M, Flags)
=> ml(Base, X, Flags),
ml(Idx, Y, Flags),
M = msub([X, Y]).
jax(subscript(Base, Idx), M, Flags)
=> jax(Base, X, Flags),
jax(Idx, Y, Flags),
format(string(M), "{~w}_{~w}", [X, Y]).
prec(subscript(Base, _Idx), P, Flags)
=> prec(Base, P, Flags).
type(subscript(Base, Idx), Type, Flags)
=> type(Base, T, Flags),
Type = [base(Base), index(Idx) | T].
mathml :-
mathml(subscript(x, i)).
mathml :-
mathml('['(x, i)).
mathml :-
mathml('['(x, i, 2)).
% Superscripts like s^2
%
% See above for terms that have an index and a power at the same time.
%
math(Base^Pwr, M, _Flags)
=> M = superscript(Base, Pwr).
math(superscript(A, Pwr), M, Flags),
index(A, Idx, Flags),
base(A, Base, Flags)
=> M = subsupscript(Base, Idx, Pwr).
% Avoid parenthesis in sin^2 x
math(superscript(Base, Pwr), M, Flags),
type(Base, Type, Flags),
\+ member(special, Type),
prec(Base, P, Flags),
current_op(Hat, xfy, ^),
P >= Hat
=> M = superscript(paren(Base), Pwr).
ml(superscript(Base, Pwr), M, Flags)
=> ml(Base, X, Flags),
ml(Pwr, Y, Flags),
M = msup([X, Y]).
jax(superscript(Base, Pwr), M, Flags)
=> jax(Base, X, Flags),
jax(Pwr, Y, Flags),
format(string(M), "{~w}^{~w}", [X, Y]).
prec(superscript(_Base, _Pwr), P, _Flags)
=> current_op(P, xfy, ^).
type(superscript(Base, Pwr), Type, Flags)
=> type(Base, T, Flags),
Type = [base(Base), power(Pwr) | T].
mathml :-
mathml(superscript(x, 2)).
mathml :-
mathml(x^2).
mathml :-
mathml(-1 ^ 2).
% Subscripts and superscripts
%
math(subsupscript(Base, Idx, Pwr), M, Flags),
type(Base, Type, Flags),
\+ member(special, Type),
prec(Base, P, Flags),
current_op(Hat, xfy, ^),
P >= Hat
=> M = subsupscript(paren(Base), Idx, Pwr).
ml(subsupscript(Base, Idx, Pwr), M, Flags)
=> ml(Base, X, Flags),
ml(Idx, Y, Flags),
ml(Pwr, Z, Flags),
M = msubsup([X, Y, Z]).
jax(subsupscript(Base, Idx, Pwr), M, Flags)
=> jax(Base, X, Flags),
jax(Idx, Y, Flags),
jax(Pwr, Z, Flags),
format(string(M), "{~w}_{~w}^{~w}", [X, Y, Z]).
prec(subsupscript(Base, _Idx, Pwr), P, Flags)
=> prec(subscript(Base, Pwr), P, Flags).
type(subsupscript(Base, Idx, Pwr), Type, Flags)
=> type(Base, T, Flags),
Type = [base(Base), index(Idx), power(Pwr) | T].
mathml :-
mathml(subsupscript(x, i, 2)).
mathml :-
mathml(subsupscript(-1, i, 2)).
mathml :-
mathml('['(x, i)^2).
% Strings are translated to upright text
math(R, M),
string(R)
=> M = text(R).
ml(text(R), M, _Flags)
=> M = mtext(R).
jax(text(R), M, _Flags)
=> format(string(M), "\\mathrm{~w}", [R]).
type(text(_), T, _Flags)
=> T = [atomic].
mathml :-
mathml("text").
mathjax :-
mathjax("text").
% Atoms with the name of greek letters are shown in greek
math(R, M),
atom(R),
memberchk(R, [alpha, beta, gamma, delta, epsilon, varepsilon, zeta, eta,
theta, vartheta, iota, kappa, lambda, mu, nu, xi, pi, rho, sigma,
varsigma, tau, upsilon, phi, varphi, chi, psi, omega, 'Gamma', 'Delta',
'Theta', 'Lambda', 'Xi', 'Pi', 'Sigma', 'Upsilon', 'Phi', 'Psi',
'Omega'])
=> M = greek(R).
ml(greek(R), M, _Flags)
=> M = mi(&(R)).
jax(greek(R), M, _Flags)
=> format(string(M), "\\~w", [R]).
type(greek(_), T, _Flags)
=> T = [atomic].
mathml :-
mathml(alpha).
% Some special symbols that are rendered as is in MathML and MathJax
%
% As it is now, this is only the diamond.
math(R, M),
atom(R),
memberchk(R, [diamond])
=> M = symbol(R).
ml(&(A), M, _Flags)
=> M = &(A).
ml(symbol(R), M, _Flags)
=> M = mi(&(R)).
jax(symbol(R), M, _Flags)
=> format(string(M), "\\~w", [R]).
type(symbol(_), T, _Flags)
=> T = [atomic].
% Booleans
math(true, M)
=> M = boolean("T").
math(false, M)
=> M = boolean("F").
ml(boolean(R), M, _Flags)
=> M = mi(R).
jax(boolean(R), M, _Flags)
=> format(string(M), "~w", [R]).
type(boolean(_), T, _Flags)
=> T = [atomic].
mathml :-
mathml(true),
mathml(false).
% Sets
%
% render is.null(A) as A = \emptyset
math('is.null'(R), M)
=> M = (R == null).
math(null, M)
=> M = set(empty).
ml(set(empty), M, _Flags)
=> M = mi(&(empty)).
jax(set(empty), M, _Flags)
=> M = "\\emptyset".
type(set(empty), T, _Flags)
=> T = [atomic].
% Special functions with powers: sin^2(x)
%
% Note that powers are stored in the Flags.
math(sin(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(sin^Pwr, [A]).
math(sinpi(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(sinpi^Pwr, [A]).
math(cos(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(cos^Pwr, [A]).
math(cospi(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(cospi^Pwr, [A]).
math(tan(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(tan^Pwr, [A]).
math(tanpi(A), M, Flags, Flags2),
select(superscript(Pwr), Flags, Flags1)
=> Flags2 = Flags1,
M = fn(tanpi^Pwr, [A]).
% Special functions
%
special(A, _Flags) :-
atom(A),
memberchk(A, [sgn, sin, cos, tan, asin, arcsin, acos, arccos, atan,
arctan, arctan2, sinh, cosh, tanh, arsinh, arcosh, artanh, log,
exp, sum, prod, min, max, argmin, argmax]).
math(R, M, Flags),
special(R, Flags)
=> M = special(R).
% Summation sign is an operator
ml(special(sum), M, _Flags)
=> M = mo(&(sum)).
prec(special(sum), Prec, _Flags)
=> current(P, yfx, *),
Prec is P + 1.
ml(special(prod), M, _Flags)
=> M = mo(&(prod)).
prec(special(prod), Prec, _Flags)
=> current(P, yfx, *),
Prec is P.
ml(special(R), M, _Flags)
=> M = mi(R).
jax(special(sgn), M, _Flags)
=> M = "\\mathrm{sgn}\\,".
jax(special(argmin), M, _Flags)
=> M = "\\arg\\min".
jax(special(argmax), M, _Flags)
=> M = "{\\arg\\max}".
jax(special(R), M, _Flags)
=> format(string(M), "\\~w", [R]).
type(special(_), T, _Flags)
=> T = [special].
prec(special(sin), Prec, _Flags)
=> Prec = 0.
prec(special(cos), Prec, _Flags)
=> Prec = 0.
prec(special(tan), Prec, _Flags)
=> Prec = 0.
prec(special(sinh), Prec, _Flags)
=> Prec = 0.
prec(special(cosh), Prec, _Flags)
=> Prec = 0.
prec(special(tanh), Prec, _Flags)
=> Prec = 0.
prec(special(exp), Prec, _Flags)
=> Prec = 0.
prec(special(_), Prec, _Flags)
=> current(Prec, yfx, *).
mathml :-
mathml(exp(x)),
mathml(exp(x + y)).
% Space
%
math(space, M)
=> M = space(thinmathspace).
ml(space(W), M, _Flags)
=> M = mspace(width(W), []).
jax(space(thinmathspace), M, _Flags)
=> M = "\\,".
jax(space(_Width), M, _Flags)
=> M = "\\ ".
% Atoms (in R, "symbols" or "names") are rendered in the
% usual italic font (MathML renders multiletter atoms in upright font).
%
% Possible decorations: plain, bold, italic, cal (= calligraphic)
%
math(R, M),
atom(R)
=> M = ident(R).
math(plain(R), M, Flags0, Flags1)
=> M = R,
Flags1 = [mathvariant(plain) | Flags0].
math(bold(R), M, Flags0, Flags1)
=> M = R,
Flags1 = [mathvariant(bold) | Flags0].
math(italic(R), M, Flags0, Flags1)
=> M = R,
Flags1 = [mathvariant(italic) | Flags0].
math(cal(A), M, Flags, New)
=> New = [mathvariant(calligraphy) | Flags],
M = A.
ml(ident(R), M, Flags),
member(mathvariant(calligraphy), Flags)
=> M = mi(mathvariant(script), R).
ml(ident(R), M, Flags),
member(mathvariant(plain), Flags)
=> M = mi(mathvariant(normal), R).
ml(ident(R), M, Flags),
member(mathvariant(italic), Flags)
=> M = mi(mathvariant(italic), R).
ml(ident(R), M, Flags),
member(mathvariant(bold), Flags)
=> M = mi(mathvariant(bold), R).
ml(ident(R), M, _Flags)
=> M = mi(R).
jax(ident(R), M, Flags),
member(mathvariant(calligraphy), Flags)
=> format(string(M), "\\mathcal{~w}", [R]).
jax(ident(R), M, Flags),
member(mathvariant(plain), Flags)
=> format(string(M), "\\mathrm{~w}", [R]).
jax(ident(R), M, Flags),
member(mathvariant(italic), Flags)
=> format(string(M), "\\mathit{~w}", [R]).
jax(ident(R), M, Flags),
member(mathvariant(bold), Flags)
=> format(string(M), "\\mathbf{~w}", [R]).
jax(ident(R), M, _Flags)
=> format(string(M), "~w", [R]).
type(ident(_), T, _Flags)
=> T = [atomic].
% Linear model (render the equation)
math(lm(F, _Data), M)
=> M = F.
% Functions from the R package base
%
% ignore return
math(return(X), M)
=> M = X.
% |x|
math(length(R), M)
=> M = abs(R).
ml(abs(R), M, Flags)
=> ml(R, X, Flags),
M = mrow([mo(&(vert)), X, mo(&(vert))]).
jax(abs(R), M, Flags)
=> jax(R, X, Flags),
format(string(M), "{\\left\\vert{~w}\\right\\vert}", [X]).
paren(abs(_), P, _Flags)
=> P = 0.
prec(abs(R), P, Flags)
=> prec(paren(R), P, Flags).
math(sign(R), M)
=> M = fn(sgn, [R]).
ml(sqrt(R), M, Flags)
=> ml(R, X, Flags),
M = msqrt(X).
jax(sqrt(A), M, Flags)
=> jax(A, X, Flags),
format(string(M), "\\sqrt{~w}", [X]).
paren(sqrt(_), P, _Flags)
=> P = 0.
prec(sqrt(_), P, _Flags)
=> current_op(P0, xfy, ^),
P is P0 + 1.
math(sin(A), M)
=> M = fn(sin, [A]).
math(cos(A), M)
=> M = fn(cos, [A]).
math(tan(A), M)
=> M = fn(tan, [A]).
math(asin(A), M)
=> M = fn(superscript(sin, -1), [A]).
math(arcsin(A), M)
=> M = fn(superscript(sin, -1), [A]).
math(acos(A), M)
=> M = fn(superscript(cos, -1), [A]).
math(arccos(A), M)
=> M = fn(superscript(cos, -1), [A]).
math(atan(A), M)
=> M = fn(superscript(tan, -1), [A]).
math(arctan(A), M)
=> M = fn(superscript(tan, -1), [A]).
math(atan2(A, B), M)
=> M = fn(superscript(tan, -1), [A, B]).
math(sinpi(A), M)
=> M = fn(sin, [A*pi]).
math(cospi(A), M)
=> M = fn(cos, [A*pi]).
math(tanpi(A), M)
=> M = fn(tan, [A*pi]).
math(sinh(A), M)
=> M = fn(sinh, [A]).
math(cosh(A), M)
=> M = fn(cosh, [A]).
math(tanh(A), M)
=> M = fn(tanh, [A]).
math(asinh(A), M)
=> M = fn(superscript(sinh, -1), [A]).
math(acosh(A), M)
=> M = fn(superscript(cosh, -1), [A]).
math(atanh(A), M)
=> M = fn(superscript(tanh, -1), [A]).
% Show all as forall
math(all(A), M)
=> M = forall(A).
ml(forall(A), M, Flags)
=> ml(A, X, Flags),
M = mrow([mo(&('ForAll')), mo(&(af)), X]).
jax(forall(A), M, Flags)
=> jax(A, X, Flags),
format(string(M), "\\forall{~w}", [X]).
paren(forall(A), P, Flags)
=> paren(A, P, Flags).
prec(forall(_), P, _Flags)
=> current(P, yfx, *).
% Show any as exists
math(any(A), M)
=> M = exists(A).
ml(exists(A), M, Flags)
=> ml(A, X, Flags),
M = mrow([mo(&('Exists')), mo(&(af)), X]).
jax(exists(A), M, Flags)
=> jax(A, X, Flags),
format(string(M), "\\exists{~w}", [X]).
paren(exists(A), P, Flags)
=> paren(A, P, Flags).
prec(exists(_), P, _Flags)
=> current(P, yfx, *).
math(besselI(X, Nu), M)
=> M = fn(subscript('I', Nu), [paren(X)]).
math(besselK(X, Nu), M)
=> M = fn(subscript('K', Nu), [paren(X)]).
math(besselJ(X, Nu), M)
=> M = fn(subscript('J', Nu), [paren(X)]).
math(besselY(X, Nu), M)
=> M = fn(subscript('Y', Nu), [paren(X)]).
math(beta(A, B), M)
=> M = fn('B', [A, B]).
math(lbeta(A, B), M)
=> M = log(beta(A, B)).
math(gamma(A), M)
=> M = fn('Gamma', [paren(A)]).
math(lgamma(A), M)
=> M = log(gamma(A)).
math(digamma(A), M)
=> M = frac(d, d*A) * log(gamma(A)).
math(trigamma(A), M)
=> M = frac(d^2, (d*A)^2) * log(gamma(A)).
math(psigamma(x=A, deriv=Deriv), M)
=> M = psigamma(A, Deriv).
math(psigamma(A, Deriv), M)