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 ...
110 {{ if .Param "math" }}
111 {{ partialCached "math.html" . }}
112 {{ end }}
113 ...
114 </head>
115 ```
116
117 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.
118
119 Step 4
120 : 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:
121
122 {{< code-toggle file=content/math-examples.md fm=true >}}
123 title = 'Math examples'
124 date = 2024-01-24T18:09:49-08:00
125 [params]
126 math = true
127 {{< /code-toggle >}}
128
129 Step 5
130 : Include mathematical equations and expressions in Markdown using LaTeX markup.
131
132 ```md {file="content/math-examples.md" copy=true}
133 This is an inline \(a^*=x-b^*\) equation.
134
135 These are block equations:
136
137 \[a^*=x-b^*\]
138
139 \[ a^*=x-b^* \]
140
141 \[
142 a^*=x-b^*
143 \]
144
145 These are also block equations:
146
147 $$a^*=x-b^*$$
148
149 $$ a^*=x-b^* $$
150
151 $$
152 a^*=x-b^*
153 $$
154 ```
155
156 ## Inline delimiters
157
158 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.
159
160 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:
161
162 ```md
163 I will give you \\$2 if you can solve $y = x^2$.
164 ```
165
166 > [!NOTE]
167 > 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][].
168
169 ## Engines
170
171 MathJax and KaTeX are open-source JavaScript display engines.
172
173 > [!NOTE]
174 > 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][].
175 >
176 >See the [inline delimiters](#inline-delimiters) section for details.
177
178 To use KaTeX instead of MathJax, replace the _partial_ template from [Step 2](#step-2) with this:
179
180 ```go-html-template {file="layouts/_partials/math.html" copy=true}
181 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-vlBdW0r3AcZO/HboRPznQNowvexd3fY8qHOWkBi5q7KGgqJ+F48+DceybYmrVbmB" crossorigin="anonymous">
182
183 <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-AtrdNsnxl/75rvBneBVH7DtOvCxSVahR2zWqle1coBKd8DEmLoviqNeJSx64gNAs" crossorigin="anonymous"></script>
184
185 <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-bjyGPfbij8/NDKJhSGZNP/khQVgtHUE5exjm4Ydllo42FwIgYsdLO2lXGmRBf5Mz" crossorigin="anonymous"
186 onload="renderMathInElement(document.body);">
187 </script>
188
189 <script>
190 document.addEventListener("DOMContentLoaded", function() {
191 renderMathInElement(document.body, {
192 delimiters: [
193 {left: '\\[', right: '\\]', display: true}, // block
194 {left: '$$', right: '$$', display: true}, // block
195 {left: '\\(', right: '\\)', display: false}, // inline
196 ],
197 throwOnError : false
198 });
199 });
200 </script>
201 ```
202
203 The delimiters above must match the delimiters in your project configuration.
204
205 ## Chemistry
206
207 Both MathJax and KaTeX provide support for chemical equations. For example:
208
209 ```md
210 $$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$
211 ```
212
213 $$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$
214
215 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][].
216
217 [KaTeX]: https://katex.org/
218 [LaTeX]: https://www.latex-project.org/
219 [MathJax]: https://www.mathjax.org/
220 [`transform.ToMath`]: /functions/transform/tomath/
221 [documentation]: https://katex.org/docs/libs
222 [passthrough extension]: /configuration/markup/#passthrough
223 [this KaTeX limitation]: https://github.com/KaTeX/KaTeX/issues/437