# Customized printing for Struct within Expr

**URL:** <https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [September 11, 2019, 2:55pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651 "2019-09-11T14:55:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 11, 2019, 2:55pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/1 "2019-09-11T14:55:12Z")

</div>

For [Grassmann.jl](https://github.com/chakravala/Grassmann.jl), I would like to customize the printing similar to the `Complex` numbers. Consider:

```nohighlight
julia> using Grassmann

julia> @basis tangent(ℝ^2,2,2) # 2D Grade, 2nd Order, 2 Variables
(⟨++₁₂⟩, v, v₁, v₂, ∂₁, ∂₂, v₁₂, ∂₁v₁, ∂₂v₁, ∂₁v₂, ∂₂v₂, ∂₁₂, ∂₁v₁₂, ∂₂v₁₂, ∂₁₂v₁, ∂₁₂v₂, ∂₁₂v₁₂)

julia> ∇ = ∂1v1 + ∂2v2
0v₁₂ + 1∂₁v₁ + 0∂₂v₁ + 0∂₁v₂ + 1∂₂v₂ + 0∂₁₂

julia> Expr(:call,:/,1,∇⋅∇)
:(1 / 0.0v₁ + 0.0v₂ + 1∂₁∂₁ + 1∂₂∂₂)

```

The problem is that when I embed my number in an `Expr` there is ambiguity due to missing parenthesis.

```nohighlight
julia> z = 1+im
1 + 1im

julia> Expr(:call,:/,1,z)
:(1 / (1 + 1im))

```

For `Complex` numbers this is resolved by adding extra `(...)` parenthesis around the printed value.

How can I customize the printing so that my numbers are also parenthesized in an `Expr` like this?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 11, 2019, 4:05pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/2 "2019-09-11T16:05:48Z")

</div>

You can have a look at how it’s defined for `Base.Complex` with `@edit show(stdin, 1.0im)`. It looks like you just need to overload `Base.show_unquoted`. (In the lines below)

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 11, 2019, 4:10pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/3 "2019-09-11T16:10:15Z")

</div>

```nohighlight
julia> Base.show_unquoted(stdout,1+im)
1 + 1im

```

this does not seem to reproduce the print behavior I am after, it should be `(1 + 1im)` instead

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 11, 2019, 4:11pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/4 "2019-09-11T16:11:27Z")

</div>

You need to pass the outer operator precedence as well.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 11, 2019, 4:14pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/5 "2019-09-11T16:14:07Z")

</div>

This works:

```julia
julia> Base.show_unquoted(stdin, 1.0im, 0#=Don't know what this does, but it isn't used by Complex=#, Base.operator_precedence(:*))
(0.0 + 1.0im)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 11, 2019, 4:17pm UTC](https://discourse.julialang.org/t/customized-printing-for-struct-within-expr/28651/6 "2019-09-11T16:17:24Z")

</div>

This new definition does what I need, which will go into the [AbstractTensors.jl](https://github.com/chakravala/AbstractTensors.jl) package

```nohighlight
function Base.show_unquoted(io::IO, z::T, ::Int, prec::Int) where T<:TensorAlgebra
    if T<:TensorMixed && Base.operator_precedence(:+) <= prec
        print(io, "(")
        show(io, z)
        print(io, ")")
    else
        show(io, z)
    end
end

```
