Is it possible to output expressions using Latex?
66 views (last 30 days)
Show older comments
Hi, I have to solve some expressions but I want my results to be easily understood, so I am looking for a method in order to print or to show my outputs in a Latex form just like the "Equation" does in the "Live Scripts".
For example: I have to output an expression like this:
m*double_dot_x+J*double_dot_theta
The method I use now is:
ddx = double_dot_x;
ddtheta = double_dot_theta;
so it is like: m*ddx+J*ddtheta which is better but still not very "user friendly"
I would like to have something like "\ddot x" and "\ddot \theta"
The problem now is that in a very long expression with terms like those it is very simple to confuse a dx with a ddx for istance, which is a big mistake in this case so the Latex rapresentation would be much better.
So the question is again is there a way to output in this way?
0 Comments
Answers (2)
Star Strider
on 16 May 2017
If you have the Symbolic Math Toolbox, it will format expressions into LaTeX for you.
Example —
syms J m theta(t) x(t) t
Expr = m*diff(x,2) + J*diff(theta,2);
LaTeX_Expr = latex(Expr)
LaTeX_Expr =
'm\,\frac{\partial ^2}{\partial t^2} x\left(t\right)+J\,\frac{\partial ^2}{\partial t^2} \mathrm{theta}\left(t\right)'
To use them in title and other objects in MATLAB, you then have to surround them sith '$' or '$$':
title(['$$' LaTeX_Expr '$$'], 'Interpreter','latex')
3 Comments
Star Strider
on 17 May 2017
I did not include all the code I used to test the title call, since it did not seem relevant at the time.
Here is the rest of it:
x = rand(1,100);
y = rand(1,100);
figure(1)
scatter(x, y)
grid
syms J m theta(t) x(t) t
Expr = m*diff(x,2) + J*diff(theta,2);
LaTeX_Expr = latex(Expr)
title(['$$' LaTeX_Expr '$$'], 'Interpreter','latex')
Walter Roberson
on 17 May 2017
No, you need to call latex() yourself -- unless you are willing to edit the source symbolic/symbolic/@sym/display.m
Steven Lord
on 16 May 2017
A second option involves using publish on your script or function and specify LaTeX as the 'format'.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!