# Getting LaTex formulas to appear in WordPress with Katex

- Link: https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/
- Published: 2022-07-09T15:00:28-07:00
- Author: rich

Well, there are two good [options](https://www.wpbeginner.com/beginners-guide/how-to-write-math-equations-in-wordpress/):

 1. Use the Unicode characters for mathematics in simple one-line formulas and then
    add the [Greek keyboard](https://www.pcworld.idg.com.au/article/696171/how-type-pi-symbol-mac-iphone-other-fun-facts/)
    for things like pi or π by using the Globe icon ???? to switch to Greek and type
    a `p`. On a Mac, you get these with CTRL-CMD-Space or ⌃-⌘-Space and then go to 
    the search bar and look for things like `superscript` to find the square symbol
    so to the formula for the area of a circle is `????=πr²`or if you want to use WordPress
    direction, click on the down arrow in the toolbar and it has superscripts (and 
    subscripts for that matter).
 2. But if you have a more complicated formula like the quadratic equation, you need
    a real equation editor like LaTex. It’s obscure but it is what everyone uses. LaTeX
    itself is a set of macros on top of TeX and is pretty complicated to [learn](https://latex-tutorial.com/tutorials/).
 3. You can use the WP QuickLaTeX plug-in which you can access by adding a Shortcode
    block and then typing the pragma [latexpage]. But sadly this was last updated two
    years ago and you get null images when you run it. This plugin basically looks 
    abandoned
 4. If you only need inline equations use MathJax-LaTeX which supports LaTex or MathML
    and allows direct inline equations without needing a shortcode block just by using
    the shortcode. This one also seems abandoned as it only supports the older MathJax
    1.0 and I had trouble getting it to accept things lie aligned blocks. It seems 
    mainly useful for single equations in the text.
 5. [Katex](https://github.com/KaTeX/KaTeX) (a fast variant of Latex) has a [plugin](https://wordpress.org/plugins/katex/)
    that has a native Gutenberg block editor which is nice and it handles multi-line
    LaTeX well. For backward compatibility, it processes the [ latex ] shortcodes as
    well. But most importantly it allows a dynamic preview of LaTex in the Gutenberg
    block so it is a big winner.
 6. In terms of usages, I would normally use a Gutenberg block for KaTex as that gives
    you a fully new layout, but if you have an inline formula, then using:

    ```wp-block-code
    Here is the Einstein equataion [katex]E=mc^2[/katex]

    Here is the equation if you want dynamic preview then it is [katex display=true]A=2 \pi r^2[/katex]
    ```

Then there is learning LaTex (or KaTex itself):

 1. LaTeX itself (see [Equplus](https://equplus.net/eqninfo/Equation-27.html) for common
    equations) which is basically stuff like this where the LaTex commands are denoted
    by a [backslash](https://en.wikipedia.org/wiki/Backslash) or reverse solidus (which
    is the technical name for a slash which is the Latin name for shilling mark when
    the English shilling mark when this obsolete really long-s was just turned into
    a diagonal line which has the Unicode character U+017F or ſ which definitely doesn’t
    look like much), aka an oblique in the 18th century)
 2. Here are some [quick starts](https://latex-tutorial.com/quick-start/) on writing
    LaTex, the ampersand means this is where all the equations should be aligned and
    the `\` means a line break and then int means an integral, frac is how you get 
    a fraction with the parenthesized things being the numerator and the denominator:

    ```wp-block-code
    [latex]E=mc^2[/latex]
    [latex]G(x) = \frac{1}{3}x^3[/latex]

    # this does not parse correctly with Katex but does with Latex
    [latex]begin{array}{*{20}c} {x = frac{{ - b pm sqrt {b^2 - 4ac} }}{{2a}}} & {{rm{when}}} & {ax^2 + bx + c = 0} \ end{array}[/latex]

    # only katex deals with this correctly
    [katex]
    \begin(aligned)
    C &= 2 \pi r
    A &= \pi r^2
    \end(aligned)
    [/katex]

    # to write this as a single block its a mess because 
    # you cannot use new lines and this works with Quick but not Katex
    [latex]begin{align}{*{20}c} {x = frac{{ - b pm sqrt {b^2 - 4ac} }}{{2a}}} & {{rm{when}}} & {ax^2 + bx + c = 0} \ end{align}[/latex]

    # For Katex
    [katex]E=mc^2[/katex]
    ```

    ```
    E=mc^2
    ```

So if you want the equation to be set out, then create a shortcode block like this
by adding a shortcode if you want to edit it easily, but you can also enter the 
text above directly and it works, note that

![E=mc^2](https://s0.wp.com/latex.php?latex=E%3Dmc%5E2&bg=ffffff&fg=000&s=0&c=20201002)

![G(x) = \frac{1}{3}x^3](https://s0.wp.com/latex.php?latex=G%28x%29+%3D+%5Cfrac%
7B1%7D%7B3%7Dx%5E3&bg=ffffff&fg=000&s=0&c=20201002)

E=mc^2

\begin{aligned} C &= 2 \pi r \\ A &= \pi r^2 \end{aligned}

Here are the same formula with an Insert Shortcode block:

 ⌊E=mc^2⌉ G(x) = \frac{1}{3}x^3

Now, this is a dynamic Katex block:

    ```
    E=mc^2 \\
    G(x) = \frac{1}{3}x^3 \\
    ```

Here is one that uses alignment and begin blocks. Note that if you are doing this
you can’t have bare formulas, they all need to be in blocks and this works great,
so it’s a winner

    ```
    \begin{aligned}
    C &= 2 \pi r \\
    A &= \pi r^2 \\
    G(x) = \frac{1}{3}x^3
    \end{aligned}
    \begin{aligned}
    E = mc^2
    \end{aligned}
    ```

### Share this:

 * [  Mastodon ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=mastodon)
 * [  LinkedIn ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=linkedin)
 * [  Facebook ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=facebook)
 * [  WhatsApp ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=jetpack-whatsapp)
 * [  Email ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?subject=%5BShared%20Post%5D%20Getting%20LaTex%20formulas%20to%20appear%20in%20WordPress%20with%20Katex&body=https%3A%2F%2Ftongfamily.com%2F2022%2F07%2F09%2Fgetting-latex-formulas-to-appear-in-wordpress%2F&share=email)
 * [More](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/#)

 * [  Telegram ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=telegram)
 * [  Print ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/#print?share=print)
 * [  Pinterest ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=pinterest)
 * [  Tumblr ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=tumblr)
 * [  X ](https://tongfamily.com/2022/07/09/getting-latex-formulas-to-appear-in-wordpress/?share=twitter)

### Like this:

Like Loading…
