Hugo KaTeX shortcode

Hugo KaTeX shortcode

KaTeX is a powerful math/LaTeX typesetting. This shortcode provides an easy way to integrate KaTeX in your content.

This article was written for Hugo 0.74.

The (heavy) KaTeX library is used to draw math typesetting. At the time of writing 0.11.1.

The text is rendered on the fly which may cause small page jumps.

Usage

There are no options to speak of, place the KaTeX syntax in the shortcode an off you go.

option   effect   optionaldefault
displayif defined it will be shown as display: block instead of display: inline-blockyes
classadds additional CSS class(es)yes

Example

Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

{{< katex display >}}
x = \begin{cases}
   a &\text{if } b \\
   c &\text{if } d
\end{cases}
{{< /katex >}}

Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

\[ x = \begin{cases} a &\text{if } b \\ c &\text{if } d \end{cases} \] Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Here is some inline example: \(\pi(x)\) and some txt here. Code {{< katex >}}\pi(x){{< /katex >}}

Code

Very similar to the mermaid shortcode. The Javascript and CSS are only loaded on the page when the KaTeX shortcode is used at least once to save resources. Also the same CDN implementation is used as in the mermaid shortcode.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{{ if not (.Page.Scratch.Get "katex") }}
<!-- Include katext only first time -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous"
      onerror="cdnFB(this, '{{ "css/thirdParty/katex.min.css" | relURL }}')">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js" integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"
        onerror="cdnFB(this, '{{ "js/thirdParty/katex.min.js" | relURL }}')"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
        onerror="cdnFB(this, '{{ "js/thirdParty/auto-render.min.js" | relURL }}')"
        onload="renderMathInElement(document.body);"></script>
{{ .Page.Scratch.Set "katex" true }}
{{ end }}

<span class="katex{{ with .Get "class" }} {{ . }}{{ end }}">
  {{ if in .Params "display" }}\[{{ else }}\({{ end -}}
  {{ $.Inner }}
  {{- if in .Params "display" }}\]{{ else }}\){{ end }}
</span>
Noticed an error in this post? Corrections are appreciated.

© Nelis Oostens