mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
util/latex: new toSymbol
This commit is contained in:
parent
e160efbb66
commit
6b1d09f2cb
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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), ');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)^{*}');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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)');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user