# Not sure to understand how matrices are displayed

**URL:** <https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371>\
**Category:** General Usage\
**Tags:** matrices, prettytables\
**Created:** [April 15, 2021, 9:32pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371 "2021-04-15T21:32:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [April 15, 2021, 9:32pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/1 "2021-04-15T21:32:23Z")

</div>

Hi !

At least, since Julia 1.3, still now with Julia 1.6 I did not know how to fix misaligned simple matrices.

```julia
julia> A=[45 "m" 6; 4.5 5 't']
2×3 Matrix{Any}:
 45 "m" 6
  4.5 5 't'

```

This is an issue for my package. Here an abstract:

```julia
using Printf

struct MP{T <: Real} <: Real
    λ::T
end

global mpstyle = 2

function Base.show(io::IO, x::MP{T}) where T
    if mpstyle == 0
        show(io, x.λ)
    elseif x.λ == -Inf
        (mpstyle == 1 || mpstyle == 2) ? (@printf io ".") : (@printf io "ε")
    elseif x.λ == zero(T)
        (mpstyle == 1 || mpstyle == 3) ? (@printf io "0") : (@printf io "e")
    elseif x.λ == trunc(x.λ)
        (@printf io "%d" x.λ)
    else
        show(io, x.λ)
    end
end

```

will display:

```julia
julia> mpstyle = 2;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
 . e
 e .

julia> mpstyle = 1;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
  . 0
 0 .

julia> mpstyle = 4;

julia> [MP(-Inf) MP(0); MP(0) MP(-Inf)]
2×2 Matrix{MP}:
 ε e
  e ε

```

How to fix my show() function to fix misalignment ?

Thanks !

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 15, 2021, 10:05pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/2 "2021-04-15T22:05:08Z")

</div>

You can use [PrettyTables.jl](https://ronisbr.github.io/PrettyTables.jl):

```julia
using PrettyTables

showpt(A) = pretty_table(A, tf = tf_borderless, noheader = true)

A = [45 "m" 6; 4.5 5 't']

julia> showpt(A)
   45 m 6
  4.5 5 t

```

---

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [April 19, 2021, 10:26pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/3 "2021-04-19T22:26:42Z")

</div>

Thank you that works for me ! But do you think I should report an error on their GitHub ?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 10:54pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/4 "2021-04-19T22:54:01Z")

</div>

It is beyond my level to answer, but in any event it should not hurt if you report said behaviour.  
Just in case, pointing to the section of the manual that addresses this [pretty-printing matter](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing).

---

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [April 19, 2021, 11:42pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/5 "2021-04-19T23:42:37Z")

</div>

Thanks for the link ! Last question. I tried without success to force using the show() function for Array. The old show is still used. How to make it implicit ?

```julia
const ArrMP{T,N} = Array{MP{T},N}

julia> function Base.show(io::IO, A::ArrMP{T}) where T
    pretty_table(A, tf = tf_borderless, noheader = true)
end

mpstyle = 4;

# Not calling show for array
julia> A = [MP(-Inf) MP(0.0); MP(0.0) MP(-Inf)]
2×2 Matrix{MP{Float64}}:
 ε e
  e ε

julia> show(stdout, A)
  ε e
  e ε

```

Edit: this code is erroneous. See my next comment for correct code.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2021, 3:58am UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/6 "2021-04-20T03:58:47Z")

</div>

Please check this [SO post](https://stackoverflow.com/questions/62868864/julia-how-to-pretty-print-an-array/62871497#62871497) out, it provides additional information on this topic.

---

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [April 20, 2021, 6:03pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/7 "2021-04-20T18:03:49Z")

</div>

@rafael.guerra Hi ! In fact, I failed to copy/paste my last example from my project, that is why I missed the `MIME`.

I finally found my last bug. Thank you, it seems to work correctly now ! I can closed this ticket. Here is the final code (I added ` _` and `#` to debug which function is called) :

```julia
using Printf, PrettyTables

struct MP <: Real
    λ::Float64
end

global mpstyle = 2;

function Base.show(io::IO, x::MP)
    (@printf io "_")
    if x.λ == -Inf
        (mpstyle == 1 || mpstyle == 2) ? (@printf io ".") : (@printf io "ε")
    elseif x.λ == zero(Float64)
        (mpstyle == 1 || mpstyle == 3) ? (@printf io "0") : (@printf io "e")
    elseif x.λ == trunc(x.λ)
        (@printf io "%d" x.λ)
    else
        show(io, x.λ)
    end
end

function Base.show(io::IO, ::MIME"text/plain", x::MP)
    (@printf io "#")
    if x.λ == -Inf
        (mpstyle == 1 || mpstyle == 2) ? (@printf io ".") : (@printf io "ε")
    elseif x.λ == zero(Float64)
        (mpstyle == 1 || mpstyle == 3) ? (@printf io "0") : (@printf io "e")
    elseif x.λ == trunc(x.λ)
        (@printf io "%d" x.λ)
    else
        show(io, x.λ)
    end
end

function Base.show(io::IO, ::MIME"text/plain", A::Array{MP})
    pretty_table(io, A, tf = tf_borderless, noheader = true)
end

```

And the test:

```julia
julia> mpstyle = 2;

julia> A = [MP(-Inf) MP(0.0); MP(5.0) MP(-Inf)]
  _. _e
  _5 _.

julia> mpstyle = 4;

julia> A = [MP(-Inf) MP(5.5); MP(0.0) MP(-Inf)]
  _ε _5.5
  _e _ε

julia> MP(1)
#1

```

For Matrix:

- the REPL is calling the `display()` function
- which calls the `Base.show(io::IO, ::MIME"text/plain", A::Array{MP})`
- which calls the `pretty_table`
- which calls the `Base.show(io::IO, x::MP)`

@rafael.guerra can you confirm that `pretty_table` cannot use the `io::IO` passed as parameter ?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2021, 6:16pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/8 "2021-04-20T18:16:07Z")

</div>

@Lecrapouille, please look at PrettyTables.jl usage [here](https://github.com/ronisbr/PrettyTables.jl/blob/5db8c36491567802167111ecd40be829e5e2fe61/docs/src/man/usage.md).

---

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [April 20, 2021, 6:57pm UTC](https://discourse.julialang.org/t/not-sure-to-understand-how-matrices-are-displayed/59371/9 "2021-04-20T18:57:08Z")

</div>

Arf ! My bad I was trying to pass it as kwargs. I fixed my example !
