# Rending math to HTML in R using katex

This vignette demonstrates some example math rendered server-side in R using the katex package. Refer to the upstream katex support table for the full list of supported tex functions.

Example equations from: https://www.intmath.com/cg5/katex-mathjax-comparison.php

### Equation 1

    tex1 <- "\\frac{1}{\\Bigl(\\sqrt{\\phi \\sqrt{5}}-\\phi\\Bigr) e^{\\frac25 \\pi}} \\equiv 1+\\frac{e^{-2\\pi}} {1+\\frac{e^{-4\\pi}} {1+\\frac{e^{-6\\pi}} {1+\\frac{e^{-8\\pi}} {1+\\cdots} } } }"
    katex_html(tex1, include_css = TRUE)

1(ϕ 5−ϕ)e 2 5 π≡1+e−2 π 1+e−4 π 1+e−6 π 1+e−8 π 1+⋯\frac{1}{\Bigl(\sqrt{\phi \sqrt{5}}-\phi\Bigr) e^{\frac25 \pi}} \equiv 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}} {1+\frac{e^{-8\pi}} {1+\cdots} } } }(ϕ 5
​
​−ϕ)e 5 2​π 1​≡1+1+1+1+1+⋯e−8 π​e−6 π​e−4 π​e−2 π​

### Equation 2

    tex2 <- "\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)"
    katex_html(tex2)

(∑k=1 n a k b k)2≤(∑k=1 n a k 2)(∑k=1 n b k 2)\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)(k=1∑n​a k​b k​)2≤(k=1∑n​a k 2​)(k=1∑n​b k 2​)

### Equation 3

    tex3 <- "1 +  \\frac{q^2}{(1-q)}+\\frac{q^6}{(1-q)(1-q^2)}+\\cdots = \\prod_{j=0}^{\\infty}\\frac{1}{(1-q^{5j+2})(1-q^{5j+3})}, \\text{ for }\\lvert q\\rvert < 1."
    katex_html(tex3)

1+q 2(1−q)+q 6(1−q)(1−q 2)+⋯=∏j=0∞1(1−q 5 j+2)(1−q 5 j+3), for ∣q∣<1. 1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots = \prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})}, \text{ for }\lvert q\rvert < 1. 1+(1−q)q 2​+(1−q)(1−q 2)q 6​+⋯=j=0∏∞​(1−q 5 j+2)(1−q 5 j+3)1​, for ∣q∣<1.

### Equation 4

    tex4 <- "\\int u \\frac{dv}{dx}\\,dx=uv-\\int \\frac{du}{dx}v\\,dx"
    katex_html(tex4)

∫u d v d x d x=u v−∫d u d x v d x\int u \frac{dv}{dx}\,dx=uv-\int \frac{du}{dx}v\,dx∫u dx d v​dx=uv−∫dx du​v dx

### Equation 5

    tex5 <- "S (\\omega)=\\frac{\\alpha g^2}{\\omega^5} \\,e ^{[-0.74\\bigl\\{\\frac{\\omega U_\\omega 19.5}{g}\\bigr\\}^{-4}]}"
    katex_html(tex5)

S(ω)=α g 2 ω 5 e[−0.74{ω U ω 19.5 g}−4]S (\omega)=\frac{\alpha g^2}{\omega^5} \,e ^{[-0.74\bigl\{\frac{\omega U_\omega 19.5}{g}\bigr\}^{-4}]}S(ω)=ω 5 α g 2​e[−0.74{g ω U ω​19.5​}−4]

### Equation 6

    tex6 <- "f(n) = \\begin{cases} \\frac{n}{2}, & \\text{if } n\\text{ is even} \\\\ 3n+1, & \\text{if } n\\text{ is odd} \\end{cases}"
    katex_html(tex6)

f(n)={n 2, if n is even 3 n+1, if n is odd f(n) = \begin{cases} \frac{n}{2}, & \text{if } n\text{ is even} \\ 3n+1, & \text{if } n\text{ is odd} \end{cases}f(n)={2 n​,3n+1,​if n is even if n is odd​

### Equation 7

    tex7 <- "\\begin{aligned}
    \\dot{x} & = \\sigma(y-x) \\\\
    \\dot{y} & = \\rho x - y - xz \\\\
    \\dot{z} & = -\\beta z + xy
    \\end{aligned}"
    katex_html(tex7)

x˙=σ(y−x)y˙=ρ x−y−x z z˙=−β z+x y\begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned}x˙y˙​z˙​=σ(y−x)=ρx−y−x z=−β z+x y​

### Equation 8

    tex8 <- "\\begin{pmatrix}
    a_{11} & a_{12} & a_{13}\\\\
    a_{21} & a_{22} & a_{23}\\\\
    a_{31} & a_{32} & a_{33}
    \\end{pmatrix}"
    katex_html(tex8)

(a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33)\begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{pmatrix}
​a 11​a 21​a 31​​a 12​a 22​a 32​​a 13​a 23​a 33​​
​