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