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

조회 수: 9 (최근 30일)
jia
jia 2024년 7월 15일
편집: surya venu 2024년 7월 16일
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일
편집: Ayush Anand 2024년 7월 15일
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일
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일
편집: surya venu 2024년 7월 16일
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일
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!

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by