sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.39k stars 472 forks source link

LaTeX display of e.g. diff(x,t)^2 needs extra set of parenthesis (ambiguous output) #36494

Open QMeqGR opened 12 months ago

QMeqGR commented 12 months ago

Steps To Reproduce

sage: %display latex sage: t = var('t') sage: x = function('x')(t) sage: latex( diff(x,t)^2 ) \frac{\partial}{\partial t}x\left(t\right)^{2}

This displays essentially as (d/dt)x^2, when it should be (d/dt x)^2.

Screenshot_20231021_094630

Expected Behavior

I would expect the output to be something with an extra set of parenthesis like

sage: latex( diff(x,t)^2 ) \left( \frac{\partial}{\partial t}x\left(t\right) \right)^{2}

Actual Behavior

It appears as if the pre-parser in latex.py just concatenates the generated LaTeX code, and adds the square without considering what is to its left.

I.e.... "diff(x,t)" --> \frac{\partial}{\partial t}x\left(t\right) "^2" --> ^{2}

sage: sage.misc.latex.Latex()._latex_preparse(r'\sage{diff(x,t)}', locals()) '\frac{\partial}{\partial t}x\left(t\right)' sage: sage.misc.latex.Latex()._latex_preparse(r'\sage{diff(x,t)^2}', locals()) '\frac{\partial}{\partial t}x\left(t\right)^{2}'

Additional Information

I've stared at the code in latex.py but I'm not confident enough about how it works to make any changes myself.

Environment

Fedora 39
Sage 10.2 beta 7

Checklist

DaveWitteMorris commented 11 months ago

Thanks for reporting the bug.

The file latex.py doesn't do much, because each sage object defines its own latex representation. In the case of a symbolic expression, such as diff(x,t)^2, the representation is constructed by pynac (an offshoot of ginac), and that's where the bug is. I should be able to upload a PR soon to fix this.