Skip to content

Commit 1e21b5c

Browse files
committed
Complex{Measurement{Num}} printing support
1 parent a547f60 commit 1e21b5c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/show.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@ end
6363
# Representation of complex measurements. Print something that is easy to
6464
# understand and that can be meaningfully copy-pasted into the REPL, at least
6565
# for standard numeric types.
66-
# FIXME: does this handle complex symbolic Measurements correctly?
6766
for mime in (MIME"text/plain", MIME"text/x-tex", MIME"text/x-latex")
6867
function Base.show(io::IO, mtype::mime, measure::Complex{<:Measurement})
6968
r, i = reim(measure)
7069
compact = get(io, :compact, false)
7170
print(io, "(")
7271
show(io, mtype, r)
7372
print(io, ")")
74-
if signbit(i) && !isnan(i)
73+
if !_is_symbolic(i.val) && signbit(i) && !isnan(i)
7574
i = -i
7675
print(io, compact ? "-" : " - ")
7776
else
@@ -83,6 +82,16 @@ for mime in (MIME"text/plain", MIME"text/x-tex", MIME"text/x-latex")
8382
end
8483
end
8584

85+
function Base.show(io::IO, ::MIME"text/latex", measure::Complex{<:Measurement})
86+
print(io, "\$")
87+
Base.show(io, "text/x-latex", measure)
88+
print(io, "\$")
89+
end
90+
91+
function Base.show(io::IO, measure::Complex{<:Measurement})
92+
Base.show(io, "text/plain", measure)
93+
end
94+
8695
# This is adapted from base/show.jl for Complex type.
8796
function Base.alignment(io::IO, measure::Measurement)
8897
m = match(r"^(.*[])(.*)$", sprint(show, measure, context=io, sizehint=0))

test/symbolics.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,41 @@ end
6161
@test repr(mime, x, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "x_val\\pmx_err"
6262
end
6363
end
64+
65+
@variables y_val, y_err
66+
y = measurement(y_val, y_err)
67+
c = complex(x, y)
68+
69+
@test_throws ErrorException repr(c, context=:error_digits=>-4)
70+
@test repr(c) == "(x_val ± x_err) + (y_val ± y_err)im"
71+
@test repr(c, context=:compact=>true) == "(x_val±x_err)+(y_val±y_err)im"
72+
for error_digits in (0, 7)
73+
@test repr(c, context=:error_digits=>error_digits) == "(x_val ± x_err) + (y_val ± y_err)im"
74+
@test repr(c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val±x_err)+(y_val±y_err)im"
75+
end
76+
77+
@test repr("text/plain", c) == "(x_val ± x_err) + (y_val ± y_err)im"
78+
@test repr("text/plain", c, context=:compact=>true) == "(x_val±x_err)+(y_val±y_err)im"
79+
for error_digits in (0, 7)
80+
@test repr("text/plain", c, context=:error_digits=>error_digits) == "(x_val ± x_err) + (y_val ± y_err)im"
81+
@test repr("text/plain", c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val±x_err)+(y_val±y_err)im"
82+
end
83+
84+
@test repr("text/latex", c) == "\$(x_val \\pm x_err) + (y_val \\pm y_err)im\$"
85+
@test repr("text/latex", c, context=:compact=>true) == "\$(x_val\\pmx_err)+(y_val\\pmy_err)im\$"
86+
for error_digits in (0, 7)
87+
@test repr("text/latex", c, context=:error_digits=>error_digits) == "\$(x_val \\pm x_err) + (y_val \\pm y_err)im\$"
88+
@test repr("text/latex", c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "\$(x_val\\pmx_err)+(y_val\\pmy_err)im\$"
89+
end
90+
91+
for mime in ("text/x-tex", "text/x-latex")
92+
@test repr(mime, c) == "(x_val \\pm x_err) + (y_val \\pm y_err)im"
93+
@test repr(mime, c, context=:compact=>true) == "(x_val\\pmx_err)+(y_val\\pmy_err)im"
94+
for error_digits in (0, 7)
95+
@test repr(mime, c, context=:error_digits=>error_digits) == "(x_val \\pm x_err) + (y_val \\pm y_err)im"
96+
@test repr(mime, c, context=IOContext(stdout, :error_digits=>error_digits, :compact=>true)) == "(x_val\\pmx_err)+(y_val\\pmy_err)im"
97+
end
98+
end
6499
end
65100

66101
##### Mathematical Operations

0 commit comments

Comments
 (0)