Octave to LaTeX

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

>> pkg install -forge symbolic
>> pkg load 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}

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.


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.