Hi !
At least, since Julia 1.3, still now with Julia 1.6 I did not know how to fix misaligned simple matrices.
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:
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> 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 !
You can use PrettyTables.jl:
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
6 Likes
Thank you that works for me ! But do you think I should report an error on their GitHub ?
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.
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 ?
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.
1 Like
Please check this SO post out, it provides additional information on this topic.
1 Like
@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) :
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> 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 ?
1 Like
@Lecrapouille, please look at PrettyTables.jl usage here.
1 Like
Arf ! My bad I was trying to pass it as kwargs. I fixed my example !
1 Like