Is there some functionality to spit out LaTeX code for typesetting matrices?

For instance to typeset 3 x 3 random matrix of floats, I would like to call a function (or a macro) to produce the typesetting code. Is there such a thing?

You might want to go through SymPy:

using SymPy
v = Sym.(rand(3,3))
args = ()
SymPy.sympy_meth(:latex, v, args...)

with args detailed here: Printing - SymPy 1.11 documentation

2 Likes

Also check out

6 Likes
julia> using Latexify

julia> A = rand(3,3)
3Ă—3 Array{Float64,2}:
 0.212977  0.297547  0.422277
 0.302519  0.522596  0.180855
 0.288964  0.01098   0.683853

julia> latexify(A)
L"\begin{equation}
\left[
\begin{array}{ccc}
0.212976937434459 & 0.29754727246675006 & 0.42227698240275924 \\
0.30251946700649746 & 0.5225962585963944 & 0.18085503939791114 \\
0.28896355463698153 & 0.010980031174606442 & 0.6838534272801575 \\
\end{array}
\right]
\end{equation}
"
6 Likes

Perfect, works like a charm. Thanks!

julia> using Reduce

julia> rounded(true)
true

julia> A = rand(3,3)
3Ă—3 Array{Float64,2}:
 0.939254  0.688392  0.499559
 0.18629   0.833578  0.503867
 0.172579  0.991595  0.93439 

julia> Algebra.latex(RExpr(A))
"\\begin{displaymath}\nmat\\left(0.939253842625,0.186290380376,0.172578870645\\right)\n\\end{displaymath}\n"
2 Likes

Have been learning LaTeX of late thanks to you all here…
Using output by @isaacsas, here are two templates that produce an isolated table output (vs. inserting the snippet into a larger TeX doc).
These use the {standalone} document class which requires the [preview] statement to be used as well. (I see now a monospace font would be better)
(EDIT: here is link to details on the standalone class and preview requirement: How to make a standalone document with one equation? - TeX - LaTeX Stack Exchange):

\documentclass[preview]{standalone}
\begin{document}
\begin{equation}
\left[
\begin{array}{ccc}
0.18458975587092463 & 0.9298785408616752 & 0.34542681064934255 \\
0.9326960389944068 & 0.40297929303916424 & 0.2930744179979483 \\
0.22849589421017158 & 0.1690502411807564 & 0.5325580036745081 \\
\end{array}
\right]
\end{equation}{~} % Remove the '{~}' to remove padding below figure
\end{document}

produces this, which has a figure # that may not be ideal. Note the {~} added for padding at bottom of array output for bottom padding:

To remove the figure number, used the $$ ... $$ syntax (\(...\) can be used in lieu of $$…$$) and removed begin/end {equation} :

\documentclass[preview]{standalone}
\begin{document}
$$
\left[
\begin{array}{ccc}
0.18458975587092463 & 0.9298785408616752 & 0.34542681064934255 \\
0.9326960389944068 & 0.40297929303916424 & 0.2930744179979483 \\
0.22849589421017158 & 0.1690502411807564 & 0.5325580036745081 \\
\end{array}
\right]
$${~} % Remove the '{~}' to remove padding below figure
\end{document}

produces:

I wrote array_to_latex to add more capable formatting. It’s Python, so you have to call it that way. I haven’t done so. Something like that should exist for Julia, but someone has to step up. I’m not familiar enough with Julia packaging and coding.

What functionality are you missing from Latexify.jl?

Not much, if any. Latexify is a very impressive package. I looked a bit and I don’t see anything now. It looks like this author “got the point” and really did a great job. I hadn’t realized the extent of it all. It puts my package to shame.

1 Like

I am just adding PrettyTables as another option for the completeness.

1 Like