if I have a matrix a = [1 2; 3 4] is there a way to get the string that display() pretty prints like so?
2×2 Matrix{Int64}:
1 2
3 4
I’m wondering because I want to be able to embed a depiction of the matrix in Pluto markdown. I’m also open to another solution to the problem. Thanks.
1 Like
You want repr
with a MIME argument:
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> repr("text/plain", a)
"2×2 Matrix{Int64}:\n 1 2\n 3 4"
4 Likes
Regrettably, the \n’s are returned as composite text, not newlines. Back to the drawing board.
Are you certain? I see
julia> println(repr("text/plain", a))
2×2 Matrix{Int64}:
1 2
3 4
Perhaps you just need to print
the string rather than display
it?
If there’s a problem, consider using replace
to make such substitutions. For example (going in the opposite direction of newline to backslash-n, since the original produced proper newlines for me):
julia> println(replace(repr("text/plain", a), "\n" => raw"\n"))
2×2 Matrix{Int64}:\n 1 2\n 3 4
2 Likes
println recognizes \n as a newline. Your substitution suggestion works… I was hoping there was a more succinct solution in order to interpolate various arrays into pluto markdown in a way that doesn’t end up with just [1 2; 3 4] on a single line.
I don’t understand this. What is the actual issue with my suggestion?