Skip to content

Commit 8b8b5fe

Browse files
Merge pull request #190 from korsbo/latexify_recipe
Add latexify support.
2 parents 870f80f + 1e83b0c commit 8b8b5fe

File tree

5 files changed

+93
-28
lines changed

5 files changed

+93
-28
lines changed

Project.toml

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
name = "ModelingToolkit"
2-
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
3-
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "0.8.0"
5-
6-
[deps]
7-
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
8-
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
9-
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
10-
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
11-
GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb"
12-
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
13-
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
14-
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
15-
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
16-
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
17-
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
18-
19-
[compat]
20-
julia = "1"
21-
22-
[extras]
23-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
24-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
25-
26-
[targets]
27-
test = ["OrdinaryDiffEq", "Test"]
1+
name = "ModelingToolkit"
2+
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
3+
authors = ["Chris Rackauckas <[email protected]>"]
4+
version = "0.8.0"
5+
6+
[deps]
7+
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
8+
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
9+
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
10+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
11+
GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb"
12+
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
13+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
14+
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
15+
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
16+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
17+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
18+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
19+
20+
[compat]
21+
julia = "1"
22+
Latexify = "0.11, 2"
23+
24+
[extras]
25+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
26+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
27+
28+
[targets]
29+
test = ["OrdinaryDiffEq", "Test"]

src/ModelingToolkit.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export modelingtoolkitize
1010

1111
using DiffEqBase, Distributed
1212
using StaticArrays, LinearAlgebra
13+
using Latexify
1314

1415
using MacroTools
15-
import MacroTools: splitdef, combinedef
16+
import MacroTools: splitdef, combinedef, postwalk
1617
import GeneralizedGenerated
1718
using DocStringExtensions
1819

@@ -90,6 +91,7 @@ include("simplify.jl")
9091
include("utils.jl")
9192
include("direct.jl")
9293
include("domains.jl")
94+
include("latexify_recipes.jl")
9395
include("systems/diffeqs/diffeqsystem.jl")
9496
include("systems/diffeqs/first_order_transform.jl")
9597
include("systems/nonlinear/nonlinear_system.jl")

src/latexify_recipes.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@latexrecipe function f(eqs::Vector{ModelingToolkit.Equation}; iv=:t)
2+
# Set default option values.
3+
env --> :align
4+
5+
# Convert both the left and right hand side to expressions of basic types
6+
# that latexify can deal with.
7+
8+
rhs = getfield.(eqs, :rhs)
9+
rhs = convert.(Expr, rhs)
10+
rhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in rhs]
11+
rhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in rhs]
12+
rhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential && length(x.args[2].args) == 2 ? :($(Symbol(:d, x.args[2]))/($(Symbol(:d, x.args[2].args[2])))) : x, eq) for eq in rhs]
13+
rhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential ? "\\frac{d\\left($(Latexify.latexraw(x.args[2]))\\right)}{d$iv}" : x, eq) for eq in rhs]
14+
15+
lhs = getfield.(eqs, :lhs)
16+
lhs = convert.(Expr, lhs)
17+
lhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in lhs]
18+
lhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in lhs]
19+
lhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential && length(x.args[2].args) == 2 ? :($(Symbol(:d, x.args[2]))/($(Symbol(:d, x.args[2].args[2])))) : x, eq) for eq in lhs]
20+
lhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential ? "\\frac{d\\left($(Latexify.latexraw(x.args[2]))\\right)}{d$iv}" : x, eq) for eq in lhs]
21+
22+
return lhs, rhs
23+
end

test/latexify.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Test
2+
using Latexify
3+
using ModelingToolkit
4+
5+
### Tips for generating latex tests:
6+
### Latexify has an unexported macro:
7+
###
8+
### Latexify.@generate_test
9+
###
10+
### which generates a test using a given latexify function.
11+
### For example:
12+
###
13+
### Latexify.@generate_test latexify([1, 2, 3], [4, 5, 6]; env=:mdtable)
14+
###
15+
### This puts a ready-made test in your clipboard which you can paste into the
16+
### test file.
17+
###
18+
### Just be sure to remove all such macros before you commit a change since it
19+
### will cause issues with Travis.
20+
21+
@parameters t σ ρ β
22+
@variables x(t) y(t) z(t)
23+
@derivatives D'~t
24+
25+
eqs = [D(x) ~ σ*(y-x)*D(x-y)/D(z),
26+
0 ~ σ*x*-z)/10-y,
27+
D(z) ~ x*y - β*z]
28+
29+
30+
@test latexify(eqs) ==
31+
raw"\begin{align}
32+
\frac{dx(t)}{dt} =& \frac{\sigma \cdot \left( \mathrm{y}\left( t \right) - \mathrm{x}\left( t \right) \right) \cdot \frac{d\left(\mathrm{x}\left( t \right) - \mathrm{y}\left( t \right)\right)}{dt}}{\frac{dz(t)}{dt}} \\
33+
0 =& \frac{\sigma \cdot \mathrm{x}\left( t \right) \cdot \left( \rho - \mathrm{z}\left( t \right) \right)}{10} - \mathrm{y}\left( t \right) \\
34+
\frac{dz(t)}{dt} =& \mathrm{x}\left( t \right) \cdot \mathrm{y}\left( t \right) - \beta \cdot \mathrm{z}\left( t \right)
35+
\end{align}
36+
"
37+

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ using ModelingToolkit, Test
99
@testset "Constraints Test" begin include("constraints.jl") end
1010
@testset "PDE Construction Test" begin include("pde.jl") end
1111
@testset "Distributed Test" begin include("distributed.jl") end
12+
#@testset "Latexify recipes Test" begin include("latexify.jl") end

0 commit comments

Comments
 (0)