hugo-theme-relearn/exampleSite/content/shortcodes/math.en.md

197 lines
7.3 KiB
Markdown
Raw Normal View History

2022-07-02 12:00:23 +00:00
+++
description = "Beautiful math and chemical formulae"
frontmatter = ["customMathJaxURL", "math", "math.force", "mathJaxInitialize"]
options = ["customMathJaxURL", "math", "math.force", "mathJaxInitialize"]
2022-07-02 12:00:23 +00:00
title = "Math"
+++
2024-10-07 06:31:28 +00:00
You can use [pure Markdown](content/markdown#subscript-and-superscript) for writing simple math expressions.
2024-10-06 13:10:39 +00:00
If this is not enough, the `math` shortcode helps you rendering math and chemical formulae using the [MathJax](https://mathjax.org/) library.
2022-07-02 12:00:23 +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
{{< tabs groupid="shortcode-parameter">}}
{{% tab title="passthrough" %}}
2022-07-02 12:00:23 +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)$$
````
2024-04-21 21:47:36 +00:00
{{% /tab %}}
{{% tab title="codefence" %}}
2022-07-02 12:00:23 +00:00
````md
```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 %}}
{{% tab title="shortcode" %}}
2022-07-02 12:00:23 +00:00
````go
{{</* 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 %}}
{{% tab title="partial" %}}
2022-07-02 12:00:23 +00:00
````go
{{ partial "shortcodes/math.html" (dict
"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)$$"
"align" "center"
2022-07-02 12:00:23 +00:00
)}}
````
2022-07-02 12:00:23 +00:00
{{% /tab %}}
{{< /tabs >}}
2024-10-06 13:10:39 +00:00
Passthrough syntax is only available by [further configuration](#passthrough-configuration) and has limited features as it does not provide any of the below parameter. Nevertheless, it is widely available in other Markdown parsers like GitHub and therefore is the recommend syntax for generating portable Markdown.
2022-07-02 12:00:23 +00:00
### Parameter
| Name | Default | Notes |
|-----------------------|------------------|-------------|
| **align** | `center` | The vertical alignment.<br><br>Allowed values are `left`, `center` or `right`. |
2024-02-22 22:17:04 +00:00
| _**&lt;content&gt;**_ | _&lt;empty&gt;_ | Your formulae. |
2022-07-02 12:00:23 +00:00
## Settings
2022-07-02 12:00:23 +00:00
### Providing Initialization Options for the MathJax Library
2022-07-02 12:00:23 +00:00
{{% badge style="cyan" icon="gears" title=" " %}}Option{{% /badge %}} {{% badge style="green" icon="fa-fw fab fa-markdown" title=" " %}}Front Matter{{% /badge %}} The MathJax library is configured with default settings for initialization.
2022-07-02 12:00:23 +00:00
You can overwrite the settings by providing a JSON object in `mathJaxInitialize`. See [MathJax's documentation](https://docs.mathjax.org/en/latest/options/index.html) for all allowed settings.
2022-07-02 12:00:23 +00:00
Keep in mind that initialization settings of your pages front matter overwrite all settings of your configuration options.
2022-07-02 12:00:23 +00:00
#### Example
2024-03-02 10:04:52 +00:00
{{< multiconfig file=hugo >}}
2022-07-02 12:00:23 +00:00
[params]
mathJaxInitialize = "{ \"chtml\": { \"displayAlign\": \"left\" }, { \"tex\": { \"inlineMath\": [[\"\\(\", \"\\)\"], [\"@\", \"@\"]], displayMath: [[\"\\[\", \"\\]\"], [\"@@\", \"@@\"]] }, \"options\": { \"enableMenu\": false }"
2024-03-02 10:04:52 +00:00
{{< /multiconfig >}}
2022-07-02 12:00:23 +00:00
### Loading an External Version of the MathJax Library
2022-07-02 12:00:23 +00:00
{{% badge style="cyan" icon="gears" title=" " %}}Option{{% /badge %}} {{% badge style="green" icon="fa-fw fab fa-markdown" title=" " %}}Front Matter{{% /badge %}} The theme uses the shipped MathJax library by default.
In case you want do use a different version of the MathJax library but don't want to override the shipped version, you can set `customMathJaxURL` to the URL of the external MathJax library.
#### Example
{{< multiconfig file=hugo >}}
[params]
customMathJaxURL = "https://unpkg.com/mathjax/es5/tex-mml-chtml.js"
{{< /multiconfig >}}
### Force Loading of the MathJax Library
{{% badge style="cyan" icon="gears" title=" " %}}Option{{% /badge %}} {{% badge style="green" icon="fa-fw fab fa-markdown" title=" " %}}Front Matter{{% /badge %}} The MathJax library will be loaded if the page contains a `math` shortcode or codefence.
You can force loading the MathJax library if no shortcode or codefence was used by setting `math=true`. If a shortcode or codefence was found, the option has no effect. This must be set in case you are using the [passthrough configuration](#passthrough-configuration) to render math.
Instead of `math=true` you can also use the alias `math.force=true`.
### Passthrough Configuration
2024-10-06 21:38:12 +00:00
You can use your math without enclosing it in a shortcode or codefence by using a [passthrough configuration](https://gohugo.io/content-management/mathematics/#step-1)
{{< multiconfig file=hugo >}}
[markup]
[markup.goldmark]
[markup.goldmark.extensions]
[markup.goldmark.extensions.passthrough]
enable = true
[markup.goldmark.extensions.passthrough.delimiters]
inline = [['\(', '\)'], ['$', '$']]
block = [['\[', '\]'], ['$$', '$$']]
{{< /multiconfig >}}
In this case you have to [force load](#force-loading-of-the-mathjax-library) the MathJax library either in your `hugo.toml` or in your page's front matter as the theme doesn't know if math is used.
[See the example](#passthrough-block-math) on how a passthrough configurations makes using math really easy.
2024-04-21 21:47:36 +00:00
2022-07-02 12:00:23 +00:00
## Examples
### Passthrough Block Math
2024-10-06 13:10:39 +00:00
With [passthrough configuration](#passthrough-configuration) enabled you can just drop your math without enclosing it by shortcodes or codefences but no other [parameters](#parameter) are available.
2024-10-06 13:10:39 +00:00
In this case you have to [force load](#force-loading-of-the-mathjax-library) the MathJax library by setting `math=true` either in your `hugo.toml` or in your page's front matter.
In passthrough default configuration, block math is generated if you use two consecutive `$$` as a delimiter around your formulae.
2022-07-02 12:00:23 +00:00
````md
$$\left|
\begin{array}{cc}
a & b \\
c & d
\end{array}\right|$$
2022-07-02 12:00:23 +00:00
````
$$\left|
\begin{array}{cc}
a & b \\
c & d
\end{array}\right|$$
2022-07-02 12:00:23 +00:00
### Passthrough Inline Math
2022-07-02 12:00:23 +00:00
The same usage restrictions as of the [previous example](#passthrough-block-math) apply here as well.
2022-07-02 12:00:23 +00:00
In passthrough default configuration, inline math is generated if you use a single `$` as a delimiter around your formulae.
````md
Euclid already knew, $\sqrt{2}$ is irrational.
2022-07-02 12:00:23 +00:00
````
Euclid already knew, $\sqrt{2}$ is irrational.
2022-07-02 12:00:23 +00:00
### Codefence Block Math with Right Alignment
2022-07-02 12:00:23 +00:00
If you are using codefences, more [parameter](#parameter) are available. Your formulae still needs to be enclosed by `$` or `$$` as delimiters respectively.
2022-07-02 12:00:23 +00:00
````md
```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 {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)$$
````
### Shortcode Block Math with Right Alignment
2024-04-21 21:47:36 +00:00
You can also use shortcode syntax. Your formulae still needs to be enclosed by `$` or `$$` as delimiters respectively.
2024-04-21 21:47:36 +00:00
````md
{{</* math align="right" */>}}
$$\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 */>}}
````
{{< math align="right" >}}
$$\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 >}}
2022-07-02 22:35:45 +00:00
### Chemical Formulae
2022-07-02 12:00:23 +00:00
The MathJax library can also be used for chemical formulae.
2022-07-02 12:00:23 +00:00
````md
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$
`````
$$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$$