From 00dc1feaba979ba658eebe88c22c349ddb79c458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 13 Jun 2024 12:53:25 +0200 Subject: [PATCH] Fixes --- src/show.jl | 17 +++++++++++------ test/constructors.jl | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/show.jl b/src/show.jl index f093094..a51b0ef 100644 --- a/src/show.jl +++ b/src/show.jl @@ -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) @@ -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 @@ -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 diff --git a/test/constructors.jl b/test/constructors.jl index 2191bc6..51a516c 100644 --- a/test/constructors.jl +++ b/test/constructors.jl @@ -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) @@ -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 @@ -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