Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 13, 2024
1 parent 453534e commit 00dc1fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function print_coefficient(io::IO, ::MIME"text/latex", coeff::AbstractFloat)
return print(io, s)
end

trim_LaTeX(::MIME, s::AbstractString) = s
trim_LaTeX(_, s::AbstractString) = s

function trim_LaTeX(::MIME"text/latex", s::AbstractString)
i = firstindex(s)
Expand Down Expand Up @@ -52,12 +52,17 @@ end
# Since they add `$$` around it, we need to trim it with `trim_LaTeX`
function print_coefficient(io::IO, mime, coeff)
print(io, "(")
if showable(mime, coeff)
print(io, trim_LaTeX(mime, sprint(show, mime, coeff)))
print_mime(io, mime, coeff)
print(io, ")")
return
end

function print_mime(io::IO, mime, x)
if showable(mime, x)
print(io, trim_LaTeX(mime, sprint(show, mime, x)))
else
show(io, coeff)
show(io, x)
end
return print(io, ")")
end

isnegative(x::Real) = x < 0
Expand All @@ -70,7 +75,7 @@ function _coeff_elt_print(io, mime, c, elt)
print_coefficient(io, mime, c)
_print_dot(io, mime)
__needs_parens(elt) && print(io, '(')
print(io, trim_LaTeX(mime, sprint(show, mime, elt)))
print_mime(io, mime, elt)
__needs_parens(elt) && print(io, ')')
return
end
Expand Down
24 changes: 24 additions & 0 deletions test/constructors.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
struct CustomLaTeXPrint
s::String
end

Base.:-(s::CustomLaTeXPrint) = s
Base.iszero(::CustomLaTeXPrint) = false
function Base.show(io::IO, ::MIME"text/latex", s::CustomLaTeXPrint)
return print(io, s.s)
end

@testset "Algebra and Elements" begin
alph = [:a, :b, :c]
A★ = FreeWords(alph)
Expand Down Expand Up @@ -81,6 +91,10 @@
@test sprint(show, Z) == "2.0·(id) + 1.0·b·c"
@test sprint(show, 2one(RG) - RG(p)) == "2·(id) - 1·b·c"
@test sprint(show, (2 + im) * one(RG) - (3im) * RG(p)) == "(2 + 1im)·(id) + (0 - 3im)·b·c"

@test sprint(print, (2 + im) * one(RG) - (3im) * RG(p)) == "(2 + 1im)·(id) + (0 - 3im)·b·c"
@test sprint(show, 1e-9 * one(RG)) == "1.0e-9·(id)"
@test sprint((io, x) -> show(io, "text/latex", x), 1e-9 * one(RG)) == "\$\$ 1.0 \\cdot 10^{-9} \\cdot (id) \$\$"

@test LinearAlgebra.norm(a, 1) == 3

Expand All @@ -93,4 +107,14 @@
@test deepcopy(a) !== a
@test coeffs(deepcopy(a)) !== coeffs(a)
@test parent(deepcopy(a)) === parent(a)

latex = CustomLaTeXPrint(" \$\$ \\[\\(α_β∀ \\) \\]\t \$\$")
@test sprint((io, x) -> show(io, "text/latex", x),
SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)) ==
"\$\$ (α_β∀) \\cdot b·c \$\$"
# Test that the check for `\\)` handles unicode well
latex = CustomLaTeXPrint("\\(β∀")
@test sprint((io, x) -> show(io, "text/latex", x),
SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)) ==
"\$\$ (\\(β∀) \\cdot b·c \$\$"
end

0 comments on commit 00dc1fe

Please sign in to comment.