4 views (last 30 days)

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 - ...

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/559205-don-t-simplify-while-using-latex#comment_924260

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/559205-don-t-simplify-while-using-latex#comment_924260

Sign in to comment.