Skip to content

Commit c7464f1

Browse files
committed
escape $ and % in latex
1 parent 55852e8 commit c7464f1

9 files changed

+71
-27
lines changed

build/math-expressions.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/math-expressions_umd.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/converters/ast-to-latex.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ const operators = {
175175
}
176176
},
177177
"unit": function (operands) {
178-
if(operands[1] === "\\circ") {
179-
return operands[0] +"^{\\circ}"
178+
if (operands[1] === "\\circ") {
179+
return operands[0] + "^{\\circ}"
180180
} else {
181181
return operands[0] + " " + operands[1];
182182
}
@@ -188,7 +188,7 @@ const operators = {
188188

189189
// allowed multicharacter latex symbols
190190
// in addition to the below applied function symbols
191-
const allowedLatexSymbolsDefault = ['alpha', 'beta', 'gamma', 'Gamma', 'delta', 'Delta', 'epsilon', 'zeta', 'eta', 'theta', 'Theta', 'iota', 'kappa', 'lambda', 'Lambda', 'mu', 'nu', 'xi', 'Xi', 'pi', 'Pi', 'rho', 'sigma', 'Sigma', 'tau', 'Tau', 'upsilon', 'Upsilon', 'phi', 'Phi', 'chi', 'psi', 'Psi', 'omega', 'Omega', 'partial', "abs", "exp", "log", "ln", "log10", "sign", "sqrt", "erf", "cos", "cosh", "cot", "coth", "csc", "csch", "sec", "sech", "sin", "sinh", "tan", "tanh", 'arcsin', 'arccos', 'arctan', 'arccsc', 'arcsec', 'arccot', 'arg', 'Re', 'Im', 'det', 'angle', 'perp', 'circ', 'int'];
191+
const allowedLatexSymbolsDefault = ['alpha', 'beta', 'gamma', 'Gamma', 'delta', 'Delta', 'epsilon', 'zeta', 'eta', 'theta', 'Theta', 'iota', 'kappa', 'lambda', 'Lambda', 'mu', 'nu', 'xi', 'Xi', 'pi', 'Pi', 'rho', 'sigma', 'Sigma', 'tau', 'Tau', 'upsilon', 'Upsilon', 'phi', 'Phi', 'chi', 'psi', 'Psi', 'omega', 'Omega', 'partial', "abs", "exp", "log", "ln", "log10", "sign", "sqrt", "erf", "cos", "cosh", "cot", "coth", "csc", "csch", "sec", "sech", "sin", "sinh", "tan", "tanh", 'arcsin', 'arccos', 'arctan', 'arccsc', 'arcsec', 'arccot', 'arg', 'Re', 'Im', 'det', 'angle', 'perp', 'circ', '%', '$', 'int'];
192192

193193

194194
const convertLatexSymbolsDefault = {
@@ -415,7 +415,7 @@ class astToLatex {
415415
if (result.length <= 1 ||
416416
(typeof tree === 'string' && tree.match(/^\w+$/)) ||
417417
(Array.isArray(tree) && tree[0] === 'apply' && tree[1] !== "sqrt") ||
418-
result.match(/^\\left\(.*\\right\)$/)||
418+
result.match(/^\\left\(.*\\right\)$/) ||
419419
result.match(/^\\left\\langle.*\\right\\rangle$/)
420420
) {
421421
return true;
@@ -439,7 +439,9 @@ class astToLatex {
439439
return "\\" + string;
440440
else
441441
return "\\operatorname{" + string + '}';
442-
} else if (string === "\uff3f" && !this.showBlanks) {
442+
} else if (this.allowedLatexSymbols.includes(string))
443+
return "\\" + string;
444+
else if (string === "\uff3f" && !this.showBlanks) {
443445
return "";
444446
}
445447
return string;

lib/converters/latex-to-ast.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ const base_latex_rules = [
346346
['\\\\operatorname\\s*{\\s*[a-zA-Z0-9\\+\\-]+\\s*}', 'VARMULTICHAR'],
347347

348348
['\\\\[a-zA-Z]+(?![a-zA-Z])', 'LATEXCOMMAND'],
349+
['\\\\[$%]', 'LATEXCOMMAND'],
349350
['[a-zA-Z\uff3f$%]', 'VAR'],
350351
];
351352

@@ -359,7 +360,7 @@ const allowSimplifiedFunctionApplicationDefault = true;
359360

360361
// allowed multicharacter latex symbols
361362
// in addition to the below applied function symbols
362-
const allowedLatexSymbolsDefault = ['alpha', 'beta', 'gamma', 'Gamma', 'delta', 'Delta', 'epsilon', 'zeta', 'eta', 'theta', 'Theta', 'iota', 'kappa', 'lambda', 'Lambda', 'mu', 'nu', 'xi', 'Xi', 'pi', 'Pi', 'rho', 'sigma', 'Sigma', 'tau', 'Tau', 'upsilon', 'Upsilon', 'phi', 'Phi', 'chi', 'psi', 'Psi', 'omega', 'Omega', 'partial', 'angle', 'circ'];
363+
const allowedLatexSymbolsDefault = ['alpha', 'beta', 'gamma', 'Gamma', 'delta', 'Delta', 'epsilon', 'zeta', 'eta', 'theta', 'Theta', 'iota', 'kappa', 'lambda', 'Lambda', 'mu', 'nu', 'xi', 'Xi', 'pi', 'Pi', 'rho', 'sigma', 'Sigma', 'tau', 'Tau', 'upsilon', 'Upsilon', 'phi', 'Phi', 'chi', 'psi', 'Psi', 'omega', 'Omega', 'partial', 'angle', 'circ', '%', '$'];
363364

364365
// Applied functions must be given an argument so that
365366
// they are applied to the argument

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "math-expressions",
33
"description": "Perform basic equality testing and symbolic computations on mathematical expressions involving transcendental functions",
4-
"version": "2.0.0-alpha55",
4+
"version": "2.0.0-alpha56",
55
"author": {
66
"name": "Jim Fowler",
77
"email": "[email protected]",

spec/quick_ast-to-latex.spec.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -894,37 +894,49 @@ const objectsToTest = [
894894
'ast': ["*", ['angle', 'A', 'B', 'C'], 'x'],
895895
'latex': '\\left( \\angle ABC \\right) x',
896896
},
897+
{
898+
'ast': "$",
899+
'latex': '\\$',
900+
},
897901
{
898902
'ast': ["unit", "$", "x"],
899-
'latex': '$ x',
903+
'latex': '\\$ x',
904+
},
905+
{
906+
'ast': "%",
907+
'latex': '\\%',
900908
},
901909
{
902910
'ast': ["unit", "x", "%"],
903-
'latex': 'x %',
911+
'latex': 'x \\%',
912+
},
913+
{
914+
'ast': "circ",
915+
'latex': '\\circ',
904916
},
905917
{
906918
'ast': ["unit", "x", "deg"],
907919
'latex': 'x^{\\circ}',
908920
},
909921
{
910922
'ast': ["*", ["unit", "$", "x"], "y"],
911-
'latex': '\\left($ x\\right) y',
923+
'latex': '\\left(\\$ x\\right) y',
912924
},
913925
{
914926
'ast': ["*", ["unit", "x", "%"], "y"],
915-
'latex': '\\left(x %\\right) y',
927+
'latex': '\\left(x \\%\\right) y',
916928
},
917929
{
918930
'ast': ["*", ["unit", "x", "deg"], "y"],
919931
'latex': '\\left(x^{\\circ}\\right) y',
920932
},
921933
{
922934
'ast': ["*", "y", ["unit", "$", "x"]],
923-
'latex': 'y \\left($ x\\right)',
935+
'latex': 'y \\left(\\$ x\\right)',
924936
},
925937
{
926938
'ast': ["*", "y", ["unit", "x", "%"]],
927-
'latex': 'y \\left(x %\\right)',
939+
'latex': 'y \\left(x \\%\\right)',
928940
},
929941
{
930942
'ast': ["*", "y", ["unit", "x", "deg"]],

spec/quick_latex-to-ast-to-latex.spec.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,30 @@ var inputs = [
317317
['\\angle(A,B,C)x', '(\\angle ABC)x'],
318318
['(\\angle ABC)x', '(\\angle ABC)x'],
319319
'50^{\\circ}',
320-
'$8',
321-
'75%',
320+
'\\$',
321+
['$', '\\$'],
322+
'\\$8',
323+
['$8', '\\$8'],
324+
'\\%',
325+
['%', '\\%'],
326+
'75\\%',
327+
['75%', '75\\%'],
328+
'\\circ',
322329
['50^{\\circ} x', '(50^{\\circ}) x'],
323-
'$8x',
324-
['75%x', '(75%)x'],
330+
'\\$8x',
331+
['$8x', '\\$8x'],
332+
['75\\%x', '(75\\%)x'],
333+
['75%x', '(75\\%)x'],
325334
['x 50^{\\circ}', 'x (50^{\\circ})'],
326-
['x$8', 'x($8)'],
327-
['x 75%', 'x \\cdot 75%'],
335+
['x\\$8', 'x(\\$8)'],
336+
['x$8', 'x(\\$8)'],
337+
['x 75\\%', 'x \\cdot 75\\%'],
338+
['x 75%', 'x \\cdot 75\\%'],
328339
['x 50^{\\circ} y', 'x (50^{\\circ}) y'],
329-
['x$8y', 'x($8y)'],
330-
['x 75%y', '(x \\cdot 75%)y'],
340+
['x\\$8y', 'x(\\$8y)'],
341+
['x$8y', 'x(\\$8y)'],
342+
['x 75\\%y', '(x \\cdot 75\\%)y'],
343+
['x 75%y', '(x \\cdot 75\\%)y'],
331344
['\\int_a^bf(x)dx', '\\int_{a}^{b}f(x)dx'],
332345
'\\int f(x) dx',
333346
['\\int x^2 dx', '\\int x^{2} dx'],

spec/quick_latex-to-ast.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,25 +530,41 @@ var trees = {
530530
'\\angle A*B': ["*", ["angle", "A"], "B"],
531531
'\\angle (ABC)': ["angle", ["*", "A", "B", "C"]],
532532
'\\angle (A,B,C)': ["angle", "A", "B", "C"],
533+
'\\$': '$',
534+
'$': '$',
535+
'\\$x': ["unit", "$", "x"],
533536
'$x': ["unit", "$", "x"],
537+
'\\%': '%',
538+
'%': '%',
539+
'x\\%': ["unit", "x", "%"],
534540
'x%': ["unit", "x", "%"],
541+
'\\circ': 'circ',
535542
'x^\\circ': ["unit", "x", "deg"],
543+
'\\$xy': ["unit", "$", ["*", "x", "y"]],
536544
'$xy': ["unit", "$", ["*", "x", "y"]],
545+
'x\\%y': ["*", ["unit", "x", "%"], "y"],
537546
'x%y': ["*", ["unit", "x", "%"], "y"],
538547
'x^\\circ y': ["*", ["unit", "x", "deg"], "y"],
539548
'x^\\circ1': ["*", ["unit", "x", "deg"], 1],
549+
'y\\$x': ["*", "y", ["unit", "$", "x"]],
540550
'y$x': ["*", "y", ["unit", "$", "x"]],
551+
'yx\\%': ["unit", ["*", "y", "x"], "%"],
541552
'yx%': ["unit", ["*", "y", "x"], "%"],
542553
'yx^\\circ': ["*", "y", ["unit", "x", "deg"]],
543554
'{yx}^\\circ': ["unit", ["*", "y", "x"], "deg"],
544555
'(yx)^\\circ': ["unit", ["*", "y", "x"], "deg"],
556+
'y\\$xz': ["*", "y", ["unit", "$", ["*", "x", "z"]]],
545557
'y$xz': ["*", "y", ["unit", "$", ["*", "x", "z"]]],
558+
'yx\\%z': ["*", ["unit", ["*", "y", "x"], "%"], "z"],
546559
'yx%z': ["*", ["unit", ["*", "y", "x"], "%"], "z"],
547560
'yx^\\circ z': ["*", "y", ["unit", "x", "deg"], "z"],
548561
'(yx)^\\circ z': ["*", ["unit", ["*", "y", "x"], "deg"], "z"],
549562
'{yx}^\\circ z': ["*", ["unit", ["*", "y", "x"], "deg"], "z"],
563+
'\\$x\\%': ["unit", "$", ["unit", "x", "%"]],
550564
'$x%': ["unit", "$", ["unit", "x", "%"]],
565+
'\\%x\\$': ["*", "%", "x", "$"],
551566
'%x$': ["*", "%", "x", "$"],
567+
'x\\%y\\$z': ["*", ["unit", "x", "%"], "y", ["unit", "$", "z"]],
552568
'x%y$z': ["*", ["unit", "x", "%"], "y", ["unit", "$", "z"]],
553569
};
554570

0 commit comments

Comments
 (0)