numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
202 stars 117 forks source link

Care needs to be taken when substituting into a string used as the argument to `expression(.)` #827

Closed christianp closed 2 years ago

christianp commented 3 years ago

I had a variable defined as expression("2{b}cos(x)").

When b = -2, this produced expression("2-2cos(x)") - the unary negation sign is instead interpreted as a binary minus.

This is because string interpolation uses content subvars instead of JME subvars.

I think the best way to make this work how you expect is to add a special case to jme.substituteTree so that expression(str) is treated differently to just str.