convert Matlab results to Latex

215 views (last 30 days)
M
M on 24 Oct 2022
Answered: Steven Lord on 25 Oct 2022
Hi all, I obtained some equations in Matlab's command window, and I have to write them in Latex, but because they are too long and hard to follow, I would like to know if there is any way to convert them directly to Latex. Please see the below formula. Thanks in advance for any help.
Dhf1=(c.^8.*kipr.*vplc.^2.*(ct.*gamma - c.*(gamma + 1)))./(ki.^2.*(kb.*kp.^2.*(c.^4 + kc.^4 - (c.^4.*kh.^4)./(c.^4 + kh.^4)) + (c.^8.*h.*vplc.^2.*(kb + 1))./(ki.^2.*(c.^2 + kplc.^2).^2)).*(c.^2 + kplc.^2).^2) - (c.^16.*h.*kipr.*vplc.^4.*(ct.*gamma - c.*(gamma + 1)).*(kb + 1))./(ki.^4.*(kb.*kp.^2.*(c.^4 + kc.^4 - (c.^4.*kh.^4)./(c.^4 + kh.^4)) + (c.^8.*h.*vplc.^2.*(kb + 1))./(ki.^2.*(c.^2 + kplc.^2).^2)).^2.*(c.^2 + kplc.^2).^4);

Accepted Answer

Walter Roberson
Walter Roberson on 24 Oct 2022
Not exactly.
I can tell from the .^ that what you are showing is MATLAB code, not the output of the Symbolic Toolbox. There is no function provided that can convert from MATLAB code to Latex.
However if you have the Symbolic Toolbox, then
eqn = 'Dhf1=(c.^8.*kipr.*vplc.^2.*(ct.*gamma - c.*(gamma + 1)))./(ki.^2.*(kb.*kp.^2.*(c.^4 + kc.^4 - (c.^4.*kh.^4)./(c.^4 + kh.^4)) + (c.^8.*h.*vplc.^2.*(kb + 1))./(ki.^2.*(c.^2 + kplc.^2).^2)).*(c.^2 + kplc.^2).^2) - (c.^16.*h.*kipr.*vplc.^4.*(ct.*gamma - c.*(gamma + 1)).*(kb + 1))./(ki.^4.*(kb.*kp.^2.*(c.^4 + kc.^4 - (c.^4.*kh.^4)./(c.^4 + kh.^4)) + (c.^8.*h.*vplc.^2.*(kb + 1))./(ki.^2.*(c.^2 + kplc.^2).^2)).^2.*(c.^2 + kplc.^2).^4)';
symeqn = str2sym(eqn)
symeqn = 
latex(symeqn)
ans = '\mathrm{Dhf}_{1}=\frac{c^8\,\mathrm{kipr}\,{\mathrm{vplc}}^2\,\left(\mathrm{ct}\,\gamma -c\,\left(\gamma +1\right)\right)}{{\mathrm{ki}}^2\,\left(\mathrm{kb}\,{\mathrm{kp}}^2\,\left(c^4+{\mathrm{kc}}^4-\frac{c^4\,{\mathrm{kh}}^4}{c^4+{\mathrm{kh}}^4}\right)+\frac{c^8\,h\,{\mathrm{vplc}}^2\,\left(\mathrm{kb}+1\right)}{{\mathrm{ki}}^2\,{\left(c^2+{\mathrm{kplc}}^2\right)}^2}\right)\,{\left(c^2+{\mathrm{kplc}}^2\right)}^2}-\frac{c^{16}\,h\,\mathrm{kipr}\,{\mathrm{vplc}}^4\,\left(\mathrm{ct}\,\gamma -c\,\left(\gamma +1\right)\right)\,\left(\mathrm{kb}+1\right)}{{\mathrm{ki}}^4\,{\left(\mathrm{kb}\,{\mathrm{kp}}^2\,\left(c^4+{\mathrm{kc}}^4-\frac{c^4\,{\mathrm{kh}}^4}{c^4+{\mathrm{kh}}^4}\right)+\frac{c^8\,h\,{\mathrm{vplc}}^2\,\left(\mathrm{kb}+1\right)}{{\mathrm{ki}}^2\,{\left(c^2+{\mathrm{kplc}}^2\right)}^2}\right)}^2\,{\left(c^2+{\mathrm{kplc}}^2\right)}^4}'
Unfortunately there is no method provided to take that dynamic output and display it in formatted form.
If you happen to be using LiveScript, then what I show here as symeqn would be automatically displayed nicely -- same as what you see here in this Answer. (There is also a sympref() to disable abbreviating the expression by moving out to a separate expression on display.)

More Answers (1)

Steven Lord
Steven Lord on 25 Oct 2022

Community Treasure Hunt

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

Start Hunting!