Skip to content

Commit

Permalink
texify: put brackets around nested unary minus/plus
Browse files Browse the repository at this point in the history
fixes #394
  • Loading branch information
christianp committed Jul 27, 2015
1 parent 80aa974 commit 1099828
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
43 changes: 31 additions & 12 deletions runtime/scripts/jme-display.js
Expand Up @@ -261,16 +261,27 @@ var texOps = jme.display.texOps = {
'!': infixTex('\\neg '),

/** unary addition */
'+u': infixTex('+'),
'+u': function(thing,texArgs,settings) {
var tex = texArgs[0];
if( thing.args[0].tok.type=='op' ) {
var op = thing.args[0].tok.name;
if( op=='-u' || op=='+u' ) {
tex='\\left ( '+tex+' \\right )';
}
}
return '+'+tex;
},

/** unary minus */
'-u': (function(thing,texArgs,settings) {
var tex = texArgs[0];
if( thing.args[0].tok.type=='op' )
{
var op = thing.args[0].tok.name;
if(!(op=='/' || op=='*') && jme.precedence[op]>jme.precedence['-u']) //brackets are needed if argument is an operation which would be evaluated after negation
{
if(
op=='-u' || op=='+u' ||
(!(op=='/' || op=='*') && jme.precedence[op]>jme.precedence['-u']) //brackets are needed if argument is an operation which would be evaluated after negation
) {
tex='\\left ( '+tex+' \\right )';
}
}
Expand Down Expand Up @@ -345,7 +356,15 @@ var texOps = jme.display.texOps = {
return s;
}),
'/': (function(thing,texArgs) { return ('\\frac{ '+texArgs[0]+' }{ '+texArgs[1]+' }'); }),
'+': infixTex('+'),
'+': (function(thing,texArgs,settings) {
var a = thing.args[0];
var b = thing.args[1];
if(jme.isOp(b.tok,'+u') || jme.isOp(b.tok,'-u')) {
return texArgs[0]+' + \\left ( '+texArgs[1]+' \\right )';
} else {
return texArgs[0]+' + '+texArgs[1];
}
}),
'-': (function(thing,texArgs,settings) {
var a = thing.args[0];
var b = thing.args[1];
Expand All @@ -354,8 +373,8 @@ var texOps = jme.display.texOps = {
return texArgs[0]+' - '+texb;
}
else{
if(jme.isOp(b.tok,'+') || jme.isOp(b.tok,'-'))
return texArgs[0]+' - \\left( '+texArgs[1]+' \\right)';
if(jme.isOp(b.tok,'+') || jme.isOp(b.tok,'-') || jme.isOp(b.tok,'+u') || jme.isOp(b.tok,'-u'))
return texArgs[0]+' - \\left ( '+texArgs[1]+' \\right )';
else
return texArgs[0]+' - '+texArgs[1];
}
Expand Down Expand Up @@ -404,7 +423,7 @@ var texOps = jme.display.texOps = {
}
else
{
return '\\left ('+texArgs[0]+' \\right)!';
return '\\left ('+texArgs[0]+' \\right )!';
}
}),
'ceil': (function(thing,texArgs) { return '\\left \\lceil '+texArgs[0]+' \\right \\rceil';}),
Expand Down Expand Up @@ -493,11 +512,11 @@ var texOps = jme.display.texOps = {
if(thing.args[0].tok.type=='function' && thing.args[0].tok.name=='abs')
return '\\ln '+texArgs[0];
else
return '\\ln \\left( '+texArgs[0]+' \\right)';
return '\\ln \\left ( '+texArgs[0]+' \\right )';
},
'log': funcTex('\\log_{10}'),
'vector': (function(thing,texArgs,settings) {
return '\\left( '+texVector(thing,settings)+' \\right)';
return '\\left ( '+texVector(thing,settings)+' \\right )';
}),
'rowvector': (function(thing,texArgs,settings) {
if(thing.args[0].tok.type!='list')
Expand Down Expand Up @@ -867,12 +886,12 @@ var typeToTeX = jme.display.typeToTeX = {
return '\\left[ '+texArgs.join(', ')+' \\right]';
},
vector: function(thing,tok,texArgs,settings) {
return ('\\left( '
return ('\\left ( '
+ texVector(tok.value,settings)
+ ' \\right)' );
+ ' \\right )' );
},
matrix: function(thing,tok,texArgs,settings) {
return '\\left( '+texMatrix(tok.value,settings)+' \\right)';
return '\\left ( '+texMatrix(tok.value,settings)+' \\right )';
},
name: function(thing,tok,texArgs,settings) {
return texName(tok.name,tok.annotation);
Expand Down
11 changes: 8 additions & 3 deletions tests/jme.html
Expand Up @@ -837,13 +837,18 @@ <h2 id="qunit-userAgent"></h2>
equals(exprToLaTeX('10000000000000000000000000'),'1 \\times 10^{25}','scientific notation - 1*e^25');
equals(exprToLaTeX('47652000000000000000000000'),'4.7652 \\times 10^{25}','scientific notation - 4.7652*e^25');
equals(exprToLaTeX('ln(abs(x))'),'\\ln \\left | x \\right |','ln(abs(x)) - ln of absolute value has no parentheses');
equals(exprToLaTeX('ln(x)'),'\\ln \\left( x \\right)','ln(x) - ln of anything else has parentheses');
equals(exprToLaTeX('4-(x^2+x+1)',[]),'4 - \\left( x^{ 2 } + x + 1 \\right)','4-(x^2+x+1) - brackets round right-hand operand in subtraction kept when they\'re wrapping an addition.');
equals(exprToLaTeX('ln(x)'),'\\ln \\left ( x \\right )','ln(x) - ln of anything else has parentheses');
equals(exprToLaTeX('4-(x^2+x+1)',[]),'4 - \\left ( x^{ 2 } + x + 1 \\right )','4-(x^2+x+1) - brackets round right-hand operand in subtraction kept when they\'re wrapping an addition.');
equals(exprToLaTeX('(x^2+x+1)-4',[]),'x^{ 2 } + x + 1 - 4','(x^2+x+1)-4 - brackets round left-hand operand in subtraction can be dropped.');
equals(exprToLaTeX('x-(-1.5)','fractionnumbers,all'),'x + \\frac{3}{2}','x-(-1.5) with args [fractionNumbers,all] - display flags get carried through properly');
equals(exprToLaTeX('x-(5-p)',[]),'x - \\left( 5 - p \\right)','x-(5-p) - keep the brackets on the right');
equals(exprToLaTeX('x-(5-p)',[]),'x - \\left ( 5 - p \\right )','x-(5-p) - keep the brackets on the right');
equals(exprToLaTeX('3*5^2*19',['basic']),'3 \\times 5^{ 2 } \\times 19','3*5^2*19 with basic - always put a \\times between two digits')
equals(exprToLaTeX('exp(x)^2'),'\\left ( e^{ x } \\right )^{ 2 }','exp(x)^2 - put brackets round e^x')
equals(exprToLaTeX('-(-x)',[]),'-\\left ( -x \\right )','-(-x) - brackets around repeated minus')
equals(exprToLaTeX('+(-x)',[]),'+\\left ( -x \\right )','+(-x) - brackets around minus straight after addition')
equals(exprToLaTeX('3+(-2)',[]),'3 + \\left ( -2 \\right )','3+(-2) - brackets around minus on right side of addition')
equals(exprToLaTeX('3-(-2)',[]),'3 - \\left ( -2 \\right )','3-(-2) - brackets around minus on right side of subtraction')
equals(exprToLaTeX('2+(3+2)+(4-5)',[]),'2 + 3 + 2 + 4 - 5','don\'t put brackets round nested addition and subtraction')
});
});
</script>
Expand Down

0 comments on commit 1099828

Please sign in to comment.