Skip to content

Commit f8edf20

Browse files
committed
escape the TeX rendering of a single underscore as a variable name
A single underscore is a valid variable name. If the student enters an answer like `a_(b)` and "single-letter variable names" is turned on, it's interpreted as `a * _ * b`. The underscore wasn't escaped when rendered in LaTeX, so the whole expression would be `a_b`, which looks like it's been interpreted correctly. This changes `getNameInfo` so a variable name starting with an underscore always counts as `isLong` and is rendered in LaTeX with `\texttt` and the underscores are escaped.
1 parent 218e554 commit f8edf20

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

runtime/scripts/jme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3617,7 +3617,7 @@ var getNameInfo = jme.getNameInfo = function(name) {
36173617
nameInfo.primes = '';
36183618
nameInfo.letterLength = name.length;
36193619
}
3620-
nameInfo.isLong = nameInfo.letterLength > 1;
3620+
nameInfo.isLong = nameInfo.letterLength > 1 || nameInfo.root.match(/^_/);
36213621

36223622
return nameInfo;
36233623
}

tests/jme-runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12440,7 +12440,7 @@ var getNameInfo = jme.getNameInfo = function(name) {
1244012440
nameInfo.primes = '';
1244112441
nameInfo.letterLength = name.length;
1244212442
}
12443-
nameInfo.isLong = nameInfo.letterLength > 1;
12443+
nameInfo.isLong = nameInfo.letterLength > 1 || nameInfo.root.match(/^_/);
1244412444

1244512445
return nameInfo;
1244612446
}

tests/jme/jme-tests.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,9 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
23212321
{name: 'x_lambda\'', tex: 'x_{\\lambda}\''},
23222322
{name: 'x_1', tex: '\\dot{x}_{1}', annotations: ['dot'], description: 'annotations only apply to the root, not subscripts'},
23232323
{name: 'ä_1', tex: 'ä_{1}'},
2324-
{name: 'phi_ß', tex: '\\phi_{ß}'}
2324+
{name: 'phi_ß', tex: '\\phi_{ß}'},
2325+
{name: '_', tex: '\\texttt{\\_}'},
2326+
{name: '_a', tex: '\\texttt{\\_a}'},
23252327
]
23262328

23272329
var texifier = new Numbas.jme.display.Texifier();

tests/numbas-runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11776,7 +11776,7 @@ var getNameInfo = jme.getNameInfo = function(name) {
1177611776
nameInfo.primes = '';
1177711777
nameInfo.letterLength = name.length;
1177811778
}
11779-
nameInfo.isLong = nameInfo.letterLength > 1;
11779+
nameInfo.isLong = nameInfo.letterLength > 1 || nameInfo.root.match(/^_/);
1178011780

1178111781
return nameInfo;
1178211782
}

0 commit comments

Comments
 (0)