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

**URL:** <https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219>\
**Category:** General Usage\
**Created:** [March 22, 2019, 10:03pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219 "2019-03-22T22:03:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 22, 2019, 10:03pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/1 "2019-03-22T22:03:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 22, 2019, 11:08pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/2 "2019-03-22T23:08:08Z")

</div>

You might want to go through SymPy:

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

```

with `args` detailed here: [Printing - SymPy 1.11 documentation](https://docs.sympy.org/latest/modules/printing.html#sympy.printing.latex.latex)

---

<div class="post-metadata">

**Author:** ![SebastianM-C](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastianm-c/32/2480_2.png) [@SebastianM-C](https://discourse.julialang.org/u/SebastianM-C)\
**Post date:** [March 22, 2019, 11:31pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/3 "2019-03-22T23:31:00Z")

</div>

Also check out

- **[Latexify.jl](https://github.com/korsbo/Latexify.jl)**
- **[LatexPrint.jl](https://github.com/scheinerman/LatexPrint.jl)**
- **[LaTeXTabulars.jl](https://github.com/tpapp/LaTeXTabulars.jl)**
- **[TexTables.jl](https://github.com/jacobadenbaum/TexTables.jl)**

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [March 22, 2019, 11:31pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/4 "2019-03-22T23:31:48Z")

</div>

```julia
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}
"

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 23, 2019, 1:56am UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/5 "2019-03-23T01:56:01Z")

</div>

Perfect, works like a charm. Thanks!

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [March 23, 2019, 6:30am UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/6 "2019-03-23T06:30:38Z")

</div>

```nohighlight
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"

```

---

<div class="post-metadata">

**Author:** ![pontus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pontus/32/10438_2.png) [@pontus](https://discourse.julialang.org/u/pontus)\
**Post date:** [November 6, 2019, 4:50pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/7 "2019-11-06T16:50:47Z")

</div>

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](https://tex.stackexchange.com/questions/50162/how-to-make-a-standalone-document-with-one-equation)):

```julia
\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:

 ![juliatex_compact_fignum_3x3](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9cbf9414d75ce28a8c5f48e3a3b2b424d28aca9.png)

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

```julia
\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:

 ![juliatex_article_nonum_3x3_v2](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b9d4bdff649c0a956c7c143a7871364583091c7.png)

---

<div class="post-metadata">

**Author:** ![Joseph\_Slater](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph_slater/32/48054_2.png) [@Joseph\_Slater](https://discourse.julialang.org/u/Joseph_Slater)\
**Post date:** [March 26, 2023, 5:50pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/8 "2023-03-26T17:50:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 26, 2023, 7:17pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/9 "2023-03-26T19:17:14Z")

</div>

What functionality are you missing from `Latexify.jl`?

---

<div class="post-metadata">

**Author:** ![Joseph\_Slater](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joseph_slater/32/48054_2.png) [@Joseph\_Slater](https://discourse.julialang.org/u/Joseph_Slater)\
**Post date:** [March 26, 2023, 11:15pm UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/10 "2023-03-26T23:15:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [March 27, 2023, 4:48am UTC](https://discourse.julialang.org/t/is-there-some-functionality-to-spit-out-latex-code-for-typesetting-matrices/22219/11 "2023-03-27T04:48:05Z")

</div>

I am just adding [PrettyTables](https://github.com/ronisbr/PrettyTables.jl) as another option for the completeness.
