필터 지우기
필터 지우기

I want to view a formula as a mathematical expression and not a single line

조회 수: 114 (최근 30일)
julien ruchot
julien ruchot 2023년 10월 23일
답변: Walter Roberson 2023년 10월 23일
Despite my best efforts, I cannot find a way to display a symbolic formula as a mathematical expression, making it readable, rather than written on a single line.
For exemple,
syms a b c
symstr = "sqrt(a)*x^2 + b*x + c";
displayFormula(symstr)
only gives me sqrt(a)*x^2 + b*x + c as an output when I would have liked to get:
Is there a setting that i must enable, or is an addon like "Pretty Equation Viewer" my only option to visualize a Matlab expression as a latex-formatted equation ?

답변 (2개)

Chunru
Chunru 2023년 10월 23일
For displaying the formatted formula, you can use "livescrip". Simply save your file as ".mlx" file and run it.
syms a b c
symstr = "sqrt(a)*x^2 + b*x + c";
displayFormula(symstr)

Walter Roberson
Walter Roberson 2023년 10월 23일
At the command window, the closest you can get is
syms a b c
symstr = "sqrt(a)*x^2 + b*x + c";
pretty(str2sym(symstr))
2 c + b x + sqrt(a) x
If you were using LiveScript or MATLAB Online or MATLAB Answers you can get
displayFormula(symstr)
If you were plotting then you could use
L = "$" + latex(str2sym(symstr)) + "$";
text(.1, .1, L, 'interpreter', 'latex')
Unfortunately, at the command window, there just is no possibility of outputting formatted graphics -- nothing you cannot generate through UNICODE characters such as √a x² + bx + c
Also unfortunately even in Livescript there is no way to build up latex and have it displayed into output -- displayFormula() is as close as you get.
I have just gone through the code. displayFormula() works by doing some fancy expression parsing, and converting the parse tree into MuPAD code, and evaluating the MuPAD code to get a symbolic expression (of type sym ) . Then it calls @sym/disp() on the expression. That function detects whether it is called at the command line or not, and if so then it converts the symbolic expression to plain text.
However if feature('SuppressCommandLineOutput') && sympref('TypesetOutput') (which typically would be true for LiveScript) then it calls createMathML to convert the sym to a MathML expression. And then it calls matlab.internal.language.signalVariableDisplay to draw the MathML .
That is, internally the way expressions are formatted for LiveScript is to create MathML . And that hints that if you created your own MathML and you were in LiveScript then you could potentially display your own rendered MathML . And Yes, it is possible to create your own MathML
However... I have not been able to get LiveScript to render the MathML I have created. Calling matlab.internal.language.signalVariableDisplay myself has never produced any output for me. I have made a few different attempts, but I just cannot seem to figure out how to get anything to render. The closest I have been able to get is to put a breakpoint inside @sym/disp.m and bash the appropriate variable field to change the MathML after it has been generated, and that change version does get rendered... but I can't figure out how to trigger rendering outside of that.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by