| 1 |
--- |
| 2 |
icon: lucide/sigma |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_arithmatex]{: .source-link } |
| 5 |
|
| 6 |
# Arithmatex |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
Arithmatex is an extension that preserves LaTeX math equations during the Markdown conversion process so that they can |
| 11 |
be used with libraries like [MathJax][mathjax]. If you prefer to use something other than MathJax, Arithmatex can output |
| 12 |
a more generic format suitable for other libraries like [KaTeX][katex]. |
| 13 |
|
| 14 |
Arithmatex searches for the patterns `#!tex $...$` and `#!tex \(...\)` for inline math, and `#!tex $$...$$`, |
| 15 |
`#!tex \[...\]`, and `#!tex \begin{}...\end{}` for block math. By default, all formats are enabled, but each format can |
| 16 |
individually be disabled if desired. |
| 17 |
|
| 18 |
The Arithmatex extension can be included in Python Markdown by using the following: |
| 19 |
|
| 20 |
```py3 |
| 21 |
import markdown |
| 22 |
md = markdown.Markdown(extensions=['pymdownx.arithmatex']) |
| 23 |
``` |
| 24 |
|
| 25 |
## Input Format |
| 26 |
|
| 27 |
By default, [`smart_dollar`](#options) mode is enabled for the `#!tex $...$` inline variant. With `smart_dollar` it is |
| 28 |
expected that the opening token (`#!tex $`) is to be followed by a non-whitespace character, and the closing to be |
| 29 |
preceded by a non-white-space character. This is to help avoid false positives when using the dollar sign in |
| 30 |
traditional ways such as: *I have $2.00 and Bob has $10.00*. The previous statement requires no escaping of the |
| 31 |
`#!tex $` character. But when needed, the `#!tex $` character can be escaped using `#!tex \$`. `smart_dollar` can be |
| 32 |
disabled and will capture any `#!tex $...$` whose dollar symbols are not escaped (`#!tex \$`). |
| 33 |
|
| 34 |
```tex title="Inline" |
| 35 |
$p(x|y) = \frac{p(y|x)p(x)}{p(y)}$, \(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\). |
| 36 |
``` |
| 37 |
|
| 38 |
/// html | div.result |
| 39 |
$p(x|y) = \frac{p(y|x)p(x)}{p(y)}$, \(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\). |
| 40 |
/// |
| 41 |
|
| 42 |
|
| 43 |
/// tip | Inline Configuration |
| 44 |
When using MathJax, for best results, it is advised to not use [`generic`](#options) mode, and configure MathJax |
| 45 |
without the `text2jax` extension since MathJax automatically detects Arithmatex's default output. |
| 46 |
|
| 47 |
If using generic mode (for libraries like KaTeX), Arithmatex will convert dollars to the form `#!tex \(...\)` in the |
| 48 |
HTML output. This is because `#!tex $...$` is extremely problematic to scan for, which is why MathJax and KaTeX |
| 49 |
disable `#!tex $...$` by default in their plain text scanners, and why Arithmatex enables `smart_dollar` by default |
| 50 |
when scanning for `#!tex $...$`. |
| 51 |
|
| 52 |
It is advised that if you are outputting in `generic` mode that you do not configure your JavaScript library to look for |
| 53 |
`#!tex $...$` and instead look for `#!tex \(...\)`, and let Arithmatex's handle `#!tex $...$`. |
| 54 |
/// |
| 55 |
|
| 56 |
For block forms, the block must start with the appropriate opening for the block type: `#!tex $$`, `#!tex \[`, and |
| 57 |
`#!tex \begin{}` for the respective search pattern. The block must also end with the proper respective end: `#!tex $$`, |
| 58 |
`#!tex \]`, and `#!tex \end{}`. A block also must contain no empty lines and should be both preceded and followed by an |
| 59 |
empty line. |
| 60 |
|
| 61 |
```tex title="Block" |
| 62 |
$$ |
| 63 |
E(\mathbf{v}, \mathbf{h}) = -\sum_{i,j}w_{ij}v_i h_j - \sum_i b_i v_i - \sum_j c_j h_j |
| 64 |
$$ |
| 65 |
|
| 66 |
\[3 < 4\] |
| 67 |
|
| 68 |
\begin{align} |
| 69 |
p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ |
| 70 |
p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) |
| 71 |
\end{align} |
| 72 |
``` |
| 73 |
|
| 74 |
/// html | div.result |
| 75 |
$$ |
| 76 |
E(\mathbf{v}, \mathbf{h}) = -\sum_{i,j}w_{ij}v_i h_j - \sum_i b_i v_i - \sum_j c_j h_j |
| 77 |
$$ |
| 78 |
|
| 79 |
\[3 < 4\] |
| 80 |
|
| 81 |
\begin{align} |
| 82 |
p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ |
| 83 |
p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) |
| 84 |
\end{align} |
| 85 |
/// |
| 86 |
|
| 87 |
## MathJax Output Format |
| 88 |
|
| 89 |
/// note | Format Dropped in MathJax 3 |
| 90 |
The title is a bit misleading. Yes, MathJax 3 dropped supporting the format as a default recognized form, but you |
| 91 |
can still use the form with MathJax 3, you just have to add a little more configuration. |
| 92 |
|
| 93 |
This used to be a supported format for MathJax 2. It appears this form was dropped in MathJax 3. While it is the |
| 94 |
current default for Arithmatex, it may be relegated to a secondary option in the future. If this output is preferred |
| 95 |
method, setting `generic` to `#!py False` will prevent any surprises in the future if/when the default changes. |
| 96 |
/// |
| 97 |
|
| 98 |
The math equations will be wrapped in a special MathJax script tag and embedded into the HTML. This format does not |
| 99 |
require the `tex2jax.js` extension when setting up MathJax. The tag will be in the form of |
| 100 |
`#!html <script type="math/tex"></script>` for inline and `#!html <script type="math/tex; mode=display"></script>` for |
| 101 |
block. |
| 102 |
|
| 103 |
By default, Arithmatex will also generate a preview span with the class `MathJax_Preview` that can/should be hidden when |
| 104 |
the math content is actually loaded. If you do not want to generate the preview, simply set `preview` to |
| 105 |
`#!py3 False`. |
| 106 |
|
| 107 |
All elements are additionally wrapped in a `#!html <div class="arithmatex"></div>` container by default (`span` if outputting |
| 108 |
inline math). |
| 109 |
|
| 110 |
## Generic Output Format |
| 111 |
|
| 112 |
If [`generic`](#options) is enabled, the extension will escape necessary symbols and normalize all output to be wrapped |
| 113 |
in the more reliable `#!tex \(...\)` for inline math and `#!tex \[...\]` for display math (unless changed via |
| 114 |
`tex_inline_wrap` and `tex_block_wrap` in the [options](#options)). Lastly, everything is inserted into a |
| 115 |
`#!html <span>` or `#!html <div>` for inline and display math respectively. |
| 116 |
|
| 117 |
With the default settings, if in your Markdown you used `#!tex $...$` for inline math, it would be converted to |
| 118 |
`#!html <span class="arithmatex">\(...\)</span>` in the HTML. Blocks would be normalized from `#!tex $$...$$` to |
| 119 |
`#!html <div class="arithmatex">\[...\]</div>`. In the case of `#!tex \begin{}...\end{}`, begins and ends will not be |
| 120 |
replaced, only wrapped: `#!html <div class="arithmatex">\[\begin{}...\end{}\]</div>`. |
| 121 |
|
| 122 |
## Loading MathJax |
| 123 |
|
| 124 |
Arithmatex requires you to provide the MathJax library and provide and configure it to your liking. The recommended way |
| 125 |
of including MathJax is to use the CDN. Latest version at time of writing this is found below. |
| 126 |
|
| 127 |
/// tab | MathJax 3 |
| 128 |
```html |
| 129 |
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script> |
| 130 |
``` |
| 131 |
/// |
| 132 |
|
| 133 |
/// tab | Legacy: MathJax 2 |
| 134 |
```html |
| 135 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script> |
| 136 |
``` |
| 137 |
/// |
| 138 |
|
| 139 |
Generally, it is best to add your own configuration to get exactly what you want. Here we show some simple examples of |
| 140 |
configurations done in JavaScript. We've provided two basic configurations below: one that is configured for |
| 141 |
Arithmatex's [MathJax Output Format](#mathjax-output-format), and one that works with the |
| 142 |
[Generic Output Format](#generic-output-format) by using `tex2jax`. These are a good starting point, so feel free to |
| 143 |
take them and configure them further. Please see the [MathJax][mathjax] site for more info on using MathJax |
| 144 |
extensions/plugins and configuring those extensions/plugins. |
| 145 |
|
| 146 |
As noted below, the non-generic methodology is more complicated in MathJax 3 as they abandoned the approach via wrapping |
| 147 |
math in script tags, but the solution is easily configurable as a simple copy/paste configuration. |
| 148 |
|
| 149 |
/// tab | Generic - MathJax 3 |
| 150 |
```js |
| 151 |
window.MathJax = { |
| 152 |
tex: { |
| 153 |
inlineMath: [ ["\\(","\\)"] ], |
| 154 |
displayMath: [ ["\\[","\\]"] ], |
| 155 |
processEscapes: true, |
| 156 |
processEnvironments: true |
| 157 |
}, |
| 158 |
options: { |
| 159 |
ignoreHtmlClass: ".*", |
| 160 |
processHtmlClass: "arithmatex" |
| 161 |
} |
| 162 |
}; |
| 163 |
``` |
| 164 |
/// |
| 165 |
|
| 166 |
/// tab | Script - MathJax 3 |
| 167 |
```js |
| 168 |
window.MathJax = { |
| 169 |
options: { |
| 170 |
ignoreHtmlClass: 'tex2jax_ignore', |
| 171 |
processHtmlClass: 'tex2jax_process', |
| 172 |
renderActions: { |
| 173 |
find: [10, function (doc) { |
| 174 |
for (const node of document.querySelectorAll('script[type^="math/tex"]')) { |
| 175 |
const display = !!node.type.match(/; *mode=display/); |
| 176 |
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display); |
| 177 |
const text = document.createTextNode(''); |
| 178 |
const sibling = node.previousElementSibling; |
| 179 |
node.parentNode.replaceChild(text, node); |
| 180 |
math.start = {node: text, delim: '', n: 0}; |
| 181 |
math.end = {node: text, delim: '', n: 0}; |
| 182 |
doc.math.push(math); |
| 183 |
if (sibling && sibling.matches('.MathJax_Preview')) { |
| 184 |
sibling.parentNode.removeChild(sibling); |
| 185 |
} |
| 186 |
} |
| 187 |
}, ''] |
| 188 |
} |
| 189 |
} |
| 190 |
}; |
| 191 |
``` |
| 192 |
/// |
| 193 |
|
| 194 |
/// tab | Legacy: Generic - MathJax 2 |
| 195 |
```js |
| 196 |
MathJax.Hub.Config({ |
| 197 |
config: ["MMLorHTML.js"], |
| 198 |
extensions: ["tex2jax.js"], |
| 199 |
jax: ["input/TeX", "output/HTML-CSS", "output/NativeMML"], |
| 200 |
tex2jax: { |
| 201 |
inlineMath: [ ["\\(","\\)"] ], |
| 202 |
displayMath: [ ["\\[","\\]"] ], |
| 203 |
processEscapes: true, |
| 204 |
processEnvironments: true, |
| 205 |
ignoreClass: ".*|", |
| 206 |
processClass: "arithmatex" |
| 207 |
}, |
| 208 |
}); |
| 209 |
``` |
| 210 |
/// |
| 211 |
|
| 212 |
/// tab | Legacy: Script - MathJax 2 |
| 213 |
```js |
| 214 |
MathJax.Hub.Config({ |
| 215 |
config: ["MMLorHTML.js"], |
| 216 |
jax: ["input/TeX", "output/HTML-CSS", "output/NativeMML"], |
| 217 |
extensions: ["MathMenu.js", "MathZoom.js"] |
| 218 |
}); |
| 219 |
``` |
| 220 |
/// |
| 221 |
|
| 222 |
Notice that in our generic configuration, we set up `tex2jax` to only load `arithmatex` classes by excluding all |
| 223 |
elements and adding an exception for the `arithmatex` class. We also don't bother adding `#!tex $...$` and |
| 224 |
`#!tex $$...$$` to the `inlineMath` and `displayMath` options as Arithmatex converts them respectively to |
| 225 |
`#!tex \(...\)` and `#!tex \[...\]` in the HTML output (unless altered in [Options](#options)). But we do have to enable |
| 226 |
`processEnvironments` to properly process `#!tex \begin{}...\end{}` blocks. |
| 227 |
|
| 228 |
## Loading KaTeX |
| 229 |
|
| 230 |
In order to use KaTeX, the generic output format is required. You will need to include the KaTeX library: |
| 231 |
|
| 232 |
```html |
| 233 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.js"></script> |
| 234 |
``` |
| 235 |
|
| 236 |
And the KaTeX CSS: |
| 237 |
|
| 238 |
```html |
| 239 |
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css"> |
| 240 |
``` |
| 241 |
|
| 242 |
Though KaTeX does have its own auto load script, we want to ensure it *only* loads math content from elements with the |
| 243 |
`arithmatex` class. Below is a script that would do just that. Notice we check for and strip wrappers `#!tex \(...\)` |
| 244 |
and `#!tex \[...\]` off the content of the elements and send it through the renderer. We also don't bother adding |
| 245 |
`#!tex $...$` and `#!tex $$...$$` to the `inlineMath` and `displayMath` options as Arithmatex converts them respectively |
| 246 |
to `#!tex \(...\)` and `#!tex \[...\]` in the HTML output (unless altered in [Options](#options)). |
| 247 |
|
| 248 |
```js |
| 249 |
(function () { |
| 250 |
'use strict'; |
| 251 |
|
| 252 |
var katexMath = (function () { |
| 253 |
var maths = document.querySelectorAll('.arithmatex'), |
| 254 |
tex; |
| 255 |
|
| 256 |
for (var i = 0; i < maths.length; i++) { |
| 257 |
tex = maths[i].textContent || maths[i].innerText; |
| 258 |
if (tex.startsWith('\\(') && tex.endsWith('\\)')) { |
| 259 |
katex.render(tex.slice(2, -2), maths[i], {'displayMode': false}); |
| 260 |
} else if (tex.startsWith('\\[') && tex.endsWith('\\]')) { |
| 261 |
katex.render(tex.slice(2, -2), maths[i], {'displayMode': true}); |
| 262 |
} |
| 263 |
} |
| 264 |
}); |
| 265 |
|
| 266 |
(function () { |
| 267 |
var onReady = function onReady(fn) { |
| 268 |
if (document.addEventListener) { |
| 269 |
document.addEventListener("DOMContentLoaded", fn); |
| 270 |
} else { |
| 271 |
document.attachEvent("onreadystatechange", function () { |
| 272 |
if (document.readyState === "interactive") { |
| 273 |
fn(); |
| 274 |
} |
| 275 |
}); |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
onReady(function () { |
| 280 |
if (typeof katex !== "undefined") { |
| 281 |
katexMath(); |
| 282 |
} |
| 283 |
}); |
| 284 |
})(); |
| 285 |
|
| 286 |
}()); |
| 287 |
``` |
| 288 |
|
| 289 |
## Alternative Math Blocks |
| 290 |
|
| 291 |
/// new | New 9.0 |
| 292 |
Added new formats `arithmatex_inline_format` and `arithmatex_fenced_format`. Both are configurable and effectively |
| 293 |
replace all other previously available formats. |
| 294 |
/// |
| 295 |
|
| 296 |
/// warning | Deprecated 9.0 |
| 297 |
The old formatters `inline_mathjax_format`, `inline_mathjax_preview_format`, and `inline_generic_format` have all |
| 298 |
been deprecated and will be removed at some future time. |
| 299 |
|
| 300 |
It is advised to use the new `arithmatex_inline_format` which is configurable and will give the same results as the |
| 301 |
above three. |
| 302 |
/// |
| 303 |
|
| 304 |
[InlineHilite](./inlinehilite.md) and [SuperFences](./superfences.md) both have a feature where you can specify your own |
| 305 |
custom inline and fence blocks respectively. Arithmatex provides a number of compatible formats that can be used in |
| 306 |
conjunction with InlineHilite and SuperFences to create an alternative (and possibly more preferable) syntax for math. |
| 307 |
|
| 308 |
In InlineHilite, by simply providing the following configuration (no need to include `pymdownx.arithmatex` as an |
| 309 |
extension), you can create a familiar inline math format: |
| 310 |
|
| 311 |
```py3 |
| 312 |
import pymdownx.arithmatex as arithmatex |
| 313 |
|
| 314 |
extensions = [ |
| 315 |
"pymdownx.inlinehilite" |
| 316 |
] |
| 317 |
|
| 318 |
extension_config = { |
| 319 |
"pymdownx.inlinehilite": { |
| 320 |
"custom_inline": [ |
| 321 |
{"name": "math", "class": "arithmatex", "format": arithmatex.arithmatex_inline_format(mode="generic")} |
| 322 |
] |
| 323 |
} |
| 324 |
} |
| 325 |
``` |
| 326 |
|
| 327 |
/// tip | YAML Configuration Format |
| 328 |
If you are attempting to configure these options in a YAML based configuration (like in [MkDocs][mkdocs]), please |
| 329 |
see the [FAQ](../faq.md#function-references-in-yaml) to see how to specify function references in YAML. |
| 330 |
/// |
| 331 |
|
| 332 |
```text title="Inline Math" |
| 333 |
`#!math p(x|y) = \frac{p(y|x)p(x)}{p(y)}` |
| 334 |
``` |
| 335 |
|
| 336 |
/// html | div.result |
| 337 |
`#!math p(x|y) = \frac{p(y|x)p(x)}{p(y)}` |
| 338 |
/// |
| 339 |
|
| 340 |
In SuperFences, by providing the following configuration (no need to include `pymdownx.arithmatex` as an extension), you |
| 341 |
can create math fences: |
| 342 |
|
| 343 |
```py3 |
| 344 |
import pymdownx.arithmatex as arithmatex |
| 345 |
|
| 346 |
extensions = [ |
| 347 |
"pymdownx.superfences" |
| 348 |
] |
| 349 |
|
| 350 |
extension_config = { |
| 351 |
"pymdownx.superfences": { |
| 352 |
"custom_fences": [ |
| 353 |
{"name": "math", "class": "arithmatex", "format": arithmatex.arithmatex_fenced_format(mode="generic")} |
| 354 |
] |
| 355 |
} |
| 356 |
} |
| 357 |
``` |
| 358 |
|
| 359 |
````text title="Math Fences" |
| 360 |
```math |
| 361 |
\begin{align} |
| 362 |
p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ |
| 363 |
p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) |
| 364 |
\end{align} |
| 365 |
``` |
| 366 |
```` |
| 367 |
|
| 368 |
/// html | div.result |
| 369 |
```math |
| 370 |
\begin{align} |
| 371 |
p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ |
| 372 |
p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) |
| 373 |
\end{align} |
| 374 |
``` |
| 375 |
/// |
| 376 |
|
| 377 |
Provided formats are found below: |
| 378 |
|
| 379 |
Name | Description |
| 380 |
------------------------------- | ----------- |
| 381 |
`arithmatex_inline_format` | Returns a formatter for creating inline math. Generic mode will wrap math in `#!tex \(...\)`. |
| 382 |
`arithmatex_fenced_format` | Returns a formatter for creating block math. Generic mode will wrap math in `#!tex \[...\]` |
| 383 |
|
| 384 |
Options apply to both formatters: |
| 385 |
|
| 386 |
Name | Description |
| 387 |
--------- | ----------- |
| 388 |
`mode` | Sets to output mode to either `generic` or `mathjax` (script output). `generic` is the default. |
| 389 |
`tag` | Sets the tag type of the parent container. By default, it is `div` of fenced blocks and `span` on inline. |
| 390 |
`preview` | Sets whether a preview is generated for `mathjax` mode only. |
| 391 |
|
| 392 |
## Options |
| 393 |
|
| 394 |
Option | Type | Default | Description |
| 395 |
----------------- | -------- | ------------------------------------- |------------ |
| 396 |
`inline_syntax` | [string] | `#!py3 ['dollar', 'round']` | Syntax to search for: dollar=`#!tex $...$` and round=`#!tex \(...\)`. |
| 397 |
`block_syntax` | [string] | `#!py3 ['dollar', 'square', 'begin']` | Syntax to search for: dollar=`#!tex $...$`, square=`#!tex \[...\]`, and `#!tex \begin{}...\end{}`. |
| 398 |
`generic` | bool | `#!py3 False` | Output in a generic format suitable for non MathJax libraries. |
| 399 |
`tex_inline_wrap` | [string] | `#!py3 ['\\(', '\\)']` | An array containing the opening and closing portion of the `generic` wrap. |
| 400 |
`tex_block_wrap` | [string] | `#!py3 ['\\[', '\\]']` | An array containing the opening and closing portion of the `generic` wrap. |
| 401 |
`smart_dollar` | bool | `#!py3 True` | Enable Arithmatex's smart dollar logic to minimize math detection issues with `#!tex $`. |
| 402 |
`preview` | bool | `#!py3 True` | Insert a preview to show until MathJax finishes loading the equations. |
| 403 |
`block_tag` | string | `#!py3 'div'` | Change the default block tag element wrapper. |
| 404 |
`inline_tag` | string | `#!py3 'span'` | Change the default inline tag element wrapper. |