Matlab converts anonymous function to Latex expression

I want to print an anonymous function, so I found a function latex(which only accepts symbolic expressions):

f =@(x,a) (x/a) * exp(-x.^2 / (2*a^2));
latex(sym(f))

which outputs:

\frac{x\, \mathrm{e}^{-\frac{x^2}{2\, a^2}}}{a}

However, the above LaTeX syntax is not how I originally entered the function. I would like LaTeX syntax to display in the same way as I originally wrote, for example:

\frac{x}{a} \mathrm{e}^{-\frac{x^2}{2\, a^2}}

I would also like to start with a line. Is there any way to do this in MATLAB?

+4
source share
1 answer

@thewaywewalk, , , "" Matlab. , . ( , ), MuPAD hold:

f = @(x,a) (x/a) * exp(-x.^2 / (2*a^2));
fstr = regexprep(func2str(f), '^@\(.*?\)|\.', '') % Remove arguments, element-wise operators
fsym = feval(symengine, 'hold', fstr)             % Convert to symbolic expression
latex(fsym)

:

\left(\frac{x}{a}\right)\, \mathrm{e}^{- \frac{x^2}{2\, a^2}}

:

LaTeX expression

, , LateX . LaTeX (, , ), .

+3

Source: https://habr.com/ru/post/1607840/


All Articles