--- icon: lucide/sigma --- [:octicons-file-code-24:][_arithmatex]{: .source-link } # Arithmatex ## Overview Arithmatex is an extension that preserves LaTeX math equations during the Markdown conversion process so that they can be used with libraries like [MathJax][mathjax]. If you prefer to use something other than MathJax, Arithmatex can output a more generic format suitable for other libraries like [KaTeX][katex]. Arithmatex searches for the patterns `#!tex $...$` and `#!tex \(...\)` for inline math, and `#!tex $$...$$`, `#!tex \[...\]`, and `#!tex \begin{}...\end{}` for block math. By default, all formats are enabled, but each format can individually be disabled if desired. The Arithmatex extension can be included in Python Markdown by using the following: ```py3 import markdown md = markdown.Markdown(extensions=['pymdownx.arithmatex']) ``` ## Input Format By default, [`smart_dollar`](#options) mode is enabled for the `#!tex $...$` inline variant. With `smart_dollar` it is expected that the opening token (`#!tex $`) is to be followed by a non-whitespace character, and the closing to be preceded by a non-white-space character. This is to help avoid false positives when using the dollar sign in traditional ways such as: *I have $2.00 and Bob has $10.00*. The previous statement requires no escaping of the `#!tex $` character. But when needed, the `#!tex $` character can be escaped using `#!tex \$`. `smart_dollar` can be disabled and will capture any `#!tex $...$` whose dollar symbols are not escaped (`#!tex \$`). ```tex title="Inline" $p(x|y) = \frac{p(y|x)p(x)}{p(y)}$, \(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\). ``` /// html | div.result $p(x|y) = \frac{p(y|x)p(x)}{p(y)}$, \(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\). /// /// tip | Inline Configuration When using MathJax, for best results, it is advised to not use [`generic`](#options) mode, and configure MathJax without the `text2jax` extension since MathJax automatically detects Arithmatex's default output. If using generic mode (for libraries like KaTeX), Arithmatex will convert dollars to the form `#!tex \(...\)` in the HTML output. This is because `#!tex $...$` is extremely problematic to scan for, which is why MathJax and KaTeX disable `#!tex $...$` by default in their plain text scanners, and why Arithmatex enables `smart_dollar` by default when scanning for `#!tex $...$`. It is advised that if you are outputting in `generic` mode that you do not configure your JavaScript library to look for `#!tex $...$` and instead look for `#!tex \(...\)`, and let Arithmatex's handle `#!tex $...$`. /// For block forms, the block must start with the appropriate opening for the block type: `#!tex $$`, `#!tex \[`, and `#!tex \begin{}` for the respective search pattern. The block must also end with the proper respective end: `#!tex $$`, `#!tex \]`, and `#!tex \end{}`. A block also must contain no empty lines and should be both preceded and followed by an empty line. ```tex title="Block" $$ 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 $$ \[3 < 4\] \begin{align} p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) \end{align} ``` /// html | div.result $$ 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 $$ \[3 < 4\] \begin{align} p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) \end{align} /// ## MathJax Output Format /// note | Format Dropped in MathJax 3 The title is a bit misleading. Yes, MathJax 3 dropped supporting the format as a default recognized form, but you can still use the form with MathJax 3, you just have to add a little more configuration. This used to be a supported format for MathJax 2. It appears this form was dropped in MathJax 3. While it is the current default for Arithmatex, it may be relegated to a secondary option in the future. If this output is preferred method, setting `generic` to `#!py False` will prevent any surprises in the future if/when the default changes. /// The math equations will be wrapped in a special MathJax script tag and embedded into the HTML. This format does not require the `tex2jax.js` extension when setting up MathJax. The tag will be in the form of `#!html ` for inline and `#!html ` for block. By default, Arithmatex will also generate a preview span with the class `MathJax_Preview` that can/should be hidden when the math content is actually loaded. If you do not want to generate the preview, simply set `preview` to `#!py3 False`. All elements are additionally wrapped in a `#!html
` container by default (`span` if outputting inline math). ## Generic Output Format If [`generic`](#options) is enabled, the extension will escape necessary symbols and normalize all output to be wrapped in the more reliable `#!tex \(...\)` for inline math and `#!tex \[...\]` for display math (unless changed via `tex_inline_wrap` and `tex_block_wrap` in the [options](#options)). Lastly, everything is inserted into a `#!html ` or `#!html