forked from davidcarlisle/html5mathml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongdiv-mj-mml2.html
executable file
·99 lines (76 loc) · 2.08 KB
/
longdiv-mj-mml2.html
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
<!DOCTYPE html>
<html>
<head>
<title>MathJax MathML2 longdiv example</title>
<script>
function padtol (s,l) {
var v = "";
for (var tmp = s; tmp< l; tmp++) {
v = v + "\\?";
}
return v;
}
function longdiv(istr,jstr) {
il=istr.length;
jl=jstr.length;
j=parseInt(jstr,10);
ia=istr.substr(0,jl);
if(parseInt(ia,10)< j) {
jl= jl+1;
}
var jpadl=jstr.length+1;
ans="";
work="";
work=jstr + "\\hspace{.2em})\\hspace{-.2em}\\overline{\\strut\\hspace{1em}}\\hspace{-1.5em}&\\overline{\\strut" +istr +"}";
pad ="";
var ansline="";
rstr=istr.substr(0,jl);
for (var k=jl;k<=il;k++) {
r=parseInt(rstr,10);
a1=Math.floor(r / j);
ans = ans + a1;
var jtimes= j*a1;
rstr= (r - j*a1) + "";
work = work + "\\\\\n&" + pad + "\\underline{" + padtol((jtimes + "").length,(k==jl? jl:jpadl)) +jtimes +"}" ;
work = work + "\\\\\n&" + pad + padtol(rstr.length,(k==jl? jl:jpadl)) + rstr + istr.substr(k,1) ;
rstr = rstr + istr.substr(k,1);
pad = ((k==jl&& jl < jpadl ) ? "": "\\?") + pad;
}
var anspad="";
for (var tmp = 0; tmp< jl -1; tmp++) {
anspad = "\\?" + anspad;
}
return "\n\\def\\?{\\phantom{0}}\n\\begin{array}{r@{}l}\n&" + anspad + ans + "\\\\" + ansline + "\n" + work + "\n\\end{array}\n";
}
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
var MML = MathJax.ElementJax.mml,
TEX = MathJax.InputJax.TeX;
TEX.Definitions.macros.longdiv = "myLongDiv";
TEX.Parse.Augment({
myLongDiv: function (name) {
var num = this.GetArgument(name),
den = this.GetArgument(name);
//working! this.Push(TEX.Parse((num + " + " + den),this.stack.env).mml());
this.Push(TEX.Parse(longdiv(num,den),this.stack.env).mml());
}
});
});
</script>
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<p>\longdiv{12345678912}{325}</p>
$$
\longdiv{12345678912}{325}
$$
<p>\longdiv{534}{17}</p>
$$
\longdiv{534}{17}
$$
</body>
</html>