Skip to content

Commit f157562

Browse files
refactor: warn in print methods inside Pluto
1 parent b39bd47 commit f157562

File tree

9 files changed

+33
-1
lines changed

9 files changed

+33
-1
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
4848
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
4949
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
5050
Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a"
51+
PlutoRunner = "dc6b355a-2368-4481-ae6d-ae0351418d79"
5152
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
5253
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
5354
SymPyPythonCall = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"
@@ -60,6 +61,7 @@ SymbolicsGroebnerExt = "Groebner"
6061
SymbolicsLatexifyExt = ["Latexify", "LaTeXStrings"]
6162
SymbolicsLuxExt = "Lux"
6263
SymbolicsNemoExt = "Nemo"
64+
SymbolicsPlutoRunnerExt = "PlutoRunner"
6365
SymbolicsPreallocationToolsExt = ["PreallocationTools", "ForwardDiff"]
6466
SymbolicsSymPyExt = "SymPy"
6567
SymbolicsSymPyPythonCallExt = "SymPyPythonCall"

ext/SymbolicsPlutoRunnerExt.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module SymbolicsPlutoRunnerExt
2+
3+
import Symbolics
4+
5+
const WARNED_LATEXIFY = Ref(false)
6+
7+
function Symbolics.warn_load_latexify(::Nothing)
8+
Base.get_extension(Symbolics, :SymbolicsLatexifyExt) === nothing || return
9+
WARNED_LATEXIFY[] && return
10+
@warn """
11+
Attempting to print a symbolic expression in a Pluto notebook. Please run \
12+
`import Latexify` to enable pretty-printing of symbolic expressions.
13+
"""
14+
WARNED_LATEXIFY[] = true
15+
return nothing
16+
end
17+
18+
end

src/Symbolics.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ const STerm = SymbolicUtils.Term{VartypeT}
8181

8282
export simplify, substitute
8383

84+
warn_load_latexify() = warn_load_latexify(nothing)
85+
warn_load_latexify(_) = nothing
86+
8487
export Num
8588
import MacroTools: splitdef, combinedef, postwalk, striplines
8689
include("wrapper-types.jl")

src/arrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ maybewrap(T) = has_symwrapper(T) ? wrapper_type(T) : T
5656
# wrapper_type(::Type{<:AbstractVector{T}}) where {T} = Arr{maybewrap(T), 1}
5757

5858
function Base.show(io::IO, arr::Arr)
59+
warn_load_latexify()
5960
x = unwrap(arr)
6061
iscall(x) && print(io, "(")
6162
print(io, unwrap(arr))

src/diff.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ function Base.:^(D::Differential, n::Integer)
8181
return Differential(D.x, D.order * n)
8282
end
8383

84-
Base.show(io::IO, D::Differential) = print(io, "Differential(", D.x, ", ", D.order, ")")
84+
function Base.show(io::IO, D::Differential)
85+
warn_load_latexify()
86+
print(io, "Differential(", D.x, ", ", D.order, ")")
87+
end
8588
Base.nameof(D::Differential) = :Differential
8689

8790
Base.:(==)(D1::Differential, D2::Differential) = isequal(D1.x, D2.x) && isequal(D1.order, D2.order)

src/equations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Base.:(==)(a::Equation, b::Equation) = isequal(a.lhs, b.lhs) && isequal(a.rhs, b
2323
Base.hash(a::Equation, salt::UInt) = hash(a.lhs, hash(a.rhs, salt))
2424

2525
function Base.show(io::IO, eq::Equation)
26+
warn_load_latexify()
2627
if hide_lhs(eq.lhs)
2728
show(io, eq.rhs)
2829
else

src/inequality.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function SymbolicUtils.scalarize(ineq::Inequality)
3232
end
3333

3434
function Base.show(io::IO, ineq::Inequality)
35+
warn_load_latexify()
3536
print(io, ineq.lhs, ineq.relational_op == leq ? "" : "", ineq.rhs)
3637
end
3738

src/num.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Base.:^(z::Complex{Num}, n::Integer) = Base.power_by_squaring(z, n)
189189
Base.:^(::Irrational{:ℯ}, x::Num) = exp(x)
190190

191191
function Base.show(io::IO, z::Complex{<:Num})
192+
warn_load_latexify()
192193
r, i = reim(z)
193194
compact = get(io, :compact, false)
194195
show(io, r)
@@ -206,6 +207,7 @@ Base.isone(x::Num) = SymbolicUtils.fraction_isone(unwrap(x))
206207
import SymbolicUtils: <ₑ, Term, operation, arguments
207208

208209
function Base.show(io::IO, n::Num)
210+
warn_load_latexify()
209211
show_numwrap[] ? print(io, :(Num($(value(n))))) : Base.show(io, value(n))
210212
end
211213

src/variable.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ Base.isequal(a::CallAndWrap, b::BasicSymbolic{VartypeT}) = isequal(a.f, b)
564564
Base.hash(x::CallAndWrap, h::UInt) = hash(x.f, h)
565565

566566
function Base.show(io::IO, caw::CallAndWrap)
567+
warn_load_latexify()
567568
show(io, caw.f)
568569
print(io, "")
569570
end

0 commit comments

Comments
 (0)