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
200 stars 118 forks source link

The part feedback display doesn't have access to the scope feedback messages were produced in #858

Open christianp opened 2 years ago

christianp commented 2 years ago

If I use \var in a feedback message and refer to a variable defined in the marking scope, such as incorrect("Your position is $\\var{pos}$"), then it's not displayed correctly: the \var handler doesn't have access to the marking scope when it displays the message.

Actually, this could happen outside feedback, if a LaTeX string refers to a variable defined using let.

christianp commented 1 year ago

This happens because \var and \simplify are only evaluated once MathJax renders the LaTeX code, so that we don't have to parse/evaluate LaTeX twice.

One way round this would be to find \var and \simplify inside LaTeX in strings at the time they have plain-text substitutions applied, and evaluate them there. This used to be how it worked, but I must have encountered cases where a simple string-replace doesn't work. The most basic example of that that I can think of is \text{\var{a}} - this should display plain text \var{a} instead of interpreting it as a macro.

Constructing strings of LaTeX in variables is quite awkward: for example, if I want to randomise the coefficients in a polynomial, then I might try "\\simplify{ {a}x^2 + {b}x + {c} }". That isn't evaluated until MathJax sees it in the page, and the uses of the variables a, b and c aren't detected by findvars.