Use with @reaction_network from Catalyst.jl.

Latexify.jl has methods for dealing with the ReactionNetwork models generated by Catalyst.jl. More information regarding these can be found here. The latexify end of things are pretty simple: feed a reaction network to latexify() and let it do its magic.

using Catalyst
using Latexify
copy_to_clipboard(true)

hill2(x, v, k) = v*x^2/(k^2 + x^2)
rn = @reaction_network begin
  hill2(y, v_x, k_x), 0 --> x
  p_y, 0 --> y
  (d_x, d_y), (x, y) --> 0
  (r_b, r_u), x ↔ y
end

latexify(rn; form=:ode)

\[\begin{align} \frac{dx}{dt} &= \frac{v_{x} \cdot y^{2}}{k_{x}^{2} + y^{2}} - d_{x} \cdot x - r_{b} \cdot x + r_{u} \cdot y \\ \frac{dy}{dt} &= p_{y} - d_{y} \cdot y + r_{b} \cdot x - r_{u} \cdot y \\ \end{align}\]

Alternatively, the SDEs generated through the chemical Langevin equations can be displayed by setting the form argument to :sde. Here, the noise in the reaction network is correlated/

latexify(rn; form=:sde)

\[\begin{align} \frac{dx}{dt} &= \sqrt{\frac{v_{x} \cdot y^{2}}{k_{x}^{2} + y^{2}}} - \sqrt{d_{x} \cdot x} - \sqrt{r_{b} \cdot x} + \sqrt{r_{u} \cdot y} \\ \frac{dy}{dt} &= \sqrt{p_{y}} - \sqrt{d_{y} \cdot y} + \sqrt{r_{b} \cdot x} - \sqrt{r_{u} \cdot y} \\ \end{align}\]

Note: On the current version of Latexify, generation of SDEs from reaction networks is broken.

Chemical arrow notation

Catalyst reaction network is all about chemical arrow notation, so why should we not be able to render arrows?

This is the default output (when no value to form is given).

latexify(rn; env=:chemical)

\begin{align} \require{mhchem} \ce{ \varnothing &->[\frac{v{x} \cdot y^{2}}{k{x}^{2} + y^{2}}] x}\\ \ce{ \varnothing &->[p{y}] y}\\ \ce{ x &->[d{x}] \varnothing}\\ \ce{ y &->[d{y}] \varnothing}\\ \ce{ x &<=>[{r{b}}][{r_{u}}] y}\\ \end{align}

The default output is meant to be rendered directly on the screen. This rendering is typically done by MathJax. To get the chemical arrow notation to render automatically, I have included a MathJax command (\require{mhchem}) in the output string. If you want to use the output in a real LaTeX document, you can pass the keyword argument mathjax=false and this extra command will be omitted. In such case you should also add \usepackage{mhchem} to the preamble of your latex document.

Another keyword argument that may be of use is expand=false (defaults to true). This determines whether your functions should be expanded or not. Also, starred=true will change the outputted latex environment from align to align*. This results in the equations not being numbered.

latexify(rn; env=:chemical, expand=false, starred=true)

\[\begin{align*} \require{mhchem} \ce{ \varnothing &->[\mathrm{hill2}\left( y, v_{x}, k_{x} \right)] x}\\ \ce{ \varnothing &->[p_{y}] y}\\ \ce{ x &->[d_{x}] \varnothing}\\ \ce{ y &->[d_{y}] \varnothing}\\ \ce{ x &<=>[{r_{b}}][{r_{u}}] y}\\ \end{align*}\]

Available options

Align

KeywordValuesDefaultDescription
:bracketBoolfalseSurround variables with square brackets.
:cdotBooltrueToggle between using \cdot or just a space to represent multiplication.
:cleanBoolfalseClean out 1* terms. Only useful for Catalyst (then named DiffEqBiological) versions 3.4 or below.
:convert_unicodeBooltrueConvert unicode characters to latex commands, for example α to \alpha
:double_linebreakBoolfalseAdd an extra \\ to the end of the line.
:expandBooltrueExpand functions such as hill(x, v, k, n) to their mathematical expression.
:fmtformat string""Format number output in accordance with Printf. Example: "%.2e"
:imaginary_unitString"\\mathit{i}"The symbol to use to represent the imaginary unit
:indexSymb:bracketRepresent index specification with :bracket (u[1]) or :subscript (u_1).
:noiseBoolfalseDisplay the noise function instead of the deterministic one.
:rowsIterable or symol:allWhich rows to include in the output.
:safescriptsBoolfalsePut scripts inside brackets (a{_b}), sometimes making them uglier, but making alternating scripts possible.
:separatorString" &= "Specify how to separate the left hand side and the right.
:snakecaseBoolfalseTreat underscores as literal underscores (if not, treat first underscore as subscript).
:starredBoolfalseStar the environment to prevent equation numbering.
:symbolicBoolfalseUse symbolic calculations to clean up the expression.

Arrow notation

KeywordValuesDefaultDescription
:double_linebreakBoolfalseAdd an extra \\ to the end of the line.
:expandBooltrueExpand functions such as hill(x, v, k, n) to their mathematical expression.
:mathjaxBooltrueAdd \require{mhchem} to tell MathJax to load the required module.
:starredBoolfalseStar the environment to prevent equation numbering.