# Octave to LaTeX

Is there anything (command/script/function) that can convert a Octave expression to LaTeX. What I am asking for is something like this:

I type

a = eye(3,3); %Identity Matrix


and then do something like textify_me(a) which produces

\begin{array}{ccc}
1 & 0 & 0 \\ blah blah blah
\end{array}


AND/OR

syms x
int(sym('x^2'))


produces

\frac{x^3}{3}


I am not asking for "sweaving" of codes, exporting figures or anything similar. I am simply asking for conversion of variables and symbolic expressions directly to LaTeX format.

MATLAB has a function latex which does this. But it is closed source. Is there something Open Source or should I attempt to try and accumulate people to write it?

• In the comments to this answer: tex.stackexchange.com/a/11716/86 Jan Hlavacek explains how to get Octave to print out a matrix in LaTeX format. It's an extremely useful comment! Commented Feb 7, 2012 at 18:20
• I'm afraid LaTeX output is not possible with Octave. :( If I may suggest one application I like, it's Maxima. And it has a LaTeX output, e.g. tex(factor(x^2+2*x+1)); gives you $$\left(x+1\right)^2$$. :) Commented Feb 7, 2012 at 18:45

If all you need is a matrix, you can do this:

strrep(strrep(mat2str(A),",","&"),";","\\\\\n")(2:end-1)


where A is your matrix. That will give you the body of your matrix, without the \begin{matrix} and \end{matrix}

strcat("\\begin{bmatrix}\n",strrep(strrep(mat2str(A),",","&"),";","\\\\\n")(2:end-1),"\n\\end{bmatrix}\n")


will generate the whole thing.

I don't think there is a more comprehensive solution in Octave.

Another option seems to be using scilab. It is also more or less MATLAB compatible (some say even more than Octave), and it has a prettyprint function that seems to do what you want. I have no experience with scilab, though.

Nowadays the latex function is available on Octave-Forge in the symbolic package.

>> pkg install -forge symbolic

>> a = eye(3);
>> latex(sym(a))
\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]

>> syms x
>> latex(int(sym('x^2')))
\frac{x^{3}}{3}


But it is closed source. Is there something Open Source or should I attempt to try and accumulate people to write it?

If you aren't completely wedded to Octave, you can use Sage to do this.

sage: M = matrix([[2,3],[3,2]])
sage: latex(M)
\left(\begin{array}{rr}
2 & 3 \\
3 & 2
\end{array}\right)
sage: a = integral(x^2,x)
sage: latex(a)
\frac{1}{3} \, x^{3}


If you really do need to do this with Octave, you can use the Sage to Octave and back interface as well. I don't have a local Octave install so I can't post some code, but I don't think there should be a huge problem with the flow Octave -> Sage -> Latex.

• This doesn't seem so satisfactory. At best it needs Sage, which is huge, and at worst you're suggesting switching languages entirely... Commented Feb 7, 2012 at 17:25
• Not exactly. Ignoring whether size is an issue these days with downloads (that depends a lot on where Nunoxic is), the same thing would work going to an online Sage server, assuming it has Octave installed (our local one, which I can't advertise, does). Again, untested... but it's worth a shot. Commented Feb 7, 2012 at 18:05
• I need code compatibility with MATLAB so sage isn't the best alternative.
– user10961
Commented Feb 7, 2012 at 19:02
• Unfortunately (and, to me, surprisingly), it looks like we don't have a rudimentary reverse parser yet, like we do with Maple and Mathematica. I've opened a ticket for this at trac.sagemath.org/sage_trac/ticket/12467. Commented Feb 7, 2012 at 19:52

You can also use this great function which converts a MATLAB array to LaTeX table. https://www.mathworks.com/matlabcentral/fileexchange/4894-matrix2latex

• This seems like a useful answer, but it can be improved by showing how it works on the example given in the question, i.e., the input for the function, the output in LaTeX code, and how the compiled table looks in a screenshot. Commented Dec 25, 2021 at 20:33