Skip to content

Commit 6473d60

Browse files
Merge pull request #1677 from AayushSabharwal/as/fix-latexify
fix: fix latexification of equations
2 parents 3ec4b9f + 9177273 commit 6473d60

File tree

10 files changed

+32
-38
lines changed

10 files changed

+32
-38
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "7.0.0"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
8+
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"
89
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
910
Bijections = "e2ed5e7c-b2de-5872-ae92-c73ca462fb04"
1011
CommonWorldInvalidations = "f70d9fcc-98c5-4d4a-abd7-e4cdeebd8ca8"
@@ -44,11 +45,10 @@ D3Trees = "e3df1716-f71e-5df9-9e2d-98e193103c45"
4445
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
4546
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
4647
Groebner = "0b43b601-686d-58a3-8a1c-6623616c7cd4"
47-
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
4848
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
49+
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
4950
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
5051
Nemo = "2edaba10-b0f1-5616-af89-8c11ac63239a"
51-
PlutoRunner = "dc6b355a-2368-4481-ae6d-ae0351418d79"
5252
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
5353
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
5454
SymPyPythonCall = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"
@@ -61,13 +61,13 @@ SymbolicsGroebnerExt = "Groebner"
6161
SymbolicsLatexifyExt = ["Latexify", "LaTeXStrings"]
6262
SymbolicsLuxExt = "Lux"
6363
SymbolicsNemoExt = "Nemo"
64-
SymbolicsPlutoRunnerExt = "PlutoRunner"
6564
SymbolicsPreallocationToolsExt = ["PreallocationTools", "ForwardDiff"]
6665
SymbolicsSymPyExt = "SymPy"
6766
SymbolicsSymPyPythonCallExt = "SymPyPythonCall"
6867

6968
[compat]
7069
ADTypes = "1.0"
70+
AbstractPlutoDingetjes = "1.3.2"
7171
ArrayInterface = "7"
7272
BenchmarkTools = "1.0"
7373
Bijections = "0.1, 0.2"

docs/src/manual/arrays.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,6 @@ Symbolics.scalarize(sum(A[:,1]) + sum(A[2,:]))
213213

214214
```@docs
215215
Symbolics.Arr
216-
Symbolics.scalarize
217-
Symbolics.shape
216+
SymbolicUtils.scalarize
217+
SymbolicUtils.shape
218218
```

docs/src/manual/derivatives.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ However, often you may want to define your own derivatives so that way
5656
automatic Jacobian etc. calculations can utilize this information. This can
5757
allow for more succinct versions of the derivatives to be calculated
5858
to scale to larger systems. You can define derivatives for your
59-
function via the dispatch:
59+
function via the macro:
6060

6161
```julia
62-
# `N` arguments are accepted by the relevant method of `my_function`
63-
Symbolics.derivative(::typeof(my_function), args::NTuple{N,Any}, ::Val{i})
62+
@register_derivative my_function(args...) i begin
63+
# ...
64+
end
6465
```
6566

6667
where `i` means that it's the derivative with respect to the `i`th argument. `args` is the
@@ -70,5 +71,7 @@ You should return an `Term` for the derivative of your function.
7071
For example, `sin(t)`'s derivative (by `t`) is given by the following:
7172

7273
```@example derivatives
73-
Symbolics.derivative(::typeof(sin), args::NTuple{1,Any}, ::Val{1}) = cos(args[1])
74+
@register_derivative sin(t) 1 cos(t)
7475
```
76+
77+
For more information see [`@register_derivative`](@ref).

docs/src/manual/expression_manipulation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ Symbolics.evaluate
4545
Symbolics.symbolic_to_float
4646
Symbolics.terms(x)
4747
Symbolics.factors(x)
48-
numerator(x::Union{Num, Symbolics.Symbolic})
49-
denominator(x::Union{Num, Symbolics.Symbolic})
48+
numerator(x::Union{Num, SymbolicUtils.BasicSymbolic})
49+
denominator(x::Union{Num, SymbolicUtils.BasicSymbolic})
5050
```

docs/src/tutorials/perturbation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function solve_newton(eq, x, x₀; abstol=1e-8, maxiters=50)
2626
xₙ = x₀ # numerical value of the initial guess
2727
for i = 1:maxiters
2828
# calculate new guess by numerically evaluating symbolic expression at previous guess
29-
xₙ₊₁ = substitute(x - f / f′, x => xₙ)
29+
xₙ₊₁ = Symbolics.value(substitute(x - f / f′, x => xₙ; fold = Val(true)))
3030
if abs(xₙ₊₁ - xₙ) < abstol
3131
return xₙ₊₁ # converged
3232
else

ext/SymbolicsLatexifyExt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ end
140140
if hide_lhs(value(eq.lhs)) || !(value(eq.lhs) isa Union{Number, AbstractArray, BasicSymbolic})
141141
return value(eq.rhs)
142142
else
143-
return Expr(:(=), wrap(value(eq.lhs)), wrap(value(eq.rhs)))
143+
return Expr(:(=), recipe(eq.lhs), recipe(eq.rhs))
144144
end
145145
end
146146

ext/SymbolicsPlutoRunnerExt.jl

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Symbolics.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,19 @@ const STerm = SymbolicUtils.Term{VartypeT}
8282

8383
export simplify, substitute
8484

85-
warn_load_latexify() = warn_load_latexify(nothing)
86-
warn_load_latexify(_) = nothing
85+
import AbstractPlutoDingetjes: is_inside_pluto
86+
const WARNED_LATEXIFY = Ref(false)
87+
function warn_load_latexify()
88+
is_inside_pluto() || return
89+
WARNED_LATEXIFY[] && return
90+
@warn """
91+
Attempting to print a symbolic expression in a Pluto notebook. Please run \
92+
`import Latexify` to enable pretty-printing of symbolic expressions. This \
93+
warning will only display once.
94+
"""
95+
WARNED_LATEXIFY[] = true
96+
return nothing
97+
end
8798

8899
export Num
89100
import MacroTools: splitdef, combinedef, postwalk, striplines

test/latexify.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ Dy = Differential(y)
4545
@test_reference "latexify_refs/stable_mul_ordering1.txt" latexify(x * y)
4646
@test_reference "latexify_refs/stable_mul_ordering2.txt" latexify(y * x)
4747

48-
@test_throws TypeError latexify(x ~ y + z)
49-
# @test_reference "latexify_refs/equation1.txt" latexify(x ~ y + z)
50-
@test_throws TypeError latexify(x ~ Dx(y + z))
51-
# @test_reference "latexify_refs/equation2.txt" latexify(x ~ Dx(y + z))
48+
@test_reference "latexify_refs/equation1.txt" latexify(x ~ y + z)
49+
@test_reference "latexify_refs/equation2.txt" latexify(x ~ Dx(y + z))
5250
@test_reference "latexify_refs/equation5.txt" latexify(AA^2 + AA + 1 + X₁)
5351

5452
@test_reference "latexify_refs/equation_vec1.txt" latexify([

test/latexify_refs/equation2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
\begin{equation}
2-
x = \frac{\mathrm{d}}{\mathrm{d}x} \left( y + z \right)
2+
x = \frac{\mathrm{d}}{\mathrm{d}x} \cdot \left( y + z \right)
33
\end{equation}

0 commit comments

Comments
 (0)