2022-07-02 12:00:23 +00:00
+++
description = "Beautiful math and chemical formulae"
title = "Math"
+++
The `math` shortcode generates beautiful formatted math and chemical formulae using the [MathJax ](https://mathjax.org/ ) library.
2022-10-31 14:27:31 +00:00
{{< math align = "center" > }}
2022-07-02 12:00:23 +00:00
$$\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)$$
{{< / math > }}
## Usage
2024-08-13 11:33:22 +00:00
While the examples are using shortcodes with named parameter it is recommended to use Markdown codefences instead. This is because more and more other software supports Markdown codefences (eg. GitHub) and so your markdown becomes more portable.
2022-07-02 12:00:23 +00:00
You are free to also call this shortcode from your own partials.
2024-08-13 11:33:22 +00:00
Math is also usable without enclosing it in a shortcode or Markdown codefence but [requires configuration ](#passthrough-configuration ) by you. In this case no parameter from the below table are available.
2024-04-21 21:47:36 +00:00
2023-07-27 15:02:13 +00:00
{{< tabs groupid = "shortcode-parameter" > }}
2024-08-13 11:33:22 +00:00
{{% tab title="markdown" %}}
2022-07-02 12:00:23 +00:00
````md
2022-10-31 14:27:31 +00:00
```math { align="center" }
2022-07-02 12:00:23 +00:00
$$\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)$$
```
````
{{% /tab %}}
2023-06-05 21:20:37 +00:00
{{% tab title="shortcode" %}}
2022-07-02 12:00:23 +00:00
````go
2022-10-31 14:27:31 +00:00
{{< /* math align="center" */>}}
2022-07-02 12:00:23 +00:00
$$\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)$$
{{< /* /math */>}}
````
{{% /tab %}}
2023-06-05 21:20:37 +00:00
{{% tab title="partial" %}}
2022-07-02 12:00:23 +00:00
````go
{{ partial "shortcodes/math.html" (dict
2023-07-27 14:14:55 +00:00
"page" .
2022-07-02 12:00:23 +00:00
"content" "$$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)$$"
2022-10-31 14:27:31 +00:00
"align" "center"
2022-07-02 12:00:23 +00:00
)}}
2024-04-19 21:37:00 +00:00
````
{{% /tab %}}
{{% tab title="passthrough" %}}
2022-07-02 12:00:23 +00:00
2024-04-19 21:37:00 +00:00
````md
$$\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)$$
2022-07-02 12:00:23 +00:00
````
{{% /tab %}}
{{< / tabs > }}
### Parameter
| Name | Default | Notes |
2023-07-27 14:07:01 +00:00
|-----------------------|------------------|-------------|
2022-07-02 12:00:23 +00:00
| **align** | `center` | Allowed values are `left` , `center` or `right` . |
2024-02-22 22:17:04 +00:00
| _**<content>**_ | _<empty>_ | Your formulae. |
2022-07-02 12:00:23 +00:00
## Configuration
2024-04-19 21:37:00 +00:00
MathJax is configured with default settings but you can customize MathJax's default settings for all of your files through a JSON object in your `hugo.toml` or override these settings per page through your pages frontmatter.
2022-07-02 12:00:23 +00:00
2023-11-29 23:14:22 +00:00
The JSON object of your `hugo.toml` / frontmatter is forwarded into MathJax's configuration object.
2022-07-02 12:00:23 +00:00
See [MathJax documentation ](https://docs.mathjax.org/en/latest/options/index.html ) for all allowed settings.
### Global Configuration File
2024-04-19 21:37:00 +00:00
This example reflects the default configuration also used if you don't define `mathJaxInitialize`
2024-03-02 10:04:52 +00:00
{{< multiconfig file = hugo > }}
2022-07-02 12:00:23 +00:00
[params]
2024-04-19 21:37:00 +00:00
mathJaxInitialize = "{ \"tex\": { \"inlineMath\": [[\"\\(\", \"\\)\"], [\"$\", \"$\"]], displayMath: [[\"\\[\", \"\\]\"], [\"$$\", \"$$\"]] }, \"options\": { \"enableMenu\": false }"
2024-03-02 10:04:52 +00:00
{{< / multiconfig > }}
2022-07-02 12:00:23 +00:00
### Page's Frontmatter
2024-04-19 21:37:00 +00:00
Usually you don't need to redefine the global initialization settings for a single page. But if you do, you have repeat all the values from your global configuration you want to keep for a single page as well.
Eg. If you have redefined the delimiters to something exotic like `@` symbols in your global config, but want to additionally align your math to the left for a specific page, you have to put this to your frontmatter:
2024-03-02 10:04:52 +00:00
{{< multiconfig fm = true > }}
2024-04-19 21:37:00 +00:00
mathJaxInitialize = "{ \"chtml\": { \"displayAlign\": \"left\" }, { \"tex\": { \"inlineMath\": [[\"\\(\", \"\\)\"], [\"@\", \"@\"]], displayMath: [[\"\\[\", \"\\]\"], [\"@@\", \"@@\"]] }, \"options\": { \"enableMenu\": false }"
{{< / multiconfig > }}
### Passthrough Configuration
2024-08-13 11:33:22 +00:00
You can use your math without enclosing it in a shortcode or Markdown codefence by using a [passthrough configuration ](https://gohugo.io/content-management/mathematics/#step-1 ) in your `hugo.toml` :
2024-04-19 21:37:00 +00:00
{{< multiconfig file = hugo > }}
[markup]
[markup.goldmark]
[markup.goldmark.extensions]
[markup.goldmark.extensions.passthrough]
enable = true
[markup.goldmark.extensions.passthrough.delimiters]
2024-04-19 21:46:49 +00:00
inline = [['\(', '\)'], ['$', '$']]
block = [['\[', '\]'], ['$$', '$$']]
2024-04-19 21:37:00 +00:00
{{< / multiconfig > }}
In this case you have to tell the theme that your page contains math by setting this in your page's frontmatter:
{{< multiconfig fm = true > }}
disableMathJax = false
2024-03-02 10:04:52 +00:00
{{< / multiconfig > }}
2022-07-02 12:00:23 +00:00
2024-04-21 21:47:36 +00:00
See the [example ](#passthrough ) on how it makes using math really easy.
2022-07-02 12:00:23 +00:00
## Examples
### Inline Math
````md
Inline math is generated if you use a single `$` as a delimiter around your formulae: {{< /* math */>}}$\sqrt{3}${{</* /math */>}}
````
Inline math is generated if you use a single `$` as a delimiter around your formulae: {{< math > }}$\sqrt{3}${{< / math > }}
2022-11-05 12:00:22 +00:00
### Blocklevel Math with Right Alignment
2022-07-02 12:00:23 +00:00
````md
If you delimit your formulae by two consecutive `$$` it generates a new block.
2022-11-05 12:00:22 +00:00
{{< /* math align="right" */>}}
2022-07-02 12:00:23 +00:00
$$\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)$$
{{< /* /math */>}}
````
If you delimit your formulae by two consecutive `$$` it generates a new block.
2022-11-05 12:00:22 +00:00
{{< math align = "right" > }}
2022-07-02 12:00:23 +00:00
$$\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)$$
{{< / math > }}
2024-08-13 11:33:22 +00:00
### Markdown Codefence
2022-07-02 12:00:23 +00:00
2024-08-13 11:33:22 +00:00
You can also use Markdown codefences.
2022-07-02 12:00:23 +00:00
````md
```math
$$\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)$$
```
````
````math
$$\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)$$
````
2024-04-19 21:37:00 +00:00
### Passthrough
2024-04-21 21:47:36 +00:00
This works for block as well as inline math but is only available if you are using the [passthrough configuration ](#passthrough-configuration ).
2024-08-13 11:33:22 +00:00
With passthrough configuration you can just drop your math without enclosed by shortcodes or Markdown codefences but no settings from the [parameter table ](#parameter ) are available.
2024-04-21 21:47:36 +00:00
2024-04-19 21:37:00 +00:00
````md
$$\left|
\begin{array}{cc}
a & b \\
c & d
\end{array}\right|$$
````
$$\left|
\begin{array}{cc}
a & b \\
c & d
\end{array}\right|$$
2022-07-02 22:35:45 +00:00
### Chemical Formulae
2022-07-02 12:00:23 +00:00
````md
{{< /* math */>}}
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$
{{< /* /math */>}}
`````
{{< math > }}
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$
{{< / math > }}