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

8 visualizaciones (últimos 30 días)
jia
jia el 15 de Jul. de 2024
Editada: surya venu el 16 de Jul. de 2024
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?

Respuestas (3)

Ayush Anand
Ayush Anand el 15 de Jul. de 2024
Editada: Ayush Anand el 15 de Jul. de 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
  1 comentario
jia
jia el 15 de Jul. de 2024
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])

Iniciar sesión para comentar.


surya venu
surya venu el 15 de Jul. de 2024
Editada: surya venu el 16 de Jul. de 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.

Rahul
Rahul el 15 de Jul. de 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])}
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!

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by