Symbolic toolbox: printing greek letters using latex() function
4 views (last 30 days)
Show older comments
I use latex() function to print results of symbolic calculations and then paste results into a latex code. How do I make MatLab to add backslash in front of greek letters? Example:
syms xi a alpha
f = xi+a^alpha
latex(f)
This code produces output:
\mathrm{xi} + a^{\mathrm{alpha}}
What I want is to get a formula ready to paste into latex code (with backslashes in front of greek symbols):
\mathrm{\xi} + a^{\mathrm{\alpha}}
If it is not possible to do using Matlab, maybe there are other simple ways to automate the process of adding backslashes?
Thanks!
0 Comments
Accepted Answer
Walter Roberson
on 17 Mar 2016
regexprep(f, '(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega)', '\\$1', 'preservecase')
3 Comments
Svyatoslav Kharitonov
on 12 Mar 2018
Dear all,
I have tested function latex in MATLAB R2017b, and got quite confusing results.
The code:
alpha = sym('alpha');
beta = sym('beta');
psi = sym('psi');
omega = sym('omega');
latex(alpha)
latex(beta)
latex(psi)
latex(omega)
And results:
ans =
'\alpha '
ans =
'\mathrm{beta}'
ans =
'\mathrm{psi}'
ans =
'\omega '
Does someone know, where this difference of interpretation of greek letters comes from?
Thank you in advance!
Walter Roberson
on 12 Mar 2018
Edited: Walter Roberson
on 12 Mar 2018
Svyatoslav Kharitonov:
Interesting. Good question. I would call that a bug. If \mathrm is to be used then it would have to be \mathrm{\beta} not \mathrm{beta}
There turns out to be two expression to tex conversion routines in MuPAD (the symbolic engine.) One of them, them one seemingly called by latex(), is symobj::TeX, which invokes generate::TeX:inner with SimplifiedIdentifierRules set to true. The other one, the documented one, is generate::TeX which calls generate::TeX::inner with SimplifiedIdentifierRules set to false.
Now, called from the MATLAB level through the mupadmex() call, both of those routines produce the same answer, the \mathrm{beta} answer. But if you open a MuPAD notebook and execute at that level, then symobj::TeX returns \beta (which is valid) and generate::TeX returns \mathrm{beta} (which is not.) The interface to the symbolic engine is messing up the call.
Checking further, I see that if you directly feval(symengine,'symobj::TeX', 'beta') then you get the \beta result, but feval(symengine,'generate::TeX', sym('beta')) gets back the bogus \mathrm{beta} . This has to do with the deep details of the interface between the MATLAB level and the symbolic engine.
At the moment, the workaround seems to be to change
latex(SYMBOLIC_EXPRESSION)
to
sprintf(char(feval(symengine,'symobj::TeX', char(SYMBOLIC_EXPRESSION))))
... Or, much more simply, use a routine I had never heard of before but which has existed for a long time: https://www.mathworks.com/help/matlab/ref/texlabel.html
texlabel(SYMBOLIC_EXPRESSION)
I do not know how well this will work with more complicated expressions -- it appears it will use a more straight-forward conversion and less mathematical, such as {A}/{B} instead of \frac{A}{B}
I just filed a bug report on this.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!