Develop many cases in LaTeX converter
dkl9

dkl9 commited on 2023-203 16:57:06
Showing 2 changed files, with 62 additions and 4 deletions.

... ...
@@ -137,7 +137,7 @@
137 137
 * implement binary operations on ASTs (returns new ASTs)
138 138
 * add functions `sum` and `product` (expecting syntactic input)
139 139
 
140
-## 2.3-dev 2022-294
140
+## 2.3-dev 2022-294, 2023-204
141 141
 
142 142
 * implement complex numbers
143 143
 * unify numeric types (reals also supported) into query-string-controlled list of types
... ...
@@ -146,4 +146,5 @@
146 146
 * fix bug with `sum` and `product` applied to tensors
147 147
 * add code-loading textbox
148 148
 * implement syntactic version of `for`
149
-* write library files `linalg.rt` and `polynomial.rt`
149
+* write library files `linalg.rt`, `polynomial.rt`, `integer.rt`
150
+* add function `latex` to convert AST to LaTeX
... ...
@@ -36,16 +36,73 @@ RTensorRender.toLaTeX = function() {
36 36
             const pl = this.l.shouldParenMul() ? `${lp}${l}${rp}` : l;
37 37
             const pr = this.r.shouldParenMul() ? `${lp}${r}${rp}` : r;
38 38
             switch (this.m) {
39
+                case 0:
40
+                    if ((this.r.ts == 7 || this.r.ts == 8) && this.r.l.v == "pm") {
41
+                        return `${l} ${r}`;
42
+                    }
43
+                    return `${l} + ${r}`;
44
+                case 1:
45
+                    return `${l} - ${r}`;
46
+                case 2:
47
+                    const dot = this.l.ts == 2 && this.r.ts == 2;
48
+                    return `${pl}${dot ? " \\cdot " : ""}${pr}`;
49
+                case 3:
50
+                    return `\\frac{${l}}{${r}}`;
51
+                case 4:
52
+                    const spe = this.l.shouldParenExp();
53
+                    return `${spe ? lp : ""}${l}${spe ? rp : ""}^{${r}}`;
54
+                case 5:
55
+                    return `\\log_{${l}}{${pr}}`;
56
+                case 6:
57
+                case 11:
58
+                    return `${l} = ${r}`;
59
+                case 7:
60
+                    return `\\sqrt[${l}]{${r}}`;
61
+                // TODO: comma operator is confusing
62
+                case 8:
63
+                    return `${l}, ${r}`;
64
+                case 9:
65
+                    return `${l} < ${r}`;
66
+                case 10:
67
+                    return `${l} \\mapsto ${r}`;
39 68
                 default:
40
-                    return `${l} ? ${r}`;
69
+                    return `${pl} ? ${pr}`;
41 70
             }
71
+            break;
72
+        case 4:
73
+            return this.l ? `${this.m.toLaTeX()} = ${this.r.toLaTeX()}` : this.m.toLaTeX();
74
+        case 5:
75
+            const m = this.m.toLaTeX();
76
+            const pm = this.m.shouldParenMul() ? `${lp}${m}${rp}` : m;
77
+            switch (this.l) {
78
+                case 0:
79
+                    return `+${pm}`;
80
+                case 1:
81
+                    return `-${pm}`;
82
+                case 3:
83
+                    return `\\frac{1}{${m}}`;
84
+                case 4:
85
+                    return `e^{${m}}`;
86
+                case 5:
87
+                    return `\\ln{${pm}}`;
88
+                case 6:
89
+                    return `${m} = 0`;
90
+                case 7:
91
+                    return `\\sqrt{${m}}`;
92
+                case 9:
93
+                    return `${m} < 0`;
94
+                default:
95
+                    return `? ${pm}`;
96
+            }
97
+        case 6:
98
+            return `${this.l.toLaTeX()}[${this.m.toLaTeX()}]`;
42 99
         default:
43 100
             return "?";
44 101
     }
45 102
 };
46 103
 
47 104
 const thingy = {
48
-   // fancy LaTeX version of the AST
105
+   // fancy HTML version of the AST
49 106
     toHTML() {
50 107
         const lp = `<span class="big">(</span>`;
51 108
         const rp = `<span class="big">)</span>`;
52 109