Skip to content

Commit 1ce3191

Browse files
authored
Fix printing of coefficients in LaTeX (#223)
1 parent bcac36e commit 1ce3191

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/show.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ unicode_superscript(i) = unicode_superscripts[i+1]
8080
# TERM
8181
function _show(io::IO, mime, t::AbstractTerm)
8282
if isconstant(t)
83-
print_coefficient(io, coefficient(t))
83+
print_coefficient(io, mime, coefficient(t))
8484
else
8585
if should_print_coefficient(coefficient(t))
8686
if !should_print_coefficient(-coefficient(t))
8787
print(io, '-')
8888
else
89-
print_coefficient(io, coefficient(t))
89+
print_coefficient(io, mime, coefficient(t))
9090
if !iszero(t)
9191
# Print a multiplication sign between coefficent and monmomial
9292
# depending on the mime type
@@ -110,8 +110,22 @@ print_maybe_multiplication_sign(io::IO, mime) = nothing
110110

111111
should_print_coefficient(x) = true # By default, just print all coefficients
112112
should_print_coefficient(x::Number) = !isone(x) # For numbers, we omit any "one" coefficients
113-
print_coefficient(io::IO, coeff::Real) = print(io, coeff)
114-
print_coefficient(io::IO, coeff) = print(io, "(", coeff, ")")
113+
# `Int`, `Float64` don't support MIME"text/latex".
114+
# We could add a check with `showable` if a `Real` subtype supports it and
115+
# the feature is requested.
116+
print_coefficient(io::IO, mime, coeff::Real) = print(io, coeff)
117+
# JuMP expressions supports LaTeX output so `showable` will return `true`
118+
# for them. It is important for anonymous variables to display properly as well:
119+
# https://github.com/jump-dev/SumOfSquares.jl/issues/256
120+
function print_coefficient(io::IO, mime, coeff)
121+
print(io, "(")
122+
if showable(mime, coeff)
123+
show(io, mime, coeff)
124+
else
125+
show(io, coeff)
126+
end
127+
print(io, ")")
128+
end
115129

116130
# POLYNOMIAL
117131
function _show(io::IO, mime, p::AbstractPolynomial{T}) where T

test/show.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
struct CustomLaTeXPrint end
2+
3+
Base.:-(::CustomLaTeXPrint) = CustomLaTeXPrint()
4+
Base.iszero(::CustomLaTeXPrint) = false
5+
Base.show(io::IO, ::MIME"text/latex", ::CustomLaTeXPrint) = print(io, "a_a")
6+
17
@testset "Show" begin
28
Mod.@polyvar x y z
39
@test sprint(show, (x*y^2 + x + 1 + y)) == "xy² + x + y + 1"
@@ -37,4 +43,8 @@
3743
@test sprint(show, x[10]) == "x₁₀"
3844

3945
@test sprint(print, 2x[1]^2+3x[3]+1+x[4]) == "2*x[1]^2 + 3*x[3] + x[4] + 1"
46+
47+
a = CustomLaTeXPrint()
48+
@test sprint((io, x) -> show(io, MIME"text/latex"(), x), term(a, x[1]^2)) == "\$\$ (a_a)x_{1}^{2} \$\$"
49+
@test sprint((io, x) -> show(io, MIME"text/latex"(), x), polynomial([a, a], [x[1]^2, x[2]])) == "\$\$ (a_a)x_{1}^{2} + (a_a)x_{2} \$\$"
4050
end

0 commit comments

Comments
 (0)