How to show latex by override `disp` in classdef?

38 次查看(过去 30 天)
jia
jia 2024-7-15,9:08
编辑: surya venu 2024-7-16,4:27
classdef Myclass
properties
R;
end
methods
function obj = PointGroupElement(R)
obj.R = R;
end
function disp(obj)
symMatrixStr = symmatrix('R(1/3,[0,0,1])');
disp(symMatrixStr);
end
end
end
When I run this class in live editor, I expect return or show latex form R(1/3,[0,0,1]) rather that str 'R(1/3,[0,0,1])', and i also know, it will return latex form when i run symMatrixStr = symmatrix('R(1/3,[0,0,1])') in live editor. So, what should i do to override disp so that the class outputs the latex form?

回答(3 个)

Ayush Anand
Ayush Anand 2024-7-15,9:49
编辑:Ayush Anand 2024-7-15,9:50
I think there is a mistake in your class definition, as the class name and the constructor names are different in the snippet provided by you.
Fixing that gives me the result as expected; the returned form when the class is called from live script is latex itself, and not string:
classdef Myclass
properties
R;
end
methods
function obj = Myclass(R) %Fixing the constructor name
obj.R = R;
end
function disp(obj)
symMatrixStr = symmatrix('R(1/3,[0,0,1])');
disp(symMatrixStr);
end
end
end
  1 个评论
jia
jia 2024-7-15,10:04
Thanks for your answer, the name of class error is just a typo.
I want to obatin the result:
g = Myclass('R');
g % output R(1/3,[0,0,1])
rather that
g = Myclass('R');
disp(g) % output R(1/3,[0,0,1])

请先登录,再进行评论。


surya venu
surya venu 2024-7-15,9:56
编辑:surya venu 2024-7-16,4:27
Hi,
To display the LaTeX form of a symbolic matrix when using the "disp" method in a class definition in MATLAB, you need to ensure that the symbolic matrix is properly created and displayed. And I see that there's mismatch of class name and constructor.
Here is an example MATLAB code:
classdef Myclass
properties
R;
end
methods
function obj = Myclass(R)
obj.R = R;
end
function disp(obj)
symMatrix = sym(obj.R);
% Convert the symbolic matrix to LaTeX format
latexStr = latex(symMatrix);
disp(['$$', latexStr, '$$']);
end
end
end
To create an instance of your class and display the matrix in LaTeX format:
R = [1/3, 0, 0; 0, 1/3, 0; 0, 0, 1];
obj = Myclass(R);
disp(obj);
When you run this in the MATLAB Live Editor, it should display the matrix R in LaTeX format.
To know more about the "sym" and "latex" function, check out the links below:
Hope it helps.

Rahul
Rahul 2024-7-15,11:02
Hi Jia,
I could the reproduce the issue. According to the code shared, the output does come as a char 'R(1/3,[0,0,1])'. I understand that you require the output in latex formatting.
Here are some of the methods that can give your desired output.
Method 1
You can change the 'disp' function in the classdef 'Myclass' as follows:
function disp(obj)
symMatrixStr = symmatrix('R(1/3,[0,0,1])');
% Addition of 'latex' function to obtain latex representation
symMatrixStrLatex = latex(symMatrixStr);
disp(symMatrixStrLatex);
end
This will give the desired latex output as: \mathrm{R(1/3,[0,0,1])}
Method 2
You can run your existing code in the live editor. The output will not be of latex format. However, if you right-click on the output, you can leverage the option available stating 'copy as LaTex'. This will copy the output in latex formatting to your clipboard.
Hope this helps!

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by