-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
latex.js
322 lines (297 loc) · 10.3 KB
/
latex.js
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
/* eslint no-template-curly-in-string: "off" */
import escapeLatexLib from 'escape-latex'
import { hasOwnProperty } from './object.js'
export const latexSymbols = {
// GREEK LETTERS
Alpha: 'A',
alpha: '\\alpha',
Beta: 'B',
beta: '\\beta',
Gamma: '\\Gamma',
gamma: '\\gamma',
Delta: '\\Delta',
delta: '\\delta',
Epsilon: 'E',
epsilon: '\\epsilon',
varepsilon: '\\varepsilon',
Zeta: 'Z',
zeta: '\\zeta',
Eta: 'H',
eta: '\\eta',
Theta: '\\Theta',
theta: '\\theta',
vartheta: '\\vartheta',
Iota: 'I',
iota: '\\iota',
Kappa: 'K',
kappa: '\\kappa',
varkappa: '\\varkappa',
Lambda: '\\Lambda',
lambda: '\\lambda',
Mu: 'M',
mu: '\\mu',
Nu: 'N',
nu: '\\nu',
Xi: '\\Xi',
xi: '\\xi',
Omicron: 'O',
omicron: 'o',
Pi: '\\Pi',
pi: '\\pi',
varpi: '\\varpi',
Rho: 'P',
rho: '\\rho',
varrho: '\\varrho',
Sigma: '\\Sigma',
sigma: '\\sigma',
varsigma: '\\varsigma',
Tau: 'T',
tau: '\\tau',
Upsilon: '\\Upsilon',
upsilon: '\\upsilon',
Phi: '\\Phi',
phi: '\\phi',
varphi: '\\varphi',
Chi: 'X',
chi: '\\chi',
Psi: '\\Psi',
psi: '\\psi',
Omega: '\\Omega',
omega: '\\omega',
// logic
true: '\\mathrm{True}',
false: '\\mathrm{False}',
// other
i: 'i', // TODO use \i ??
inf: '\\infty',
Inf: '\\infty',
infinity: '\\infty',
Infinity: '\\infty',
oo: '\\infty',
lim: '\\lim',
undefined: '\\mathbf{?}'
}
export const latexOperators = {
transpose: '^\\top',
ctranspose: '^H',
factorial: '!',
pow: '^',
dotPow: '.^\\wedge', // TODO find ideal solution
unaryPlus: '+',
unaryMinus: '-',
bitNot: '\\~', // TODO find ideal solution
not: '\\neg',
multiply: '\\cdot',
divide: '\\frac', // TODO how to handle that properly?
dotMultiply: '.\\cdot', // TODO find ideal solution
dotDivide: '.:', // TODO find ideal solution
mod: '\\mod',
add: '+',
subtract: '-',
to: '\\rightarrow',
leftShift: '<<',
rightArithShift: '>>',
rightLogShift: '>>>',
equal: '=',
unequal: '\\neq',
smaller: '<',
larger: '>',
smallerEq: '\\leq',
largerEq: '\\geq',
bitAnd: '\\&',
bitXor: '\\underline{|}',
bitOr: '|',
and: '\\wedge',
xor: '\\veebar',
or: '\\vee'
}
export const latexFunctions = {
// arithmetic
abs: { 1: '\\left|${args[0]}\\right|' },
add: { 2: `\\left(\${args[0]}${latexOperators.add}\${args[1]}\\right)` },
cbrt: { 1: '\\sqrt[3]{${args[0]}}' },
ceil: { 1: '\\left\\lceil${args[0]}\\right\\rceil' },
cube: { 1: '\\left(${args[0]}\\right)^3' },
divide: { 2: '\\frac{${args[0]}}{${args[1]}}' },
dotDivide: { 2: `\\left(\${args[0]}${latexOperators.dotDivide}\${args[1]}\\right)` },
dotMultiply: { 2: `\\left(\${args[0]}${latexOperators.dotMultiply}\${args[1]}\\right)` },
dotPow: { 2: `\\left(\${args[0]}${latexOperators.dotPow}\${args[1]}\\right)` },
exp: { 1: '\\exp\\left(${args[0]}\\right)' },
expm1: `\\left(e${latexOperators.pow}{\${args[0]}}-1\\right)`,
fix: { 1: '\\mathrm{${name}}\\left(${args[0]}\\right)' },
floor: { 1: '\\left\\lfloor${args[0]}\\right\\rfloor' },
gcd: '\\gcd\\left(${args}\\right)',
hypot: '\\hypot\\left(${args}\\right)',
log: {
1: '\\ln\\left(${args[0]}\\right)',
2: '\\log_{${args[1]}}\\left(${args[0]}\\right)'
},
log10: { 1: '\\log_{10}\\left(${args[0]}\\right)' },
log1p: {
1: '\\ln\\left(${args[0]}+1\\right)',
2: '\\log_{${args[1]}}\\left(${args[0]}+1\\right)'
},
log2: '\\log_{2}\\left(${args[0]}\\right)',
mod: { 2: `\\left(\${args[0]}${latexOperators.mod}\${args[1]}\\right)` },
multiply: { 2: `\\left(\${args[0]}${latexOperators.multiply}\${args[1]}\\right)` },
norm: {
1: '\\left\\|${args[0]}\\right\\|',
2: undefined // use default template
},
nthRoot: { 2: '\\sqrt[${args[1]}]{${args[0]}}' },
nthRoots: { 2: '\\{y : $y^{args[1]} = {${args[0]}}\\}' },
pow: { 2: `\\left(\${args[0]}\\right)${latexOperators.pow}{\${args[1]}}` },
round: {
1: '\\left\\lfloor${args[0]}\\right\\rceil',
2: undefined // use default template
},
sign: { 1: '\\mathrm{${name}}\\left(${args[0]}\\right)' },
sqrt: { 1: '\\sqrt{${args[0]}}' },
square: { 1: '\\left(${args[0]}\\right)^2' },
subtract: { 2: `\\left(\${args[0]}${latexOperators.subtract}\${args[1]}\\right)` },
unaryMinus: { 1: `${latexOperators.unaryMinus}\\left(\${args[0]}\\right)` },
unaryPlus: { 1: `${latexOperators.unaryPlus}\\left(\${args[0]}\\right)` },
// bitwise
bitAnd: { 2: `\\left(\${args[0]}${latexOperators.bitAnd}\${args[1]}\\right)` },
bitNot: { 1: latexOperators.bitNot + '\\left(${args[0]}\\right)' },
bitOr: { 2: `\\left(\${args[0]}${latexOperators.bitOr}\${args[1]}\\right)` },
bitXor: { 2: `\\left(\${args[0]}${latexOperators.bitXor}\${args[1]}\\right)` },
leftShift: { 2: `\\left(\${args[0]}${latexOperators.leftShift}\${args[1]}\\right)` },
rightArithShift: { 2: `\\left(\${args[0]}${latexOperators.rightArithShift}\${args[1]}\\right)` },
rightLogShift: { 2: `\\left(\${args[0]}${latexOperators.rightLogShift}\${args[1]}\\right)` },
// combinatorics
bellNumbers: { 1: '\\mathrm{B}_{${args[0]}}' },
catalan: { 1: '\\mathrm{C}_{${args[0]}}' },
stirlingS2: { 2: '\\mathrm{S}\\left(${args}\\right)' },
// complex
arg: { 1: '\\arg\\left(${args[0]}\\right)' },
conj: { 1: '\\left(${args[0]}\\right)^*' },
im: { 1: '\\Im\\left\\lbrace${args[0]}\\right\\rbrace' },
re: { 1: '\\Re\\left\\lbrace${args[0]}\\right\\rbrace' },
// logical
and: { 2: `\\left(\${args[0]}${latexOperators.and}\${args[1]}\\right)` },
not: { 1: latexOperators.not + '\\left(${args[0]}\\right)' },
or: { 2: `\\left(\${args[0]}${latexOperators.or}\${args[1]}\\right)` },
xor: { 2: `\\left(\${args[0]}${latexOperators.xor}\${args[1]}\\right)` },
// matrix
cross: { 2: '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)' },
ctranspose: { 1: `\\left(\${args[0]}\\right)${latexOperators.ctranspose}` },
det: { 1: '\\det\\left(${args[0]}\\right)' },
dot: { 2: '\\left(${args[0]}\\cdot${args[1]}\\right)' },
expm: { 1: '\\exp\\left(${args[0]}\\right)' },
inv: { 1: '\\left(${args[0]}\\right)^{-1}' },
pinv: { 1: '\\left(${args[0]}\\right)^{+}' },
sqrtm: { 1: `{\${args[0]}}${latexOperators.pow}{\\frac{1}{2}}` },
trace: { 1: '\\mathrm{tr}\\left(${args[0]}\\right)' },
transpose: { 1: `\\left(\${args[0]}\\right)${latexOperators.transpose}` },
// probability
combinations: { 2: '\\binom{${args[0]}}{${args[1]}}' },
combinationsWithRep: { 2: '\\left(\\!\\!{\\binom{${args[0]}}{${args[1]}}}\\!\\!\\right)' },
factorial: { 1: `\\left(\${args[0]}\\right)${latexOperators.factorial}` },
gamma: { 1: '\\Gamma\\left(${args[0]}\\right)' },
lgamma: { 1: '\\ln\\Gamma\\left(${args[0]}\\right)' },
// relational
equal: { 2: `\\left(\${args[0]}${latexOperators.equal}\${args[1]}\\right)` },
larger: { 2: `\\left(\${args[0]}${latexOperators.larger}\${args[1]}\\right)` },
largerEq: { 2: `\\left(\${args[0]}${latexOperators.largerEq}\${args[1]}\\right)` },
smaller: { 2: `\\left(\${args[0]}${latexOperators.smaller}\${args[1]}\\right)` },
smallerEq: { 2: `\\left(\${args[0]}${latexOperators.smallerEq}\${args[1]}\\right)` },
unequal: { 2: `\\left(\${args[0]}${latexOperators.unequal}\${args[1]}\\right)` },
// special
erf: { 1: 'erf\\left(${args[0]}\\right)' },
// statistics
max: '\\max\\left(${args}\\right)',
min: '\\min\\left(${args}\\right)',
variance: '\\mathrm{Var}\\left(${args}\\right)',
// trigonometry
acos: { 1: '\\cos^{-1}\\left(${args[0]}\\right)' },
acosh: { 1: '\\cosh^{-1}\\left(${args[0]}\\right)' },
acot: { 1: '\\cot^{-1}\\left(${args[0]}\\right)' },
acoth: { 1: '\\coth^{-1}\\left(${args[0]}\\right)' },
acsc: { 1: '\\csc^{-1}\\left(${args[0]}\\right)' },
acsch: { 1: '\\mathrm{csch}^{-1}\\left(${args[0]}\\right)' },
asec: { 1: '\\sec^{-1}\\left(${args[0]}\\right)' },
asech: { 1: '\\mathrm{sech}^{-1}\\left(${args[0]}\\right)' },
asin: { 1: '\\sin^{-1}\\left(${args[0]}\\right)' },
asinh: { 1: '\\sinh^{-1}\\left(${args[0]}\\right)' },
atan: { 1: '\\tan^{-1}\\left(${args[0]}\\right)' },
atan2: { 2: '\\mathrm{atan2}\\left(${args}\\right)' },
atanh: { 1: '\\tanh^{-1}\\left(${args[0]}\\right)' },
cos: { 1: '\\cos\\left(${args[0]}\\right)' },
cosh: { 1: '\\cosh\\left(${args[0]}\\right)' },
cot: { 1: '\\cot\\left(${args[0]}\\right)' },
coth: { 1: '\\coth\\left(${args[0]}\\right)' },
csc: { 1: '\\csc\\left(${args[0]}\\right)' },
csch: { 1: '\\mathrm{csch}\\left(${args[0]}\\right)' },
sec: { 1: '\\sec\\left(${args[0]}\\right)' },
sech: { 1: '\\mathrm{sech}\\left(${args[0]}\\right)' },
sin: { 1: '\\sin\\left(${args[0]}\\right)' },
sinh: { 1: '\\sinh\\left(${args[0]}\\right)' },
tan: { 1: '\\tan\\left(${args[0]}\\right)' },
tanh: { 1: '\\tanh\\left(${args[0]}\\right)' },
// unit
to: { 2: `\\left(\${args[0]}${latexOperators.to}\${args[1]}\\right)` },
// utils
numeric: function (node, options) {
// Not sure if this is strictly right but should work correctly for the vast majority of use cases.
return node.args[0].toTex()
},
// type
number: {
0: '0',
1: '\\left(${args[0]}\\right)',
2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)'
},
string: {
0: '\\mathtt{""}',
1: '\\mathrm{string}\\left(${args[0]}\\right)'
},
bignumber: {
0: '0',
1: '\\left(${args[0]}\\right)'
},
bigint: {
0: '0',
1: '\\left(${args[0]}\\right)'
},
complex: {
0: '0',
1: '\\left(${args[0]}\\right)',
2: `\\left(\\left(\${args[0]}\\right)+${latexSymbols.i}\\cdot\\left(\${args[1]}\\right)\\right)`
},
matrix: {
0: '\\begin{bmatrix}\\end{bmatrix}',
1: '\\left(${args[0]}\\right)',
2: '\\left(${args[0]}\\right)'
},
sparse: {
0: '\\begin{bsparse}\\end{bsparse}',
1: '\\left(${args[0]}\\right)'
},
unit: {
1: '\\left(${args[0]}\\right)',
2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)'
}
}
export const defaultTemplate = '\\mathrm{${name}}\\left(${args}\\right)'
const latexUnits = {
deg: '^\\circ'
}
export function escapeLatex (string) {
return escapeLatexLib(string, { preserveFormatting: true })
}
// @param {string} name
// @param {boolean} isUnit
export function toSymbol (name, isUnit) {
isUnit = typeof isUnit === 'undefined' ? false : isUnit
if (isUnit) {
if (hasOwnProperty(latexUnits, name)) {
return latexUnits[name]
}
return '\\mathrm{' + escapeLatex(name) + '}'
}
if (hasOwnProperty(latexSymbols, name)) {
return latexSymbols[name]
}
return escapeLatex(name)
}