Skip to content

Commit

Permalink
fix texify(set)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianp committed Jul 9, 2021
1 parent 123771e commit 22feab6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions runtime/scripts/jme-display.js
Expand Up @@ -537,7 +537,7 @@ var texOps = jme.display.texOps = {
}),
'set': function(tree,texArgs) {
if(tree.args.length==1 && tree.args[0].tok.type=='list') {
return '\\left\\{ '+this.render(tree.args[0])+' \\right\\}';
return '\\left\\{ '+this.render({tok: tree.args[0]})+' \\right\\}';
} else {
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
}
Expand Down Expand Up @@ -771,7 +771,7 @@ var typeToTeX = jme.display.typeToTeX = {
set: function(tree,tok,texArgs) {
texArgs = [];
for(var i=0;i<tok.value.length;i++) {
texArgs.push(this.render(tok.value[i]));
texArgs.push(this.render({tok: tok.value[i]}));
}
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
},
Expand Down
11 changes: 7 additions & 4 deletions tests/jme-runtime.js
Expand Up @@ -20006,7 +20006,7 @@ var texOps = jme.display.texOps = {
}),
'set': function(tree,texArgs) {
if(tree.args.length==1 && tree.args[0].tok.type=='list') {
return '\\left\\{ '+this.render(tree.args[0])+' \\right\\}';
return '\\left\\{ '+this.render({tok: tree.args[0]})+' \\right\\}';
} else {
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
}
Expand Down Expand Up @@ -20240,7 +20240,7 @@ var typeToTeX = jme.display.typeToTeX = {
set: function(tree,tok,texArgs) {
texArgs = [];
for(var i=0;i<tok.value.length;i++) {
texArgs.push(this.render(tok.value[i]));
texArgs.push(this.render({tok: tok.value[i]}));
}
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
},
Expand Down Expand Up @@ -20516,6 +20516,7 @@ Texifier.prototype = {
out='-'+out;
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.tex;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand All @@ -20538,8 +20539,7 @@ Texifier.prototype = {
* @param {number} n
* @returns {TeX}
*/
real_number: function(n)
{
real_number: function(n) {
var piD;
if(this.common_constants.pi && (piD = math.piDegree(n)) > 0)
n /= Math.pow(Math.PI*this.common_constants.pi.scale, piD);
Expand All @@ -20550,6 +20550,7 @@ Texifier.prototype = {
}
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.tex;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down Expand Up @@ -21269,6 +21270,7 @@ JMEifier.prototype = {
if(n<0 && out!='0')
out='-'+out;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down Expand Up @@ -21311,6 +21313,7 @@ JMEifier.prototype = {
}
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.name;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down
1 change: 1 addition & 0 deletions tests/jme/jme-tests.js
Expand Up @@ -1771,6 +1771,7 @@ Numbas.queueScript('go',['jme','jme-rules','jme-display','jme-calculus','localis
assert.equal(texify(Numbas.jme.compile('+(x-2)e^x')),'+\\left ( x - 2 \\right ) e^{ x }','+(x-2)e^x');
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('latex("\\{"+1+"\\}")')}),'{1}','slashes removed before braces in raw latex')
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('latex(safe("\\{"+1+"\\}"))')}),'\\{1\\}','slashes retained before curly braces in safe latex')
assert.equal(texify({tok:Numbas.jme.builtinScope.evaluate('set(1,2)')}),'\\left\\{ 1, 2 \\right\\}','texify a set')
});

QUnit.test('expression to LaTeX', function(assert) {
Expand Down
27 changes: 19 additions & 8 deletions tests/numbas-runtime.js
Expand Up @@ -15058,7 +15058,7 @@ var texOps = jme.display.texOps = {
}),
'set': function(tree,texArgs) {
if(tree.args.length==1 && tree.args[0].tok.type=='list') {
return '\\left\\{ '+this.render(tree.args[0])+' \\right\\}';
return '\\left\\{ '+this.render({tok: tree.args[0]})+' \\right\\}';
} else {
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
}
Expand Down Expand Up @@ -15292,7 +15292,7 @@ var typeToTeX = jme.display.typeToTeX = {
set: function(tree,tok,texArgs) {
texArgs = [];
for(var i=0;i<tok.value.length;i++) {
texArgs.push(this.render(tok.value[i]));
texArgs.push(this.render({tok: tok.value[i]}));
}
return '\\left\\{ '+texArgs.join(', ')+' \\right\\}';
},
Expand Down Expand Up @@ -15568,6 +15568,7 @@ Texifier.prototype = {
out='-'+out;
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.tex;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand All @@ -15590,8 +15591,7 @@ Texifier.prototype = {
* @param {number} n
* @returns {TeX}
*/
real_number: function(n)
{
real_number: function(n) {
var piD;
if(this.common_constants.pi && (piD = math.piDegree(n)) > 0)
n /= Math.pow(Math.PI*this.common_constants.pi.scale, piD);
Expand All @@ -15602,6 +15602,7 @@ Texifier.prototype = {
}
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.tex;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down Expand Up @@ -16321,6 +16322,7 @@ JMEifier.prototype = {
if(n<0 && out!='0')
out='-'+out;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down Expand Up @@ -16363,6 +16365,7 @@ JMEifier.prototype = {
}
var circle_constant_symbol = this.common_constants.pi && this.common_constants.pi.constant.name;
switch(piD) {
case undefined:
case 0:
return out;
case 1:
Expand Down Expand Up @@ -20122,7 +20125,7 @@ Question.prototype = /** @lends Numbas.Question.prototype */
};
var builtin_constants = tryGet(data,'builtin_constants');
if(builtin_constants) {
q.constantsTodo = Object.entries(builtin_constants).map(function(d){
q.constantsTodo.builtin = Object.entries(builtin_constants).map(function(d){
return {name: d[0], enable: d[1]};
});
}
Expand Down Expand Up @@ -20339,7 +20342,7 @@ Question.prototype = /** @lends Numbas.Question.prototype */
q.signals.on('constantsLoaded', function() {
var defined_constants = Numbas.jme.variables.makeConstants(q.constantsTodo.custom,q.scope);
q.constantsTodo.builtin.forEach(function(c) {
if(!c.enabled) {
if(!c.enable) {
c.name.split(',').forEach(function(name) {
if(defined_constants.indexOf(jme.normaliseName(name,q.scope))==-1) {
q.scope.deleteConstant(name);
Expand Down Expand Up @@ -20418,6 +20421,14 @@ Question.prototype = /** @lends Numbas.Question.prototype */
});
},

/** Get this question's scope object.
*
* @returns {Numbas.jme.Scope}
*/
getScope: function() {
return this.scope;
},

/** Generate this question's variables.
*/
generateVariables: function() {
Expand Down Expand Up @@ -28724,7 +28735,7 @@ Numbas.queueScript('answer-widgets',['knockout','util','jme','jme-display'],func
viewModel: function(params) {
this.answerJSON = params.answerJSON;
var p = this.part = params.part;
var scope = p.getScope();
var scope = Knockout.unwrap(p).getScope();
this.options = Knockout.unwrap(params.options);
this.showPreview = this.options.showPreview || false;
this.returnString = this.options.returnString || false;
Expand Down Expand Up @@ -28776,7 +28787,7 @@ Numbas.queueScript('answer-widgets',['knockout','util','jme','jme-display'],func
} else {
try {
var expr = Numbas.jme.compile(input);
var scope = p.getScope();
var scope = Knockout.unwrap(p).getScope();
var ruleset = new Numbas.jme.rules.Ruleset([],{});
expr = Numbas.jme.display.simplifyTree(expr, ruleset, scope);
return {valid: true, value: expr}
Expand Down

0 comments on commit 22feab6

Please sign in to comment.