2

I have some complicated codes and I want to convert them in equation format.

After that, I will write them using LaTeX.

I am sharing following part of code:

Dx = hypot(bsxfun(@minus,x,x'),bsxfun(@minus,y,y'));
CarLaTeX
  • 62,716
  • Take a look at the minted package, if you want to automatically generate nicely formated source code from your .m files. If that's not what you seek to do, please clarify. – Christoph90 Apr 03 '18 at 08:58
  • 1
    I do not understand at all what you want to do, could you try to elaborate/clarify? – Torbjørn T. Apr 03 '18 at 09:55

1 Answers1

4

The Matlab function latex converts symbolic functions to LaTeX code (see https://nl.mathworks.com/help/symbolic/latex.html).

For your example, latex(Dx) returns

\sqrt{\left|{x - \overline{x}}\right|^{2} + \left|{y - \overline{y}}\right|^{2}}

which renders as

enter image description here

Note that you need to declare the variables first (i.e., syms x y).

In case your function is not symbolic, then you need to declare the variables and convert the function using symfun:

>> syms q w
>> Ax = @(q, w) sqrt (q .^ 2 + w .^ 2);
>> g = symfun(Ax,[q w])
>> latex(g)
\sqrt{q^{2} + w^{2}}
Marijn
  • 37,699
  • 1
    I tried but following error message was appeared. Undefined function 'latex' for input arguments of type 'double'. – user8151324 Apr 04 '18 at 13:38
  • The function is part of the Symbolic Math Toolbox (for Matlab, or the symbolic package for Octave). You may need to install and load this toolbox first. See also https://stackoverflow.com/questions/38240367/undefined-function-function-name-for-input-arguments-of-type-double/. – Marijn Apr 04 '18 at 14:56