MATLAB Answers

Don't simplify while using 'latex'

4 views (last 30 days)
Niklas Kurz
Niklas Kurz on 3 Jul 2020
Edited: Niklas Kurz on 10 Jul 2020
Hey, each time I use Matlabs fabulous latex function the term automatically becomes simplified. However, sometimes I want to emphasize some expression that get reduced by the simplification. So: How to surpress it?

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jul 2020
In some of the earlier releases, it was possible to ask to, for example,
latex('3*x - 5*x')
It would not be unreasonable to want the latex generated to reflect the exact expression in the string, especially for educational purposes.
However, the way that latex() worked in those releases is that it use sym() to evaluate the quoted string into a symbolic expression, and then asked the symbolic engine to convert the symbolic expression into latex code. This process always applied all "automatic simplifications" such as removing any terms that involve multiplication by 0, adding or multiplying constants together, gathering like terms, and so on -- so before the symbolic engine even got asked to convert the expression into latex, the process of converting to symbolic expression would already have simplified the above example to -2*x .
In current versions, latex() cannot operate on a quoted string at all, only on symbolic expressions, and the conversion from quoted string to symbolic expression already does the kind of simplification described. Current code would have to look more like
latex(str2sym('3*x - 5*x'))
and although one layer of detail has changed, it is still going through the symbolic engine and the automatic simplifications are still done.
In symbolic computing, it is not uncommon to have procedures that need to focus on one part of the expression without executing another part of the expression. The maple-like languages such as MuPAD usually use a "hold" operation to say that an expression should not be evaluated; MuPAD uses hold() around the expression, whereas Maple uses delayed-evaluation quotes.
There are tricks you can use with MuPAD to use short functions to dip into the functionality of MuPAD while using MATLAB syntax. You can, for example, construct an internal MuPAD list object using such a trick, and that works well. Unfortunately, there are a couple of places that MATLAB just does not expect to get the resulting objects back from MuPAD and if you ask to display them, the symbolic engine will get reset. Asking to hold() an operation would be very useful at times, but it happens to be one of the situations in which the symbolic engine will get triggered.
So what can be done? Not a lot more other than telling the symbolic engine to do the entire operation, with you happening to know the internal MuPAD language. For example,
feval(symengine, 'symobj::TeX', 'hold(3*x-5*x)')
doesn't quite do the job, because MuPAD will generate the equivalent of -5*x+3*x . But
S = feval(symengine, 'symobj::TeX', '_minus(hold(3*x),hold(5*x))')
will generate
3\\,x\\setminus 5\\,x
which you can then post-process to change the \\ to \ and to add $ before and after to get the latex to use...
SS = sprintf(['$', S, '$']);
text(0.1,0.3, SS, 'interpreter', 'latex')
... except for the fact that the symbolic engine got confused about what kind of objects were being generated, and generated the Set-Minus operator \ instead of subtraction - ...


Niklas Kurz
Niklas Kurz on 4 Jul 2020
wow, that's some deep research and knowledge. It's astonishing how much effort is related to a simple suppression like this. It'd probably wise to write an own latex converter but I haven't the foggiest as far as this is concerned. I guess the feval-thing can come in handy, it seems like rewriting + and - is just more favourable then unwrapping the whole term. So to say, thank you for this lead.
Niklas Kurz
Niklas Kurz on 10 Jul 2020
I've worked with this thing now several times and I have to admit: this saves so much time. Hence I just have to say it again: Thank you!

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!