How to show latex by override `disp` in classdef?
12 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
Ayush Anand
on 15 Jul 2024
Edited: Ayush Anand
on 15 Jul 2024
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
surya venu
on 15 Jul 2024
Edited: surya venu
on 16 Jul 2024
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.
0 Comments
Rahul
on 15 Jul 2024
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])}
You can refer to this link for more information regarding latex function: https://www.mathworks.com/help/releases/R2024a/symbolic/sym.latex.html?searchHighlight=latex&s_tid=doc_srchtitle
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!
0 Comments
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!