Class that outputs text with formulas in livesript

조회 수: 2 (최근 30일)
Javier Ros
Javier Ros 2022년 10월 20일
답변: Walter Roberson 2023년 10월 5일
Hello,
I know that symbolic expressions are nicely displayed, but there are heavy limitations I cannot live with.
To name one: What if I want my symbolic variable ddtheta to show as . No way.
As there is no way to change the output behaviour of the symbolic toolbox up to this point,. I would like to write my own class, that outputs nicely formated formulas as desired.
I dont want a work arround as to use a msgbox displaying the formula.
This should not be that difficult, as symbolic classes have a natural ouput in this direction.
Can you point me to the right direction for implementation?.
I belive this is an important feature, is Mathworks going to implement something aroun this line?, like, forexample, extending disp allowing to include $a encapsulated formula$.
Thanks in advance.

답변 (2개)

arushi
arushi 2023년 10월 5일
편집: Walter Roberson 2023년 10월 5일
Hi Javier,
I understand that you would like to write a customized class that outputs nicely formatted formulas as desired.
You can define a custom class in MATLAB and override the "disp" method to control the display behaviour. Here is an example of a custom class which displays formulas as the desired:
classdef CustomFormula
  properties
  formula % Store the formula as a property
  end
methods
% Constructor
function obj = CustomFormula(formula)
if nargin > 0
obj.formula = formula;
  end
  end
% Override disp method for custom display
function disp(obj)
% Format the formula as desired
formattedFormula = obj.formatFormula();
% Display the formatted formula
disp(formattedFormula);
  end
% Custom method to format the formula
function formattedFormula = formatFormula(obj)
% Implement your own formatting logic here
% You can use string manipulation or LaTeX formatting
% Example: Replace ddtheta with \ddot{\theta}
formattedFormula = strrep(obj.formula, "ddtheta", "$\ddot{\theta}$");
  end
  end
end
In this example, the "CustomFormula" class is defined with a formula property to store the formula. The "disp" method is overridden to control the display behaviour. Inside the "disp" method, you can implement your own formatting logic in the "formatFormula" method.
You can customize the "formatFormula" method to apply your desired formatting rules to the formula string. In the example, it uses "strrep" to replace the string 'ddtheta' with the LaTeX representation '\ddot{\theta}'.
Once you have defined the "CustomFormula" class, you can create instances of it and display them using the "disp" function.
% Create a CustomFormula object
formulaObj = CustomFormula('ddtheta + 2 * theta');
% Display the formatted formula
disp(formulaObj);
I hope this helps you create a customized class.
  댓글 수: 2
Javier Ros
Javier Ros 2023년 10월 5일
이동: Walter Roberson 2023년 10월 5일

Yes I was thinking of using the disp method, but the question is that I want it to generate output that is rendered as latex formula in a live script. But having disp to generate latex code does not work.

So the piece of information I need is how do I make my particular latex code output to be rendered as a latex formula. For instance:

disp('$\theta$');

To show the Greek symbol theta in liveditor

Thanks!.

Walter Roberson
Walter Roberson 2023년 10월 5일
Is the task to emit latex code because you intend to copy it to somewhere else?
Is the task to emit latex code with the intention that livescript will render the latex code?
Is the task to dynamically emit something that will get rendered in livescript ?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2023년 10월 5일
What if I want my symbolic variable ddtheta to show as
syms ddtheta x
f = sin(x) + ddtheta
f = 
subs(f, ddtheta, sym('theta_ddot'))
ans = 

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by