# "show" nested struct

**URL:** <https://discourse.julialang.org/t/show-nested-struct/85298>\
**Category:** General Usage\
**Created:** [August 4, 2022, 12:04pm UTC](https://discourse.julialang.org/t/show-nested-struct/85298 "2022-08-04T12:04:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 4, 2022, 12:04pm UTC](https://discourse.julialang.org/t/show-nested-struct/85298/1 "2022-08-04T12:04:24Z")

</div>

How can I use the `show` method for a type that is nested in another struct? For example:

```julia
julia> struct A
           x::Int
       end

julia> Base.show(io::IO, ::MIME"text/plain", a::A) = print(io, ">>A$(a.x)")

julia> struct B
           x::A
       end

julia> a = A(1)
>>A1 # what I expected in displaying A

julia> function Base.show(io::IO, ::MIME"text/plain", b::B) 
           println(io, "B containing:")
           print(b.x) # how can I use the show(::A) method here?
       end

julia> b = B(a)
B containing:
A(1) # not what I expect when displaying A

```

Ok, I found it, just had pass `io` and `mime` around:

```julia
julia> function Base.show(io::IO, mime::MIME"text/plain", b::B) 
           println(io, "B containing:")
           show(io, mime, b.x) 
       end

julia> b = B(a)
B containing:
>>A1

```

If that is not the correct way to do it, let me know 🙂

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 4, 2022, 12:27pm UTC](https://discourse.julialang.org/t/show-nested-struct/85298/2 "2022-08-04T12:27:19Z")

</div>

Now, how to properly indent the nested shows? I found this: [Nested indentation in show](https://discourse.julialang.org/t/nested-indentation-in-show/7191)

But it seems that those keywords do not work anymore.

---

<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:** [August 4, 2022, 12:40pm UTC](https://discourse.julialang.org/t/show-nested-struct/85298/3 "2022-08-04T12:40:51Z")

</div>

> [@lmiq](#):
>
> `print(b.x) # how can I use the show(::A) method here?`

This _does_ call `show(io, A)`, but note that `print` calls the _two_-argument `show` function. You typically define the 3-argument `show` in _addition_ to the 2-argument `show`, if you want a more verbose multi-line pretty-printing in addition to compact output (for use in e.g. REPL output via `display`), as explained in the manual on [Custom pretty printing](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing).

> [@lmiq](#):
>
> But it seems that those keywords do not work anymore.

I’m not sure what you mean — you can still add indentation metadata to `IOContext` in the same way:

```julia
Base.show(io::IO, ::MIME"text/plain", a::A) = print(io, ' '^get(io, :indent, 0), ">>A$(a.x)")
function Base.show(io::IO, mime::MIME"text/plain", b::B)
    indent = get(io, :indent, 0)
    println(io, ' '^indent, "B containing:")
    show(IOContext(io, :indent => indent+4), mime, b.x)
end

```

which gives

```julia
julia> B(A(1))
B containing:
    >>A1

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 4, 2022, 1:11pm UTC](https://discourse.julialang.org/t/show-nested-struct/85298/4 "2022-08-04T13:11:41Z")

</div>

> [@stevengj](#):
>
> to `IOContext` in the same way

Thanks, I was not getting the syntax right from the example in the other post, and the help entry of `IOContext` does not mention indentation, so I was lost.

---

<div class="post-metadata">

**Author:** ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)\
**Post date:** [October 9, 2023, 11:46am UTC](https://discourse.julialang.org/t/show-nested-struct/85298/5 "2023-10-09T11:46:13Z")

</div>

However, how do I handle things like arrays which might be displayed in multiple lines but are returned as one long single line?

```julia
B containing:
    >>A[0.0630552585807721, 0.01634873789978597, 0.5896380066893505, 0.6857614491329537, 0.7584816600668288, 0.8824239555341309, 0.12059965087269808, 0.2734410920898175, 0.021199529301916153, 0.11775015736939831]

```

I want (depending on the length of my console window):

```julia
B containing:
    >>A[0.0630552585807721, 0.01634873789978597, 0.5896380066893505, 0.6857614491329537, 
       0.7584816600668288, 0.8824239555341309, 0.12059965087269808, 0.2734410920898175, 
       0.021199529301916153, 0.11775015736939831]

```
