Skip to content

Commit 61c9926

Browse files
committed
use a multiplication symbol when RHS is a negative number
The regex for the case when there's a real number on the LHS wasn't checking if the RHS was a negative number, just if it started with a digit. Checking the string rendering of the RHS is a bit indirect, but it articulates that you don't want a string of digits or + or - signs next to each other.
1 parent 6b574b3 commit 61c9926

4 files changed

Lines changed: 12 additions & 6 deletions

File tree

runtime/scripts/jme-display.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ var texOps = jme.display.texOps = {
426426
// if we'd end up with two digits next to each other, but from different arguments, we need a times symbol
427427
} else if(util.isInt(texArgs[i-1].charAt(texArgs[i-1].length-1)) && util.isInt(texArgs[i].charAt(0)) && !this.texifyWouldBracketOpArg(tree,i)) {
428428
use_symbol = true;
429-
//real number times something that doesn't start with a letter
430-
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^0-9]/)) {
429+
//real number times something that doesn't start with a digit or minus sign
430+
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^\-+0-9]/)) {
431431
use_symbol = false
432432
//number times a power of i
433433
} else if (jme.isOp(right.tok,'^') && jme.isType(right.args[0].tok,'number') && math.eq(right.args[0].tok.value,math.complex(0,1)) && jme.isType(left.tok,'number')) {

tests/jme-runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17862,8 +17862,8 @@ var texOps = jme.display.texOps = {
1786217862
// if we'd end up with two digits next to each other, but from different arguments, we need a times symbol
1786317863
} else if(util.isInt(texArgs[i-1].charAt(texArgs[i-1].length-1)) && util.isInt(texArgs[i].charAt(0)) && !this.texifyWouldBracketOpArg(tree,i)) {
1786417864
use_symbol = true;
17865-
//real number times something that doesn't start with a letter
17866-
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^0-9]/)) {
17865+
//real number times something that doesn't start with a digit or minus sign
17866+
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^\-0-9]/)) {
1786717867
use_symbol = false
1786817868
//number times a power of i
1786917869
} else if (jme.isOp(right.tok,'^') && jme.isType(right.args[0].tok,'number') && math.eq(right.args[0].tok.value,math.complex(0,1)) && jme.isType(left.tok,'number')) {

tests/jme/jme-tests.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,12 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
23342334
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('matrix([1,2])')},{matrixcommas:false}),'\\left ( \\begin{matrix} 1 & 2 \\end{matrix} \\right )', 'one-row matrix has no commas with matrixcommas: false');
23352335
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('vector(1,2)')}, {rowvector: true}),'\\left ( 1 , 2 \\right )', 'row vector has commas');
23362336
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('vector(1,2)')}, {rowvector: true, matrixcommas: false}),'\\left ( 1 \\quad 2 \\right )', 'row vector has no commas with matrixcommas: false');
2337+
2338+
var tree = Numbas.jme.compile('a*b');
2339+
var scope = new Numbas.jme.Scope([Numbas.jme.builtinScope, {variables: {a:Numbas.jme.builtinScope.evaluate('-2'), b:Numbas.jme.builtinScope.evaluate('-3')}}]);
2340+
var t2 = Numbas.jme.substituteTree(tree,scope);
2341+
assert.equal(Numbas.jme.display.texify(t2,'',scope), '-2 \\times -3', 'multiplication symbol used when RHS is a negative number');
2342+
assert.equal(Numbas.jme.display.treeToJME(t2,'',scope), '-2(-3)', 'multiplication symbol used when RHS is a negative number');
23372343
});
23382344

23392345
QUnit.test('expression to LaTeX', function(assert) {

tests/numbas-runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17198,8 +17198,8 @@ var texOps = jme.display.texOps = {
1719817198
// if we'd end up with two digits next to each other, but from different arguments, we need a times symbol
1719917199
} else if(util.isInt(texArgs[i-1].charAt(texArgs[i-1].length-1)) && util.isInt(texArgs[i].charAt(0)) && !this.texifyWouldBracketOpArg(tree,i)) {
1720017200
use_symbol = true;
17201-
//real number times something that doesn't start with a letter
17202-
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^0-9]/)) {
17201+
//real number times something that doesn't start with a digit or minus sign
17202+
} else if (jme.isType(left.tok,'number') && !isComplex(left.tok) && texArgs[i].match(/^[^\-0-9]/)) {
1720317203
use_symbol = false
1720417204
//number times a power of i
1720517205
} else if (jme.isOp(right.tok,'^') && jme.isType(right.args[0].tok,'number') && math.eq(right.args[0].tok.value,math.complex(0,1)) && jme.isType(left.tok,'number')) {

0 commit comments

Comments
 (0)