Use operator precedence to determine argument of /, etc
See original GitHub issueDescription
Arno and I have discussed this in the past, but I don’t think I ever put into an issue. Now I have…
@saivan brought up the problem again in a comment in https://github.com/arnog/mathlive/issues/185#issuecomment-489025046:
I’m not sure if I’m in the right place for this. But it seems like typing something like “sinx” followed by “/” should give you \frac{\sin{x}}{ _ }but currently, it gives you \sin \frac{x}{_}. I’d say that when you type “/”, the expectation is that the previous term to the immediate left of your cursor will end up on the top of the fraction.
Similarly for parenthesis, an opening parenthesis somewhere, should put the closing parenthesis (by default), at the complete end of the same scope.
I think the proper thing to do is to use an operator’s precedence in determining the argument to division and scripts and other 2D operators. The MathML operator dictionary is a good place to find relative precedence along with whether an operator is prefix, infix, or postfix, or potentially more than one of those (with different relative precedence values). In the case of sin x /
and similarly sin(x) /
, the operator dictionary puts function application at 850 and /
at 660. This means that sin
binds more tightly to it’s argument than does /
so that sin x
should be the numerator, not x
. [I’m not sure why U+2044 (fraction slash) and U+2215 (division slash) are given 265; seems like a mistake]
In order to do the right thing, you really need to parse the expression because you might have something more complicated such as sin^-1 sin x /
which should have sin^-1 sin x
as the numerator. I suspect that conversion to maston has to deal with this type of issue, so the functionality is at least partially in mathlive already. Years ago (actually now decades, sigh) I did that with the Mathematica frontend’s editor and that seems to have been well accepted. On the other hand, most editors don’t bother with this and I don’t see many complaints, so maybe people just are accepting of what they have to use 😌
FYI: for scripted expressions, the precedence should come from the base of the script (recursively). Typically this might happen with sums and integrals. The above example with sin ^ -1
doesn’t apply here because the operator is function application, not the sin
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Yep of course
3 + 4(sin(x) > 3 + 4[sin(x)]/[ __ ]
So for this one when you type the ( to the left of sin, it would implicitly add a bracket to the end of the expression, but when you type /, we destroy the original bracket and make sin(x) the numerator.
A similar thing would happen here:
3 + 4(sin(x) + 5x > 3 + 4(sin(x) + [5x]/[__]
Since we press the forward slash after the 5x term, we would most likely expect the 5x to become the numerator.
I can largely say that this is how you would read these expressions in english. For example, you’d say
three plus 4, open bracket, sin x plus five x over ...
. As a general rule, I’d expect to be able to type the expression in the way I’d read it, because that will avoid the need for me to back track when I realise that I forgot to type something.Oh wow, that was fast :p