diff --git a/lib/expression/node/OperatorNode.js b/lib/expression/node/OperatorNode.js index 0b7a86911..f52c0de90 100644 --- a/lib/expression/node/OperatorNode.js +++ b/lib/expression/node/OperatorNode.js @@ -121,8 +121,17 @@ function factory (type, config, load, typed, math) { //handle special cases for LaTeX, where some of the parentheses aren't needed if (latex && (operandPrecedence !== null)) { - var operandIdentifier = args[0].getIdentifier(); - var rootIdentifier = root.getIdentifier(); + var operandIdentifier; + var rootIdentifier; + if (config.parenthesis === 'keep') { + operandIdentifier = args[0].getIdentifier(); + rootIdentifier = root.getIdentifier(); + } + else { + //Ignore Parenthesis Nodes when not in 'keep' mode + operandIdentifier = args[0].getContent().getIdentifier(); + rootIdentifier = root.getContent().getIdentifier(); + } if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) { return [false]; } @@ -198,9 +207,20 @@ function factory (type, config, load, typed, math) { //handle special cases for LaTeX, where some of the parentheses aren't needed if (latex) { - var rootIdentifier = root.getIdentifier(); - var lhsIdentifier = root.args[0].getIdentifier(); - var rhsIdentifier = root.args[1].getIdentifier(); + var rootIdentifier; + var lhsIdentifier; + var rhsIdentifier; + if (config.parenthesis === 'keep') { + rootIdentifier = root.getIdentifier(); + lhsIdentifier = root.args[0].getIdentifier(); + rhsIdentifier = root.args[1].getIdentifier(); + } + else { + //Ignore ParenthesisNodes when not in 'keep' mode + rootIdentifier = root.getContent().getIdentifier(); + lhsIdentifier = root.args[0].getContent().getIdentifier(); + rhsIdentifier = root.args[1].getContent().getIdentifier(); + } if (lhsPrecedence !== null) { if (operators.properties[precedence][rootIdentifier].latexLeftParens === false) { @@ -323,6 +343,14 @@ function factory (type, config, load, typed, math) { } //handle some exceptions (due to the way LaTeX works) + var lhsIdentifier; + if (config.parenthesis === 'keep') { + lhsIdentifier = lhs.getIdentifier(); + } + else { + //Ignore ParenthesisNodes if in 'keep' mode + lhsIdentifier = lhs.getContent().getIdentifier(); + } switch (this.getIdentifier()) { case 'OperatorNode:divide': //op contains '\\frac' at this point @@ -330,7 +358,7 @@ function factory (type, config, load, typed, math) { case 'OperatorNode:pow': lhsTex = '{' + lhsTex + '}'; rhsTex = '{' + rhsTex + '}'; - switch (lhs.getIdentifier()) { + switch (lhsIdentifier) { case 'ConditionalNode': // case 'OperatorNode:divide': lhsTex = '\\left(' + lhsTex + '\\right)'; diff --git a/test/expression/node/OperatorNode.test.js b/test/expression/node/OperatorNode.test.js index b50b2164f..6729ebfd5 100644 --- a/test/expression/node/OperatorNode.test.js +++ b/test/expression/node/OperatorNode.test.js @@ -458,4 +458,11 @@ describe('OperatorNode', function() { assert.equal(pow.toTex(), '\\left({\\left\\{\\begin{array}{l l}{1}, &\\quad{\\text{if}\\;1}\\\\{1}, &\\quad{\\text{otherwise}}\\end{array}\\right.}\\right)^{1}'); }); + it ('should LaTeX simple expressions in \'auto\' mode', function () { + var autoMath = math.create({parenthesis: 'auto'}); + + //this covers a bug that was triggered previously + assert.equal(autoMath.parse('1+(1+1)').toTex(), '1+1+1'); + }); + });