# How to print elements of a matrix in LaTeX formatting?

**URL:** <https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309>\
**Category:** General Usage\
**Tags:** question, latex, display, pretty-printing\
**Created:** [March 7, 2024, 4:30pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309 "2024-03-07T16:30:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [March 7, 2024, 4:30pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309/1 "2024-03-07T16:30:54Z")

</div>

Given a matrix

```julia
A = [[182740835762038560236502360265029360346025236045;;
 2];
[328470287402983460892460286408129634;;
 4]]

```

I’d like to get LaTeX-formatted output, _one_ element per line, e.g.:

```julia
latexprint(A)

A_{1,1} = 182740835762038560236502360265029360346025236045
...
A_{2,2} = 4

```

Is there a package to do this?

If not, how do I:

- Substitute the variable name `A` into a string?
- Get all the Cartesian indices for the matrix and interpolate the string with them? `eachindex()` is `1:4` in this case, which is not very convenient.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 7, 2024, 4:38pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309/2 "2024-03-07T16:38:11Z")

</div>

somthing like

```julia
["A_{$i,$j} = $(A[i,j])" for i in 1:size(A,1), for j in 1:size(A,2)]

```

?

You might take a look at LaTeXStrings.jl

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [March 7, 2024, 4:49pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309/3 "2024-03-07T16:49:23Z")

</div>

Thanks!

> [@lrnv](#):
>
> `"A_{$i,$j}`

Can I put the variable name here programmatically?

> [@lrnv](#):
>
> `i in 1:size(A,1), for j in 1:size(A,2)`

Julia warns one should use `eachindex` and the like, but I doubt I will ever need to print offset arrays. However, the disadvantage is that I can’t print arbitrary Nd-arrays like this.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 7, 2024, 4:55pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309/4 "2024-03-07T16:55:48Z")

</div>

For Nd arrays, use `CartesianIndices`. Look at the docs with `?CartesianIndex` or maybe `?CartesianIndices` directly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 7, 2024, 5:02pm UTC](https://discourse.julialang.org/t/how-to-print-elements-of-a-matrix-in-latex-formatting/111309/5 "2024-03-07T17:02:07Z")

</div>

> [@homocomputeris](#):
>
> Can I put the variable name here programmatically?

Not with a function. You do could do it with a macro, e.g. you could write a macro:

```julia
macro latexprint(A)
    :(latexprint(A, $(string(A))))
end
function latexprint(A, name)
    for i in CartesianIndices(A)
        println(name, "_{", join(Tuple(i),','), "} = ", A[i])
    end
end

```

e.g.

```julia
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> @latexprint(A)
A_{1,1} = 1
A_{2,1} = 3
A_{1,2} = 2
A_{2,2} = 4

```
