| 1 |
--- |
| 2 |
title: transform.ToMath |
| 3 |
description: Renders mathematical equations and expressions written in the LaTeX markup language. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: template.HTML |
| 10 |
signatures: ['transform.ToMath INPUT [OPTIONS]'] |
| 11 |
aliases: [/functions/tomath] |
| 12 |
--- |
| 13 |
|
| 14 |
Hugo uses an embedded instance of the [KaTeX][] display engine to render mathematical markup to HTML. You do not need to install the KaTeX display engine. |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }} |
| 18 |
``` |
| 19 |
|
| 20 |
> [!NOTE] |
| 21 |
> By default, Hugo renders mathematical markup to [MathML][], and does not require any CSS to display the result. |
| 22 |
> |
| 23 |
> To optimize rendering quality and accessibility, use the `htmlAndMathml` output option as described below. This approach requires an external stylesheet. |
| 24 |
|
| 25 |
```go-html-template |
| 26 |
{{ $opts := dict "output" "htmlAndMathml" }} |
| 27 |
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" $opts }} |
| 28 |
``` |
| 29 |
|
| 30 |
## Options |
| 31 |
|
| 32 |
The `transform.ToMath` function accepts an options map. These options are a subset of the KaTeX [rendering options][]. |
| 33 |
|
| 34 |
`displayMode` |
| 35 |
: (`bool`) Whether to render in display mode instead of inline mode. Default is `false`. |
| 36 |
|
| 37 |
`errorColor` |
| 38 |
: (`string`) The color of the error messages expressed as an RGB [hexadecimal color][]. Default is `#cc0000`. |
| 39 |
|
| 40 |
`fleqn` |
| 41 |
: (`bool`) Whether to render flush left with a 2em left margin. Default is `false`. |
| 42 |
|
| 43 |
`macros` |
| 44 |
: (`map`) A map of macros to be used in the math expression. Default is `{}`. |
| 45 |
|
| 46 |
```go-html-template |
| 47 |
{{ $macros := dict |
| 48 |
"\\addBar" "\\bar{#1}" |
| 49 |
"\\bold" "\\mathbf{#1}" |
| 50 |
}} |
| 51 |
{{ $opts := dict "macros" $macros }} |
| 52 |
{{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }} |
| 53 |
``` |
| 54 |
|
| 55 |
`minRuleThickness` |
| 56 |
: (`float`) The minimum thickness of the fraction lines in `em`. Default is `0.04`. |
| 57 |
|
| 58 |
`output` |
| 59 |
: (`string`) Determines the markup language of the output, one of `html`, `mathml`, or `htmlAndMathml`. Default is `mathml`. |
| 60 |
|
| 61 |
With `html` and `htmlAndMathml` you must include the KaTeX style sheet within the `head` element of your base template. |
| 62 |
|
| 63 |
```html |
| 64 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-vlBdW0r3AcZO/HboRPznQNowvexd3fY8qHOWkBi5q7KGgqJ+F48+DceybYmrVbmB" crossorigin="anonymous"> |
| 65 |
``` |
| 66 |
|
| 67 |
`strict` |
| 68 |
: {{< new-in 0.147.6 />}} |
| 69 |
: (`string`) Controls how KaTeX handles LaTeX features that offer convenience but aren't officially supported, one of `error`, `ignore`, or `warn`. Default is `error`. |
| 70 |
|
| 71 |
- `error`: Throws an error when convenient, unsupported LaTeX features are encountered. |
| 72 |
- `ignore`: Allows convenient, unsupported LaTeX features without any feedback. |
| 73 |
- `warn`: {{< new-in 0.147.7 />}} Emits a warning when convenient, unsupported LaTeX features are encountered. |
| 74 |
|
| 75 |
The `newLineInDisplayMode` error code, which flags the use of `\\` or `\newline` in display mode outside an array or tabular environment, is intentionally designed not to throw an error, despite this behavior being questionable. |
| 76 |
|
| 77 |
`throwOnError` |
| 78 |
: (`bool`) Whether to throw a `ParseError` when KaTeX encounters an unsupported command or invalid LaTeX. Default is `true`. |
| 79 |
|
| 80 |
## Error handling |
| 81 |
|
| 82 |
There are three ways to handle errors: |
| 83 |
|
| 84 |
1. Let KaTeX throw an error and fail the build. This is the default behavior. |
| 85 |
1. Set the `throwOnError` option to `false` to make KaTeX render the expression as an error instead of throwing an error. See [options](#options). |
| 86 |
1. Handle the error in your template. |
| 87 |
|
| 88 |
The example below demonstrates error handing within a template. |
| 89 |
|
| 90 |
## Example |
| 91 |
|
| 92 |
Instead of client-side JavaScript rendering of mathematical markup using MathJax or KaTeX, create a passthrough render hook which calls the `transform.ToMath` function. |
| 93 |
|
| 94 |
Step 1 |
| 95 |
: 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. |
| 96 |
|
| 97 |
{{< code-toggle file=hugo copy=true >}} |
| 98 |
[markup.goldmark.extensions.passthrough] |
| 99 |
enable = true |
| 100 |
[markup.goldmark.extensions.passthrough.delimiters] |
| 101 |
block = [['\[', '\]'], ['$$', '$$']] |
| 102 |
inline = [['\(', '\)']] |
| 103 |
{{< /code-toggle >}} |
| 104 |
|
| 105 |
> [!NOTE] |
| 106 |
> The configuration above precludes the use of the `$...$` delimiter pair for inline equations. Although you can add this delimiter pair to the configuration, you must double-escape the `$` symbol when used outside of math contexts to avoid unintended formatting. |
| 107 |
|
| 108 |
Step 2 |
| 109 |
: Create a [passthrough render hook][] to capture and render the LaTeX markup.4 |
| 110 |
|
| 111 |
```go-html-template {file="layouts/_markup/render-passthrough.html" copy=true} |
| 112 |
{{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }} |
| 113 |
{{- with try (transform.ToMath .Inner $opts) }} |
| 114 |
{{- with .Err }} |
| 115 |
{{- errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }} |
| 116 |
{{- else }} |
| 117 |
{{- .Value }} |
| 118 |
{{- $.Page.Store.Set "hasMath" true }} |
| 119 |
{{- end }} |
| 120 |
{{- end -}} |
| 121 |
``` |
| 122 |
|
| 123 |
Step 3 |
| 124 |
: In your base template, conditionally include the KaTeX CSS within the head element. |
| 125 |
|
| 126 |
```go-html-template {file="layouts/baseof.html" copy=true} |
| 127 |
<head> |
| 128 |
{{ $noop := .WordCount }} |
| 129 |
{{ if .Page.Store.Get "hasMath" }} |
| 130 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-vlBdW0r3AcZO/HboRPznQNowvexd3fY8qHOWkBi5q7KGgqJ+F48+DceybYmrVbmB" crossorigin="anonymous"> |
| 131 |
{{ end }} |
| 132 |
</head> |
| 133 |
``` |
| 134 |
|
| 135 |
In the above, note the use of a [noop](g) statement to force content rendering before we check the value of `hasMath` with the `Store.Get` method. |
| 136 |
|
| 137 |
> [!NOTE] |
| 138 |
> This conditional approach only identifies math on the current page. Mathematical expressions will not display correctly when one page's content is embedded within another. For example, if a [list page](g) calls the [`Content`][] or [`Summary`][] methods while ranging through its page collection, the list page will not load the KaTeX CSS. |
| 139 |
> |
| 140 |
> If this affects your site, use this conditional logic instead: |
| 141 |
> |
| 142 |
> ```go-html-template {file="layouts/baseof.html" copy=true} |
| 143 |
> {{ $noop := .WordCount }} |
| 144 |
> {{ if or (.Page.Store.Get "hasMath") .IsNode }} |
| 145 |
> <link rel="stylesheet" href="..."> |
| 146 |
> {{ end }} |
| 147 |
> ``` |
| 148 |
|
| 149 |
Step 4 |
| 150 |
: Add some mathematical markup to your content, then test. |
| 151 |
|
| 152 |
```md {file="content/example.md"} |
| 153 |
This is an inline \(a^*=x-b^*\) equation. |
| 154 |
|
| 155 |
These are block equations: |
| 156 |
|
| 157 |
\[a^*=x-b^*\] |
| 158 |
|
| 159 |
$$a^*=x-b^*$$ |
| 160 |
``` |
| 161 |
|
| 162 |
## Chemistry |
| 163 |
|
| 164 |
{{< new-in 0.144.0 />}} |
| 165 |
|
| 166 |
You can also use the `transform.ToMath` function to render chemical equations, leveraging the `\ce` and `\pu` functions from the [`mhchem`][] package. |
| 167 |
|
| 168 |
```md |
| 169 |
$$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$ |
| 170 |
``` |
| 171 |
|
| 172 |
$$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$ |
| 173 |
|
| 174 |
[KaTeX]: https://katex.org/ |
| 175 |
[MathML]: https://developer.mozilla.org/en-US/docs/Web/MathML |
| 176 |
[`Content`]: /methods/page/content/ |
| 177 |
[`Summary`]: /methods/page/summary/ |
| 178 |
[`mhchem`]: https://mhchem.github.io/MathJax-mhchem/ |
| 179 |
[hexadecimal color]: https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color |
| 180 |
[passthrough extension]: /configuration/markup/#passthrough |
| 181 |
[passthrough render hook]: /render-hooks/passthrough/ |
| 182 |
[rendering options]: https://katex.org/docs/options.html |