Skip to content

Commit

Permalink
fix latex display of symbolic stoich
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacsas committed Mar 15, 2022
1 parent a6118f5 commit d93897e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ Manifest.toml

# vscode stuff
.vscode
.vscode/*
.vscode/*

LocalPreferences.toml
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Catalyst"
uuid = "479239e8-5488-4da2-87a7-35f2df7eef83"
version = "10.6"
version = "10.6.0"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand All @@ -9,6 +9,7 @@ DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
DiffEqJump = "c894b116-72e5-5b58-be3c-e6d8d4ac2b12"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Expand All @@ -25,6 +26,7 @@ DiffEqBase = "6.54.0"
DiffEqJump = "7.0, 8.0"
DocStringExtensions = "0.8"
Graphs = "1.4"
LaTeXStrings = "v1.3.0"
Latexify = "0.14, 0.15"
MacroTools = "0.5.5"
ModelingToolkit = "6.3, 7.0, 8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Catalyst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Catalyst

using DocStringExtensions
using SparseArrays, DiffEqBase, Reexport, DiffEqJump
using Latexify, Requires
using LaTeXStrings, Latexify, Requires

# ModelingToolkit imports and convenience functions we use
using ModelingToolkit; const MT = ModelingToolkit
Expand Down
25 changes: 22 additions & 3 deletions src/latexify_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ function any_nonrx_subsys(rn::MT.AbstractSystem)
false
end

function make_stoich_str(spec, stoich, subber; kwargs...)
if isequal(stoich,one(stoich))
latexraw(subber(spec); kwargs...)
else
if Symbolics.istree(stoich)
LaTeXString("(") *
latexraw(subber(stoich); kwargs...) *
LaTeXString(")") *
latexraw(subber(spec); kwargs...)
else
latexraw(subber(stoich); kwargs...) * LaTeXString(" ") *
latexraw(subber(spec); kwargs...)
end
end
end

function chemical_arrows(rn::ReactionSystem;
expand = true, double_linebreak=false, mathjax=true, starred=false, kwargs...)

Expand All @@ -79,7 +95,7 @@ function chemical_arrows(rn::ReactionSystem;
mathjax && (str *= "\\require{mhchem}\n")
backwards_reaction = false

subber = ModelingToolkit.substituter([s => MT.operation(s) for s in species(rn)])
subber = ModelingToolkit.substituter([s => value(Symbolics.variable(MT.getname(s))) for s in species(rn)])

for (i, r) in enumerate(rxs)
if backwards_reaction
Expand All @@ -94,7 +110,9 @@ function chemical_arrows(rn::ReactionSystem;
expand && (rate = recursive_clean!(rate))

### Generate formatted string of substrates
substrates = [latexraw("$(substrate[2]== 1 ? "" : "$(substrate[2]) * ") $(substrate[1].f.name)"; kwargs...) for substrate in zip(r.substrates,r.substoich)]
substrates = [make_stoich_str(substrate[1],substrate[2],subber; kwargs...) for substrate in zip(r.substrates,r.substoich)]
@show substrates
#substrates = [latexraw("$(substrate[2]== 1 ? "" : "$(substrate[2]) * ") $(substrate[1].f.name)"; kwargs...) for substrate in zip(r.substrates,r.substoich)]
isempty(substrates) && (substrates = ["\\varnothing"])

str *= join(substrates, " + ")
Expand All @@ -119,7 +137,8 @@ function chemical_arrows(rn::ReactionSystem;
end

### Generate formatted string of products
products = [latexraw("$(product[2]== 1 ? "" : "$(product[2]) * ") $(product[1].f.name)"; kwargs...) for product in zip(r.products,r.prodstoich) ]
products = [make_stoich_str(product[1],product[2],subber; kwargs...) for product in zip(r.products,r.prodstoich)]
#products = [latexraw("$(product[2]== 1 ? "" : "$(product[2]) * ") $(product[1].f.name)"; kwargs...) for product in zip(r.products,r.prodstoich) ]
isempty(products) && (products = ["\\varnothing"])
str *= join(products, " + ")
str *= "}$eol"
Expand Down

0 comments on commit d93897e

Please sign in to comment.