April 1, 2019 · math software diy

Easily Add LaTex Math to a Ghost Blog

This is a guide to get LaTex math to work on a Ghost Blog. I chose a fast JavaScript Library developed by Khan Academy called KaTex. To see it in action, check my previous blog post on P-NP. Here are the steps to get it working:

  1. Login to the Ghost Administration Panel, and click the "Code Injection" tab on the sidebar.

  2. Paste in the following code block into the "Blog Header" section and Save:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.css" crossorigin="anonymous">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.js" crossorigin="anonymous"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/contrib/auto-render.min.js" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>
    
  3. Create your LaTex equations in the Ghost editor:

    For the New Koenig Editor

    For inline equations, simply surround the LaTex math with a backslash and parentheses: \( LaTex_HERE \). Example: \(\sum_{i=1}^{n}{n} = \frac{n(n-1)}{2}\). To get a large and centred expression, surround the LaTex math with a backslash and square brackets: \[ LaTex_HERE \]. Example: \[\mathcal L_{\mathcal T}(\vec{\lambda}) = \sum_{(\mathbf{x},\mathbf{s})\in \mathcal T} \log P(\mathbf{s}\mid\mathbf{x}) - \sum_{i=1}^m \frac{\lambda_i^2}{2\sigma^2} \]

    For the Old Markdown Editor

    In Markdown, you will have to escape some of the control characters, as explained here. Usually, all that needs to be escaped are the surrounding backslashes. To get the same expressions as above, surround the LaTex with double backslashes: \\( LaTex_HERE \\) and \\[ LaTex_HERE \\].