1- Base. print (io:: IO , v:: AbstractPolynomialLike ) = show (io, MIME " text/print" (), v)
1+ function Base. show (io:: IO , mime:: MIME"text/latex" ,
2+ p:: Union{AbstractPolynomialLike, RationalPoly} )
3+ print (io, " \$\$ " )
4+ _show (io, mime, p)
5+ print (io, " \$\$ " )
6+ end
27
3- # VARIABLES
4- Base. show (io:: IO , v:: AbstractVariable ) = print_var (io, MIME " text/plain" (), v)
5- Base. show (io:: IO , mime:: MIME"text/plain" , v:: AbstractVariable ) = print_var (io, mime, v)
6- Base. show (io:: IO , mime:: MIME"text/latex" , v:: AbstractVariable ) = print_var (io, mime, v)
7- Base. show (io:: IO , mime:: MIME"text/print" , v:: AbstractVariable ) = print_var (io, mime, v)
8+ # If the MIME is not specified, IJulia thinks that it supports images, ...
9+ # and then use the result of show and tries to interpret it as an svg, ...
10+ # We need the two methods to avoid ambiguity
11+ function Base. show (io:: IO , mime:: MIME"text/plain" ,
12+ p:: Union{AbstractPolynomialLike, RationalPoly} )
13+ _show (io, mime, p)
14+ end
15+ function Base. show (io:: IO , mime:: MIME"text/print" ,
16+ p:: Union{AbstractPolynomialLike, RationalPoly} )
17+ _show (io, mime, p)
18+ end
819
9- function print_var (io:: IO , mime:: MIME , var:: AbstractVariable )
20+ Base. print (io:: IO , p:: Union{AbstractPolynomialLike, RationalPoly} ) = show (io, MIME " text/print" (), p)
21+ Base. show (io:: IO , p:: Union{AbstractPolynomialLike, RationalPoly} ) = show (io, MIME " text/plain" (), p)
22+
23+ # VARIABLES
24+ function _show (io:: IO , mime:: MIME , var:: AbstractVariable )
1025 base, indices = name_base_indices (var)
1126 if isempty (indices)
1227 print (io, base)
@@ -15,7 +30,7 @@ function print_var(io::IO, mime::MIME, var::AbstractVariable)
1530 print_subscript (io, mime, indices)
1631 end
1732end
18- print_var (io:: IO , mime:: MIME"text/print" , var:: AbstractVariable ) = print (io, name (var))
33+ _show (io:: IO , mime:: MIME"text/print" , var:: AbstractVariable ) = print (io, name (var))
1934
2035function print_subscript (io:: IO , :: MIME"text/latex" , index)
2136 print (io, " _{" , join (index, " ," ), " }" )
@@ -32,13 +47,7 @@ const unicode_subscripts = ("₀","₁","₂","₃","₄","₅","₆","₇","₈
3247unicode_subscript (i) = join (unicode_subscripts[d+ 1 ] for d in reverse (digits (i)))
3348
3449# MONOMIALS
35-
36- Base. show (io:: IO , mime:: MIME"text/latex" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
37- Base. show (io:: IO , mime:: MIME"text/plain" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
38- Base. show (io:: IO , mime:: MIME"text/print" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
39- Base. show (io:: IO , m:: AbstractMonomial ) = print_monomial (io, MIME " text/plain" (), m)
40-
41- function print_monomial (io:: IO , mime, m:: AbstractMonomial )
50+ function _show (io:: IO , mime, m:: AbstractMonomial )
4251 if isconstant (m)
4352 print (io, ' 1' )
4453 else
@@ -50,7 +59,7 @@ function print_monomial(io::IO, mime, m::AbstractMonomial)
5059 if mime isa MIME " text/print" && printed_var && i > 0 &&
5160 print (io," *" )
5261 end
53- print_var (io, mime, var)
62+ _show (io, mime, var)
5463 printed_var = true
5564 if ! isone (exp)
5665 print_exponent (io, mime, exp)
@@ -59,7 +68,7 @@ function print_monomial(io::IO, mime, m::AbstractMonomial)
5968 end
6069 end
6170end
62- #
71+
6372print_exponent (io:: IO , :: MIME"text/latex" , exp) = print (io, " ^{" , exp, " }" )
6473print_exponent (io:: IO , :: MIME"text/print" , exp) = print (io, " ^" , exp)
6574function print_exponent (io:: IO , mime, exp)
@@ -70,13 +79,7 @@ const unicode_superscripts = ("⁰","¹","²","³","⁴","⁵","⁶","⁷","⁸"
7079unicode_superscript (i) = unicode_superscripts[i+ 1 ]
7180
7281# TERM
73-
74- Base. show (io:: IO , t:: AbstractTerm ) = print_term (io, MIME " text/plain" (), t)
75- Base. show (io:: IO , mime:: MIME"text/latex" , t:: AbstractTerm ) = print_term (io, mime, t)
76- Base. show (io:: IO , mime:: MIME"text/plain" , t:: AbstractTerm ) = print_term (io, mime, t)
77- Base. show (io:: IO , mime:: MIME"text/print" , t:: AbstractTerm ) = print_term (io, mime, t)
78-
79- function print_term (io:: IO , mime, t:: AbstractTerm )
82+ function _show (io:: IO , mime, t:: AbstractTerm )
8083 if isconstant (t)
8184 print_coefficient (io, coefficient (t))
8285 else
@@ -93,7 +96,7 @@ function print_term(io::IO, mime, t::AbstractTerm)
9396 end
9497 end
9598 if ! iszero (t)
96- show (io, mime, monomial (t))
99+ _show (io, mime, monomial (t))
97100 end
98101 end
99102end
@@ -112,25 +115,19 @@ print_coefficient(io::IO, coeff::Real) = print(io, coeff)
112115print_coefficient (io:: IO , coeff) = print (io, " (" , coeff, " )" )
113116
114117# POLYNOMIAL
115-
116- Base. show (io:: IO , t:: AbstractPolynomial ) = print_poly (io, MIME " text/plain" (), t)
117- Base. show (io:: IO , mime:: MIME"text/plain" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
118- Base. show (io:: IO , mime:: MIME"text/latex" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
119- Base. show (io:: IO , mime:: MIME"text/print" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
120-
121- function print_poly (io:: IO , mime, p:: AbstractPolynomial{T} ) where T
118+ function _show (io:: IO , mime, p:: AbstractPolynomial{T} ) where T
122119 ts = terms (p)
123120 if isempty (ts)
124121 print (io, zero (T))
125122 else
126- print_term (io, mime, first (ts))
123+ _show (io, mime, first (ts))
127124 for t in Iterators. drop (ts, 1 )
128125 if isnegative (coefficient (t))
129126 print (io, " - " )
130- show (io, mime, abs (coefficient (t)) * monomial (t))
127+ _show (io, mime, abs (coefficient (t)) * monomial (t))
131128 else
132129 print (io, " + " )
133- show (io, mime, t)
130+ _show (io, mime, t)
134131 end
135132 end
136133 end
@@ -140,16 +137,10 @@ isnegative(x::Real) = x < 0
140137isnegative (x) = false
141138
142139# RATIONAL POLY
143-
144- Base. show (io:: IO , t:: RationalPoly ) = print_ratpoly (io, MIME " text/plain" (), t)
145- Base. show (io:: IO , mime:: MIME"text/plain" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
146- Base. show (io:: IO , mime:: MIME"text/latex" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
147- Base. show (io:: IO , mime:: MIME"text/print" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
148-
149- function print_ratpoly (io:: IO , mime, p:: RationalPoly )
150- print (io, " (" )
151- show (io, mime, p. num)
152- print (io, " ) / (" )
153- show (io, mime, p. den)
154- print (io, " )" )
140+ function _show (io:: IO , mime, p:: RationalPoly )
141+ print (io, mime isa MIME " text/latex" ? " \\ frac{" : " (" )
142+ _show (io, mime, p. num)
143+ print (io, mime isa MIME " text/latex" ? " }{" : " ) / (" )
144+ _show (io, mime, p. den)
145+ print (io, mime isa MIME " text/latex" ? " }" : " )" )
155146end
0 commit comments