mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Make Node.toTex a wrapper like Node.compile
Node.prototype.toTex is now a wrapper that calls the node's _toTex
This commit is contained in:
parent
37c1363c2d
commit
35ce7f7fb4
@ -99,7 +99,7 @@ ArrayNode.prototype.toString = function() {
|
||||
* @param {String} type
|
||||
* @return {String} str
|
||||
*/
|
||||
ArrayNode.prototype.toTex = function(customFunctions, type) {
|
||||
ArrayNode.prototype._toTex = function(customFunctions, type) {
|
||||
type = type || 'bmatrix';
|
||||
var s = '\\begin{' + type + '}';
|
||||
|
||||
|
||||
@ -92,7 +92,7 @@ AssignmentNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String}
|
||||
*/
|
||||
AssignmentNode.prototype.toTex = function(customFunctions) {
|
||||
AssignmentNode.prototype._toTex = function(customFunctions) {
|
||||
var precedence = operators.getPrecedence(this);
|
||||
var exprPrecedence = operators.getPrecedence(this.expr);
|
||||
|
||||
|
||||
@ -125,7 +125,7 @@ BlockNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
BlockNode.prototype.toTex = function(customFunctions) {
|
||||
BlockNode.prototype._toTex = function(customFunctions) {
|
||||
return this.blocks.map(function (param) {
|
||||
return param.node.toTex(customFunctions) + (param.visible ? '' : ';');
|
||||
}).join('\n');
|
||||
|
||||
@ -154,7 +154,7 @@ ConditionalNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
ConditionalNode.prototype.toTex = function(customFunctions) {
|
||||
ConditionalNode.prototype._toTex = function(customFunctions) {
|
||||
var s = (
|
||||
latex.addBraces(this.trueExpr.toTex(customFunctions)) +
|
||||
', &\\quad' +
|
||||
|
||||
@ -159,7 +159,7 @@ ConstantNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
ConstantNode.prototype.toTex = function(customFunctions) {
|
||||
ConstantNode.prototype._toTex = function(customFunctions) {
|
||||
var value = this.value,
|
||||
index;
|
||||
switch (this.valueType) {
|
||||
|
||||
@ -113,7 +113,7 @@ FunctionAssignmentNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
FunctionAssignmentNode.prototype.toTex = function(customFunctions) {
|
||||
FunctionAssignmentNode.prototype._toTex = function(customFunctions) {
|
||||
var precedence = operators.getPrecedence(this);
|
||||
var exprPrecedence = operators.getPrecedence(this.expr);
|
||||
|
||||
|
||||
@ -116,7 +116,7 @@ FunctionNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
FunctionNode.prototype.toTex = function(customFunctions) {
|
||||
FunctionNode.prototype._toTex = function(customFunctions) {
|
||||
return latex.toArgs(this, customFunctions);
|
||||
};
|
||||
|
||||
|
||||
@ -212,7 +212,7 @@ IndexNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
IndexNode.prototype.toTex = function(customFunctions) {
|
||||
IndexNode.prototype._toTex = function(customFunctions) {
|
||||
return this.object.toTex(customFunctions) + '[' + this.ranges.join(', ') + ']';
|
||||
};
|
||||
|
||||
|
||||
@ -222,10 +222,23 @@ Node.prototype.toString = function() {
|
||||
|
||||
/**
|
||||
* Get LaTeX representation
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String}
|
||||
*/
|
||||
Node.prototype.toTex = function() {
|
||||
return '';
|
||||
Node.prototype.toTex = function(customFunctions) {
|
||||
return this._toTex(customFunctions);
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal function to generate the LaTeX output.
|
||||
* This has to be implemented by every Node
|
||||
*
|
||||
* @param {Object}|function}
|
||||
* @throws {Error}
|
||||
*/
|
||||
Node.prototype._toTex = function () {
|
||||
//must be implemented by each of the Node implementations
|
||||
throw new Error('_toTex not implemented for this Node');
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -239,7 +239,7 @@ OperatorNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
OperatorNode.prototype.toTex = function(customFunctions) {
|
||||
OperatorNode.prototype._toTex = function(customFunctions) {
|
||||
var args = this.args;
|
||||
var parens = calculateNecessaryParentheses(this, args);
|
||||
var op = latex.toOperator(this.op); //operator
|
||||
|
||||
@ -124,7 +124,7 @@ RangeNode.prototype.toString = function() {
|
||||
* @params {Object|function} callback(s)
|
||||
* @return {String} str
|
||||
*/
|
||||
RangeNode.prototype.toTex = function(customFunctions) {
|
||||
RangeNode.prototype._toTex = function(customFunctions) {
|
||||
var str = this.start.toTex(customFunctions);
|
||||
if (this.step) {
|
||||
str += ':' + this.step.toTex(customFunctions);
|
||||
|
||||
@ -103,7 +103,7 @@ SymbolNode.prototype.toString = function() {
|
||||
* @return {String} str
|
||||
* @override
|
||||
*/
|
||||
SymbolNode.prototype.toTex = function(customFunctions) {
|
||||
SymbolNode.prototype._toTex = function(customFunctions) {
|
||||
return latex.toSymbol(this.name, customFunctions);
|
||||
};
|
||||
|
||||
|
||||
@ -87,7 +87,7 @@ UpdateNode.prototype.toString = function() {
|
||||
* @param {Object|function} callback(s)
|
||||
* @return {String}
|
||||
*/
|
||||
UpdateNode.prototype.toTex = function(customFunctions) {
|
||||
UpdateNode.prototype._toTex = function(customFunctions) {
|
||||
return this.index.toTex(customFunctions) + ' = ' + this.expr.toTex(customFunctions);
|
||||
};
|
||||
|
||||
|
||||
@ -74,9 +74,11 @@ describe('Node', function() {
|
||||
assert.equal(node.toString(), '');
|
||||
});
|
||||
|
||||
it ('should LaTeX a Node', function () {
|
||||
var node = new Node();
|
||||
assert.equal(node.toTex(), '');
|
||||
it ('should throw an error when calling toTex', function () {
|
||||
assert.throws(function () {
|
||||
var node = new Node();
|
||||
node.toTex();
|
||||
}, /_toTex not implemented for this Node/);
|
||||
});
|
||||
|
||||
it ('should throw an error in case of wrong arguments for compile', function () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user