diff --git a/lib/expression/node/AssignmentNode.js b/lib/expression/node/AssignmentNode.js index 316c5a6d8..5fa093397 100644 --- a/lib/expression/node/AssignmentNode.js +++ b/lib/expression/node/AssignmentNode.js @@ -101,7 +101,7 @@ AssignmentNode.prototype._toTex = function(callbacks) { expr = '\\left(' + expr + '\\right)'; } - return '{' + latex.toSymbol(this.name) + '}=' + expr; + return latex.toSymbol(this.name) + '=' + expr; }; module.exports = AssignmentNode; diff --git a/lib/expression/node/FunctionAssignmentNode.js b/lib/expression/node/FunctionAssignmentNode.js index 1b2e357e5..8fb289954 100644 --- a/lib/expression/node/FunctionAssignmentNode.js +++ b/lib/expression/node/FunctionAssignmentNode.js @@ -122,9 +122,8 @@ FunctionAssignmentNode.prototype._toTex = function(callbacks) { expr = '\\left(' + expr + '\\right)'; } - return '\\mathrm{' + this.name + '}' - + '\\left({' + this.params.map(latex.toSymbol).join(', ') + '}\\right)=' //FIXME, this doesn't call toTex on the parameters AFAIK (toSymbol) - + expr; + return latex.toSymbol(this.name) + + '\\left(' + this.params.map(latex.toSymbol).join(',') + '\\right)=' + expr; }; module.exports = FunctionAssignmentNode; diff --git a/lib/expression/node/SymbolNode.js b/lib/expression/node/SymbolNode.js index 6d807d31d..d7efd94c1 100644 --- a/lib/expression/node/SymbolNode.js +++ b/lib/expression/node/SymbolNode.js @@ -104,7 +104,7 @@ SymbolNode.prototype.toString = function() { * @override */ SymbolNode.prototype._toTex = function(callbacks) { - return latex.toSymbol(this.name, callbacks); + return latex.toSymbol(this.name); }; module.exports = SymbolNode; diff --git a/lib/util/latex.js b/lib/util/latex.js index 543184648..86ecd8ecf 100644 --- a/lib/util/latex.js +++ b/lib/util/latex.js @@ -1,6 +1,6 @@ 'use strict'; -var symbols = { +exports.symbols = { // GREEK LETTERS Alpha: 'A', alpha: '\\alpha', Beta: 'B', beta: '\\beta', @@ -30,6 +30,7 @@ var symbols = { 'true': '\\mathrm{True}', 'false': '\\mathrm{False}', //other + i: 'i', //TODO use \i ?? inf: '\\infty', Inf: '\\infty', infinity: '\\infty', @@ -454,6 +455,22 @@ var units = { deg: '^{\\circ}' }; +//FIXME find a good solution so that single characters still +//get rendered in regular italic whereas single character units +//are rendered with \mathrm +exports.toSymbol = function (name) { + if (typeof exports.symbols[name] !== 'undefined') { + return '{' + exports.symbols[name] + '}'; + } + else if (name.indexOf('_') !== -1) { + //symbol with index (eg. alpha_1) + var index = name.indexOf('_'); + return '{' + toSymbol(name.substring(0, index)) + '}_{' + + toSymbol(name.substring(index + 1)) + '}'; + } + return '\\mathrm{' + name + '}'; +}; + function latexToFn(arr) { return function(value) { if (typeof arr[value] === 'string') { @@ -471,7 +488,6 @@ function latexToFn(arr) { }; } -exports.toSymbol = latexToFn(symbols); //returns the latex output for a given function exports.toFunction = function (node, callbacks, name) { diff --git a/test/expression/node/AssignmentNode.test.js b/test/expression/node/AssignmentNode.test.js index 86cb862dd..58a95e986 100644 --- a/test/expression/node/AssignmentNode.test.js +++ b/test/expression/node/AssignmentNode.test.js @@ -213,7 +213,7 @@ describe('AssignmentNode', function() { var b = new ConstantNode(3); var n = new AssignmentNode('b', b); - assert.equal(n.toTex(), '{b}={3}'); + assert.equal(n.toTex(), '\\mathrm{b}={3}'); }); it ('should LaTeX an AssignmentNode containing an AssignmentNode', function () { @@ -222,7 +222,7 @@ describe('AssignmentNode', function() { var n = new AssignmentNode('b', b); - assert.equal(n.toTex(), '{b}=\\left({{a}={2}}\\right)'); + assert.equal(n.toTex(), '\\mathrm{b}=\\left({\\mathrm{a}={2}}\\right)'); }); it ('should LaTeX an AssignmentNode with custom toTex', function () { diff --git a/test/expression/node/BlockNode.test.js b/test/expression/node/BlockNode.test.js index 8514fb00c..6d42be984 100644 --- a/test/expression/node/BlockNode.test.js +++ b/test/expression/node/BlockNode.test.js @@ -253,7 +253,7 @@ describe('BlockNode', function() { {node: new SymbolNode('foo'), visible:true} ]); - assert.equal(n.toTex(), '5\n{foo}={3};\nfoo'); + assert.equal(n.toTex(), '5\n\\mathrm{foo}={3};\n\\mathrm{foo}'); }); it ('should LaTeX a BlockNode with custom toTex', function () { diff --git a/test/expression/node/ConditionalNode.test.js b/test/expression/node/ConditionalNode.test.js index 5b4001cec..0c3fdb15e 100644 --- a/test/expression/node/ConditionalNode.test.js +++ b/test/expression/node/ConditionalNode.test.js @@ -257,7 +257,7 @@ describe('ConditionalNode', function() { it ('should LaTeX a ConditionalNode', function () { var n = new ConditionalNode(condition, a, b); - assert.equal(n.toTex(), '\\left\\{\\begin{array}{l l}{{a}={2}}, &\\quad{\\text{if}\\;true}\\\\{{b}={3}}, &\\quad{\\text{otherwise}}\\end{array}\\right.'); + assert.equal(n.toTex(), '\\left\\{\\begin{array}{l l}{\\mathrm{a}={2}}, &\\quad{\\text{if}\\;true}\\\\{\\mathrm{b}={3}}, &\\quad{\\text{otherwise}}\\end{array}\\right.'); }); it ('should LaTeX a ConditionalNode with custom toTex', function () { diff --git a/test/expression/node/FunctionAssignmentNode.test.js b/test/expression/node/FunctionAssignmentNode.test.js index c7c437f5c..bd4394ba0 100644 --- a/test/expression/node/FunctionAssignmentNode.test.js +++ b/test/expression/node/FunctionAssignmentNode.test.js @@ -195,7 +195,7 @@ describe('FunctionAssignmentNode', function() { var p = new OperatorNode('^', 'pow', [o, a]); var n = new FunctionAssignmentNode('f', ['x'], p); - assert.equal(n.toTex(), '\\mathrm{f}\\left({x}\\right)={\\left({\\frac{x}{2}}\\right) ^ {2}}'); + assert.equal(n.toTex(), '\\mathrm{f}\\left(\\mathrm{x}\\right)={\\left({\\frac{\\mathrm{x}}{2}}\\right) ^ {2}}'); }); it ('should LaTeX a FunctionAssignmentNode containing an AssignmentNode', function () { @@ -204,7 +204,7 @@ describe('FunctionAssignmentNode', function() { var n1 = new AssignmentNode('a', a); var n = new FunctionAssignmentNode('f', ['x'], n1); - assert.equal(n.toTex(), '\\mathrm{f}\\left({x}\\right)=\\left({{a}={2}}\\right)'); + assert.equal(n.toTex(), '\\mathrm{f}\\left(\\mathrm{x}\\right)=\\left({\\mathrm{a}={2}}\\right)'); }); it ('should LaTeX a FunctionAssignmentNode with custom toTex', function () { diff --git a/test/expression/node/IndexNode.test.js b/test/expression/node/IndexNode.test.js index 149d16bc6..83a74036a 100644 --- a/test/expression/node/IndexNode.test.js +++ b/test/expression/node/IndexNode.test.js @@ -279,10 +279,10 @@ describe('IndexNode', function() { ]; var n = new IndexNode(a, ranges); - assert.equal(n.toTex(), 'a[2, 1]'); + assert.equal(n.toTex(), '\\mathrm{a}[2, 1]'); var n2 = new IndexNode(a, []); - assert.equal(n2.toTex(), 'a[]') + assert.equal(n2.toTex(), '\\mathrm{a}[]') }); it ('should LaTeX an IndexNode with custom toTex', function () { @@ -307,7 +307,7 @@ describe('IndexNode', function() { var n = new IndexNode(a, [b, c]); - assert.equal(n.toTex(customFunction), 'a at const\\left(1, number\\right), const\\left(2, number\\right), '); + assert.equal(n.toTex(customFunction), '\\mathrm{a} at const\\left(1, number\\right), const\\left(2, number\\right), '); }); }); diff --git a/test/expression/node/SymbolNode.test.js b/test/expression/node/SymbolNode.test.js index 7152acf29..5147219ee 100644 --- a/test/expression/node/SymbolNode.test.js +++ b/test/expression/node/SymbolNode.test.js @@ -104,7 +104,7 @@ describe('SymbolNode', function() { it ('should LaTeX a SymbolNode', function () { var s = new SymbolNode('foo'); - assert.equal(s.toTex(), 'foo'); + assert.equal(s.toTex(), '\\mathrm{foo}'); }); it ('should LaTeX a SymbolNode with custom toTex', function () { diff --git a/test/expression/node/UpdateNode.test.js b/test/expression/node/UpdateNode.test.js index 543ef1ae7..5019b1d77 100644 --- a/test/expression/node/UpdateNode.test.js +++ b/test/expression/node/UpdateNode.test.js @@ -349,7 +349,7 @@ describe('UpdateNode', function() { var n = new UpdateNode(new IndexNode(a, ranges), v); - assert.equal(n.toTex(customFunction), 'a at const\\left(2, number\\right), const\\left(1, number\\right), equals const\\left(5, number\\right)'); + assert.equal(n.toTex(customFunction), '\\mathrm{a} at const\\left(2, number\\right), const\\left(1, number\\right), equals const\\left(5, number\\right)'); }); }); diff --git a/test/function/arithmetic/dotDivide.test.js b/test/function/arithmetic/dotDivide.test.js index 9a04cc445..b33b3809b 100644 --- a/test/function/arithmetic/dotDivide.test.js +++ b/test/function/arithmetic/dotDivide.test.js @@ -89,7 +89,7 @@ describe('dotDivide', function() { it('should LaTeX dotDivide', function () { var expression = math.parse('dotDivide(a,b)'); //TODO do this properly with matrices - assert.equal(expression.toTex(), '\\mathrm{dotDivide}\\left({a},{b}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{dotDivide}\\left({\\mathrm{a}},{\\mathrm{b}}\\right)'); }); }); diff --git a/test/function/arithmetic/dotMultiply.test.js b/test/function/arithmetic/dotMultiply.test.js index fbd12e57b..317a023b9 100644 --- a/test/function/arithmetic/dotMultiply.test.js +++ b/test/function/arithmetic/dotMultiply.test.js @@ -97,6 +97,6 @@ describe('dotMultiply', function() { it('should LaTeX dotMultiply', function () { var expression = math.parse('dotMultiply(a,b)'); //TODO do this properly with matrices - assert.equal(expression.toTex(), '\\mathrm{dotMultiply}\\left({a},{b}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{dotMultiply}\\left({\\mathrm{a}},{\\mathrm{b}}\\right)'); }); }); diff --git a/test/function/arithmetic/dotPow.test.js b/test/function/arithmetic/dotPow.test.js index 74b4be581..657e8656f 100644 --- a/test/function/arithmetic/dotPow.test.js +++ b/test/function/arithmetic/dotPow.test.js @@ -105,7 +105,7 @@ describe('dotPow', function() { it('should LaTeX dotPow', function () { var expression = math.parse('dotPow(a,b)'); //TODO do this properly with matrices - assert.equal(expression.toTex(), '\\mathrm{dotPow}\\left({a},{b}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{dotPow}\\left({\\mathrm{a}},{\\mathrm{b}}\\right)'); }); }); diff --git a/test/function/arithmetic/log.test.js b/test/function/arithmetic/log.test.js index 81025173f..ba36600ad 100644 --- a/test/function/arithmetic/log.test.js +++ b/test/function/arithmetic/log.test.js @@ -105,7 +105,7 @@ describe('log', function() { var expr1 = math.parse('log(e)'); var expr2 = math.parse('log(32,2)'); - assert.equal(expr1.toTex(), '\\ln\\left({e}\\right)'); + assert.equal(expr1.toTex(), '\\ln\\left({\\mathrm{e}}\\right)'); assert.equal(expr2.toTex(), '\\log_{2}\\left({32}\\right)'); }); diff --git a/test/function/arithmetic/norm.test.js b/test/function/arithmetic/norm.test.js index 7f4cd645b..967550430 100644 --- a/test/function/arithmetic/norm.test.js +++ b/test/function/arithmetic/norm.test.js @@ -105,7 +105,7 @@ describe('norm', function () { var expr1 = math.parse('norm(a)'); var expr2 = math.parse("norm(a,2)"); - assert.equal(expr1.toTex(), '\\left\\|{a}\\right\\|'); - assert.equal(expr2.toTex(), '\\mathrm{norm}\\left({a},{2}\\right)'); + assert.equal(expr1.toTex(), '\\left\\|{\\mathrm{a}}\\right\\|'); + assert.equal(expr2.toTex(), '\\mathrm{norm}\\left({\\mathrm{a}},{2}\\right)'); }); }); diff --git a/test/function/complex/arg.test.js b/test/function/complex/arg.test.js index 0d9a9fe83..5930fcc65 100644 --- a/test/function/complex/arg.test.js +++ b/test/function/complex/arg.test.js @@ -71,7 +71,7 @@ describe('arg', function() { it('should LaTeX arg', function () { var expression = math.parse('arg(1+i)'); - assert.equal(expression.toTex(), '\\arg\\left({{1} + {i}}\\right)'); + assert.equal(expression.toTex(), '\\arg\\left({{1} + {{i}}}\\right)'); }); }); diff --git a/test/function/complex/conj.test.js b/test/function/complex/conj.test.js index 6d29a225f..7db4659e3 100644 --- a/test/function/complex/conj.test.js +++ b/test/function/complex/conj.test.js @@ -54,7 +54,7 @@ describe('conj', function() { it('should LaTeX conj', function () { var expression = math.parse('conj(1+i)'); - assert.equal(expression.toTex(), '\\left({{1} + {i}}\\right)^{*}'); + assert.equal(expression.toTex(), '\\left({{1} + {{i}}}\\right)^{*}'); }); }); diff --git a/test/function/complex/im.test.js b/test/function/complex/im.test.js index f7eff9eba..51c7caf2f 100644 --- a/test/function/complex/im.test.js +++ b/test/function/complex/im.test.js @@ -48,7 +48,7 @@ describe('im', function() { it('should LaTeX im', function () { var expression = math.parse('im(1+i)'); - assert.equal(expression.toTex(), '\\Im\\left\\lbrace{{1} + {i}}\\right\\rbrace'); + assert.equal(expression.toTex(), '\\Im\\left\\lbrace{{1} + {{i}}}\\right\\rbrace'); }); }); diff --git a/test/function/complex/re.test.js b/test/function/complex/re.test.js index 59ad41a98..2a1ba487d 100644 --- a/test/function/complex/re.test.js +++ b/test/function/complex/re.test.js @@ -43,7 +43,7 @@ describe('re', function() { it('should LaTeX re', function () { var expression = math.parse('re(1+i)'); - assert.equal(expression.toTex(), '\\Re\\left\\lbrace{{1} + {i}}\\right\\rbrace'); + assert.equal(expression.toTex(), '\\Re\\left\\lbrace{{1} + {{i}}}\\right\\rbrace'); }); }); diff --git a/test/function/construction/number.test.js b/test/function/construction/number.test.js index e86086388..9d3474490 100644 --- a/test/function/construction/number.test.js +++ b/test/function/construction/number.test.js @@ -74,7 +74,7 @@ describe('number', function() { assert.equal(expr1.toTex(), '0'); assert.equal(expr2.toTex(), '\\left({1}\\right)'); - assert.equal(expr3.toTex(), '\\left(\\left({1}\\right){cm}\\right)'); + assert.equal(expr3.toTex(), '\\left(\\left({1}\\right){\\mathrm{cm}}\\right)'); }); }); diff --git a/test/function/construction/unit.test.js b/test/function/construction/unit.test.js index aa9e238c3..8af58d09a 100644 --- a/test/function/construction/unit.test.js +++ b/test/function/construction/unit.test.js @@ -73,7 +73,7 @@ describe('unit', function() { var expr1 = math.parse('unit(cm)'); var expr2 = math.parse('unit(1,cm)'); - assert.equal(expr1.toTex(), '\\left({cm}\\right)'); - assert.equal(expr2.toTex(), '\\left({\\left({1}\\right){cm}}\\right)'); + assert.equal(expr1.toTex(), '\\left({\\mathrm{cm}}\\right)'); + assert.equal(expr2.toTex(), '\\left({\\left({1}\\right){\\mathrm{cm}}}\\right)'); }); }); diff --git a/test/function/expression/eval.test.js b/test/function/expression/eval.test.js index 1912d9e6c..d5d34de4e 100644 --- a/test/function/expression/eval.test.js +++ b/test/function/expression/eval.test.js @@ -77,8 +77,8 @@ describe('eval', function() { var expr1 = math.parse('eval(expr)'); var expr2 = math.parse('eval(expr,scope)'); - assert.equal(expr1.toTex(), '\\mathrm{eval}\\left({expr}\\right)'); - assert.equal(expr2.toTex(), '\\mathrm{eval}\\left({expr},{scope}\\right)'); + assert.equal(expr1.toTex(), '\\mathrm{eval}\\left({\\mathrm{expr}}\\right)'); + assert.equal(expr2.toTex(), '\\mathrm{eval}\\left({\\mathrm{expr}},{\\mathrm{scope}}\\right)'); }); }); diff --git a/test/function/expression/help.test.js b/test/function/expression/help.test.js index 26c6003d4..ecb91a647 100644 --- a/test/function/expression/help.test.js +++ b/test/function/expression/help.test.js @@ -54,7 +54,7 @@ describe('help', function() { it('should LaTeX help', function () { var expression = math.parse('help(parse)'); - assert.equal(expression.toTex(), '\\mathrm{help}\\left({parse}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{help}\\left({\\mathrm{parse}}\\right)'); }); }); diff --git a/test/function/expression/parse.test.js b/test/function/expression/parse.test.js index 2ea84fd5d..ceabe6335 100644 --- a/test/function/expression/parse.test.js +++ b/test/function/expression/parse.test.js @@ -25,7 +25,7 @@ describe('parse', function() { it('should LaTeX parse', function () { var expression = math.parse('parse(expr,options)'); - assert.equal(expression.toTex(), '\\mathrm{parse}\\left({expr},{options}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{parse}\\left({\\mathrm{expr}},{\\mathrm{options}}\\right)'); }); }); diff --git a/test/function/probability/distribution.test.js b/test/function/probability/distribution.test.js index ae0167a01..dd2c7a0db 100644 --- a/test/function/probability/distribution.test.js +++ b/test/function/probability/distribution.test.js @@ -329,6 +329,6 @@ describe('distribution', function () { it('should LaTeX distribution', function () { var expression = math.parse('distribution(normal)'); - assert.equal(expression.toTex(), '\\mathrm{distribution}\\left({normal}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{distribution}\\left({\\mathrm{normal}}\\right)'); }); }); diff --git a/test/function/units/to.test.js b/test/function/units/to.test.js index bf0c25384..9bcebf106 100644 --- a/test/function/units/to.test.js +++ b/test/function/units/to.test.js @@ -77,7 +77,7 @@ describe('to', function() { it('should LaTeX to', function () { var expression = math.parse('to(2cm,m)'); - assert.equal(expression.toTex(), '\\left({{2} \\cdot {cm}}\\rightarrow{m}\\right)'); + assert.equal(expression.toTex(), '\\left({{2} \\cdot {\\mathrm{cm}}}\\rightarrow{\\mathrm{m}}\\right)'); }); }); diff --git a/test/function/utils/filter.test.js b/test/function/utils/filter.test.js index fe94a6710..47d08483d 100644 --- a/test/function/utils/filter.test.js +++ b/test/function/utils/filter.test.js @@ -48,7 +48,7 @@ describe('filter', function() { it('should LaTeX filter', function () { var expression = math.parse('filter(1,test)'); - assert.equal(expression.toTex(), '\\mathrm{filter}\\left({1},{test}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{filter}\\left({1},{\\mathrm{test}}\\right)'); }); }); diff --git a/test/function/utils/forEach.test.js b/test/function/utils/forEach.test.js index c921d3d43..d16c3530c 100644 --- a/test/function/utils/forEach.test.js +++ b/test/function/utils/forEach.test.js @@ -47,7 +47,7 @@ describe('forEach', function() { it('should LaTeX forEach', function () { var expression = math.parse('forEach([1,2,3],callback)'); - assert.equal(expression.toTex(), '\\mathrm{forEach}\\left({\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}},{callback}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{forEach}\\left({\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}},{\\mathrm{callback}}\\right)'); }); }); diff --git a/test/function/utils/import.test.js b/test/function/utils/import.test.js index 03ea9a1b8..1cf175276 100644 --- a/test/function/utils/import.test.js +++ b/test/function/utils/import.test.js @@ -157,7 +157,7 @@ describe('import', function() { it('should LaTeX import', function () { var expression = math.parse('import(object)'); - assert.equal(expression.toTex(), '\\mathrm{import}\\left({object}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{import}\\left({\\mathrm{object}}\\right)'); }); }); diff --git a/test/function/utils/map.test.js b/test/function/utils/map.test.js index db4aec156..ba95883a2 100644 --- a/test/function/utils/map.test.js +++ b/test/function/utils/map.test.js @@ -50,7 +50,7 @@ describe('map', function() { it('should LaTeX map', function () { var expression = math.parse('map([1,2,3],callback)'); - assert.equal(expression.toTex(), '\\mathrm{map}\\left({\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}},{callback}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{map}\\left({\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}},{\\mathrm{callback}}\\right)'); }); }); diff --git a/test/function/utils/print.test.js b/test/function/utils/print.test.js index 5bb5206bd..226074c50 100644 --- a/test/function/utils/print.test.js +++ b/test/function/utils/print.test.js @@ -46,7 +46,7 @@ describe('print', function() { it('should LaTeX print', function () { var expression = math.parse('print(template,values)'); - assert.equal(expression.toTex(), '\\mathrm{print}\\left({template},{values}\\right)'); + assert.equal(expression.toTex(), '\\mathrm{print}\\left({\\mathrm{template}},{\\mathrm{values}}\\right)'); }); });