Debian Codegraph @

docs/content/en/content-management/mathematics.md

raw
1 ---
2 title: Mathematics in Markdown
3 linkTitle: Mathematics
4 description: Include mathematical equations and expressions in Markdown using LaTeX markup.
5 categories: []
6 keywords: []
7 ---
8
9 ## Overview
10
11 Mathematical equations and expressions written in [LaTeX][] are common in academic and scientific publications. Your browser typically renders this mathematical markup using an open-source JavaScript display engine such as [MathJax][] or [KaTeX][].
12
13 For example, this LaTeX markup:
14
15 ```md
16 \[
17 \begin{aligned}
18 KL(\hat{y} || y) &= \sum_{c=1}^{M}\hat{y}_c \log{\frac{\hat{y}_c}{y_c}} \\
19 JS(\hat{y} || y) &= \frac{1}{2}(KL(y||\frac{y+\hat{y}}{2}) + KL(\hat{y}||\frac{y+\hat{y}}{2}))
20 \end{aligned}
21 \]
22 ```
23
24 Is rendered to:
25
26 \[
27 \begin{aligned}
28 KL(\hat{y} || y) &= \sum_{c=1}^{M}\hat{y}_c \log{\frac{\hat{y}_c}{y_c}} \\
29 JS(\hat{y} || y) &= \frac{1}{2}(KL(y||\frac{y+\hat{y}}{2}) + KL(\hat{y}||\frac{y+\hat{y}}{2}))
30 \end{aligned}
31 \]
32
33 Equations and expressions can be displayed inline with other text, or as standalone blocks. Block presentation is also known as "display" mode.
34
35 Whether an equation or expression appears inline, or as a block, depends on the delimiters that surround the mathematical markup. Delimiters are defined in pairs, where each pair consists of an opening and closing delimiter. The opening and closing delimiters may be the same, or different.
36
37 > [!NOTE]
38 > You can configure Hugo to render mathematical markup on the client side using the MathJax or KaTeX display engine, or you can render the markup with the [`transform.ToMath`][] function while building your project.
39 >
40 > The first approach is described below.
41
42 ## Setup
43
44 Follow these instructions to include mathematical equations and expressions in your Markdown using LaTeX markup.
45
46 Step 1
47 : Enable and configure the Goldmark [passthrough extension][] in your project configuration. The passthrough extension preserves raw Markdown within delimited snippets of text, including the delimiters themselves.
48
49 {{< code-toggle file=hugo copy=true >}}
50 [markup.goldmark.extensions.passthrough]
51 enable = true
52
53 [markup.goldmark.extensions.passthrough.delimiters]
54 block = [['\[', '\]'], ['$$', '$$']]
55 inline = [['\(', '\)']]
56
57 [params]
58 math = true
59 {{< /code-toggle >}}
60
61 The configuration above enables mathematical rendering on every page unless you set the `math` parameter to `false` in front matter. To enable mathematical rendering as needed, set the `math` parameter to `false` in your project configuration, and set the `math` parameter to `true` in front matter. Use this parameter in your _base_ template as shown in [Step 3](#step-3).
62
63 > [!NOTE]
64 > The configuration above precludes the use of the `$...$` delimiter pair for inline equations. Although you can add this delimiter pair to the configuration and JavaScript, you must double-escape the `$` symbol when used outside of math contexts to avoid unintended formatting.
65 >
66 > See the [inline delimiters](#inline-delimiters) section for details.
67
68 To disable passthrough of inline snippets, omit the `inline` key from the configuration:
69
70 {{< code-toggle file=hugo >}}
71 [markup.goldmark.extensions.passthrough.delimiters]
72 block = [['\[', '\]'], ['$$', '$$']]
73 {{< /code-toggle >}}
74
75 You can define your own opening and closing delimiters, provided they match the delimiters that you set in [Step 2](#step-2).
76
77 {{< code-toggle file=hugo >}}
78 [markup.goldmark.extensions.passthrough.delimiters]
79 block = [['@@', '@@']]
80 inline = [['@', '@']]
81 {{< /code-toggle >}}
82
83 Step 2
84 : Create a _partial_ template to load MathJax or KaTeX. The example below loads MathJax, or you can use KaTeX as described in the [engines](#engines) section.
85
86 ```go-html-template {file="layouts/_partials/math.html" copy=true}
87 <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
88
89 <script>
90 MathJax = {
91 tex: {
92 displayMath: [['\\[', '\\]'], ['$$', '$$']], // block
93 inlineMath: [['\\(', '\\)']] // inline
94 },
95 loader:{
96 load: ['ui/safe']
97 },
98 };
99 </script>
100 ```
101
102 The delimiters above must match the delimiters in your project configuration.
103
104 Step 3
105 : Conditionally call the _partial_ template from the _base_ template.
106
107 ```go-html-template {file="layouts/baseof.html"}
108 <head>
109 {{ if .Param "math" }}
110 {{ partialCached "math.html" . }}
111 {{ end }}
112 </head>
113 ```
114
115 The example above loads the _partial_ template if you have set the `math` parameter in front matter to `true`. If you have not set the `math` parameter in front matter, the conditional statement falls back to the `math` parameter in your project configuration.
116
117 Step 4
118 : If you set the `math` parameter to `false` in your project configuration, you must set the `math` parameter to `true` in front matter. For example:
119
120 {{< code-toggle file=content/math-examples.md fm=true >}}
121 title = 'Math examples'
122 date = 2024-01-24T18:09:49-08:00
123 [params]
124 math = true
125 {{< /code-toggle >}}
126
127 Step 5
128 : Include mathematical equations and expressions in Markdown using LaTeX markup.
129
130 ```md {file="content/math-examples.md" copy=true}
131 This is an inline \(a^*=x-b^*\) equation.
132
133 These are block equations:
134
135 \[a^*=x-b^*\]
136
137 \[ a^*=x-b^* \]
138
139 \[
140 a^*=x-b^*
141 \]
142
143 These are also block equations:
144
145 $$a^*=x-b^*$$
146
147 $$ a^*=x-b^* $$
148
149 $$
150 a^*=x-b^*
151 $$
152 ```
153
154 ## Inline delimiters
155
156 The configuration, JavaScript, and examples above use the `\(...\)` delimiter pair for inline equations. The `$...$` delimiter pair is a common alternative, but using it may result in unintended formatting if you use the `$` symbol outside of math contexts.
157
158 If you add the `$...$` delimiter pair to your configuration and JavaScript, you must double-escape the `$` symbol when used outside of math contexts to avoid unintended formatting. For example:
159
160 ```md
161 I will give you \\$2 if you can solve $y = x^2$.
162 ```
163
164 > [!NOTE]
165 > If you use the `$...$` delimiter pair for inline equations, and occasionally use the&nbsp;`$`&nbsp;symbol outside of math contexts, you must use MathJax instead of KaTeX to avoid unintended formatting caused by [this KaTeX limitation][].
166
167 ## Engines
168
169 MathJax and KaTeX are open-source JavaScript display engines.
170
171 > [!NOTE]
172 > If you use the `$...$` delimiter pair for inline equations, and occasionally use the&nbsp;`$`&nbsp;symbol outside of math contexts, you must use MathJax instead of KaTeX to avoid unintended formatting caused by [this KaTeX limitation][].
173 >
174 >See the [inline delimiters](#inline-delimiters) section for details.
175
176 To use KaTeX instead of MathJax, replace the _partial_ template from [Step 2](#step-2) with this:
177
178 ```go-html-template {file="layouts/_partials/math.html" copy=true}
179 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-u1zONI5gPXUx0UKI62c75/zww972y0v2rSK5ZYlVdS6xEuWDeZWUI66v6t1gvlXJ" crossorigin="anonymous">
180
181 <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-ykMNcWQhhTUb0YV9SPpPUFURHZ+tWmubkakGBP+OgNK/UXdO2gtzglWx0Rj9hnO3" crossorigin="anonymous"></script>
182
183 <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-bjyGPfbij8/NDKJhSGZNP/khQVgtHUE5exjm4Ydllo42FwIgYsdLO2lXGmRBf5Mz" crossorigin="anonymous"
184 onload="renderMathInElement(document.body);"></script>
185
186 <script>
187 document.addEventListener("DOMContentLoaded", function() {
188 renderMathInElement(document.body, {
189 delimiters: [
190 {left: '\\[', right: '\\]', display: true}, // block
191 {left: '$$', right: '$$', display: true}, // block
192 {left: '\\(', right: '\\)', display: false}, // inline
193 ],
194 throwOnError : false
195 });
196 });
197 </script>
198 ```
199
200 The delimiters above must match the delimiters in your project configuration.
201
202 ## Chemistry
203
204 Both MathJax and KaTeX provide support for chemical equations. For example:
205
206 ```md
207 $$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$
208 ```
209
210 $$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$
211
212 As shown in [Step 2](#step-2) above, MathJax supports chemical equations without additional configuration. To add chemistry support to KaTeX, enable the mhchem extension as described in the KaTeX [documentation][].
213
214 [KaTeX]: https://katex.org/
215 [LaTeX]: https://www.latex-project.org/
216 [MathJax]: https://www.mathjax.org/
217 [`transform.ToMath`]: /functions/transform/tomath/
218 [documentation]: https://katex.org/docs/libs
219 [passthrough extension]: /configuration/markup/#passthrough
220 [this KaTeX limitation]: https://github.com/KaTeX/KaTeX/issues/437