42914c50e content: Correct module mount example d7b80be5f content: Add a "vendor" glossary entry e1bd65866 content: Miscellaneous edits deb1c4b1c content: Fix formatting 7ff740aae content: Update capitalizeListTitles documentation a9f40164f content: Remove data namespace from functions b3386a13a content: Remove expired content e21f66fa5 content: Clean up deprecated service configurations 39ab42192 content: Change default value of respectDoNotTrack to true 067cbb771 Update partial-decorators.md b419af329 content: Minor edits to collections functions b12dd9366 content: Improve documentation for partial decorators 573b8610f content: Improve descriptions and signatures for collections functions 81befbae8 content: Update version references 684dbcedb Update HUGO_VERSION to 0.155.3 198e5f1df misc: Update README.md 4d20bbc04 content: Correct and improve glob patterns documentation 39d369ddc content: Update disablePathToLower documentation be2da46e9 content: Miscellaneous edits to configuration documentation 19a580828 content: Add Commentix to the list of commercial commenting systems d350725cd theme: Pin npm dependencies and adjust Netlify build commands 9b33bb5fa content: Define canonical output format and default site 84b72792e content: Document glob pattern matching logic 1839dd114 content: Create glob pattern quick reference 30f8f9ca0 content: Revise multi-dimensional content model description b7e36e479 content: Fix formatting b1722ac6b content: Indicate data types returned by the or function 1bbe3a566 content: Fix typo b7e1ac7bb content: Improve example of _redirects rendering 37c71ef80 content: Improve and/or descriptions 0226c14ed content: Updates for v0.155.0 301fa208b content: Change GitHub Pages workflow to support dot files 5d07a679c Update HUGO_VERSION to 0.155.2 adb8e8b2c theme: Pin turbo 9206cb64b Update netlify.toml 84e712633 content: Refine ToMath admonition and provide example a099af3cd content: Warn about conditional KaTeX CSS on list pages c3ca7ac4c misc: Address build warnings 80a9139f4 Update HUGO_VERSION to 0.155.0 b096f6279 content: Restore homebrew on linux instructions 05132aa29 content: Hide snap installation instructions d0e448a14 content: Refactor pages related to image processing d141bc04f content: Clarify default site redirect description 1d6a0bed5 content: Adjust paragraph order fa53534cb content: Clarify usage of quote characters in templates 1d62aa670 content: Miscellaneous edits 579c0678b content: Fix description of disableDefaultLanguageRedirect cd12583ee Update all.md 37b01c320 Update HUGO_VERSION to 0.154.5 66053b6df content: Update version references 3f6fb8737 content: Refresh deployment dependencies to latest versions c2915ee98 theme: Add a mobile menu variant 6457c3f59 Remove trailing whitespace from content/en/configuration/all.md 46c4d117a config: Mention disableDefaultDimensionRedirect 338500de0 Regen docs helper 98b9d25f3 Updates for 0.154.4 release 8306956e2 Update HUGO_VERSION to 0.154.4 af3f62992 Update HUGO_VERSION to 0.154.3 d06de65bd content: Fix typo 0d841a59e content: Fix typo ed6fa87cf content: Fix typo dcf80dbea content: Describe Markdown attribute access in render hook templates 70d3513f1 content: Update version references 7e581c529 Update netlify.toml c6a706943 content: Remove expired pages e64ee7d54 misc: Update workflows bbec5a198 Update HUGO_VERSION to 0.154.1 e8717cb72 content: Fix formatting of glossary entry 0da233817 Add a link to templates.Inner from the partial decorator glossary entry d55f7ad5e Update Inner.md b674dfd48 Some more about templates.Inner ef354e664 Remove some old new-in markers 7d1e407fd content: Misc updates for v0.154.0 43589a017 Update HUGO_VERSION to 0.154.0 46f832b9b content: Change shortcode usage note to an admonition 71ea41250 Adjust sponstors.toml a5dc99838 Update netlify.toml c3edd4d5b Update netlify.toml c1d306d6b Add Zoomment to comments management options cb222be99 Update HUGO_VERSION to 0.153.3 92ee87b4f content: Update CI/CD and version references 170bf3693 content: Note disabled extended check in v0.153.2 and later 34b5ef8b3 Update HUGO_VERSION to 0.153.2 c3c42b171 content: Add Dart Sass installation to SourceHut CI workflow 1768cc2f5 content: Fix links 63ba04562 theme: Modernize sans-serif font stack for cross-platform consistency 287da5c1c content: Update edition comparison and guidance 44b5010ca content: Update min version required for quick start ec4f60cb7 content: Update version references fafa69e7c Update HUGO_VERSION to 0.153.1 37bedd7d3 content: Fix formatting ed9056088 content: Miscellaneous edits fa4d61004 content: Miscellaneous post-release updates for v0.153.0 b99f47449 Misc v0.153.0 related edits 018c3e969 Regen docs.yml be80ec4c7 content: Document new dimensions ba397d5aa content: Miscellaneous updates for v0.153.0 59ca8fa3f Update HUGO_VERSION to 0.153.0 git-subtree-dir: docs git-subtree-split: 42914c50ee338b00d0b1cda2c3af8066cd9ad65c
7.2 KiB
title, description, categories, keywords, params, aliases
| title | description | categories | keywords | params | aliases | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| transform.ToMath | Renders mathematical equations and expressions written in the LaTeX markup language. |
|
|
{{< new-in 0.132.0 />}}
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.
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
Note
By default, Hugo renders mathematical markup to MathML, and does not require any CSS to display the result.
To optimize rendering quality and accessibility, use the
htmlAndMathmloutput option as described below. This approach requires an external stylesheet.
{{ $opts := dict "output" "htmlAndMathml" }}
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" $opts }}
Options
Pass a map of options as the second argument to the transform.ToMath function. The options below are a subset of the KaTeX rendering options.
- displayMode
- (
bool) Whether to render in display mode instead of inline mode. Default isfalse. - errorColor
- (
string) The color of the error messages expressed as an RGB hexadecimal color. Default is#cc0000. - fleqn
- (
bool) Whether to render flush left with a 2em left margin. Default isfalse. - macros
- (
map) A map of macros to be used in the math expression. Default is{}.{{ $macros := dict "\\addBar" "\\bar{#1}" "\\bold" "\\mathbf{#1}" }} {{ $opts := dict "macros" $macros }} {{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }} - minRuleThickness
- (
float) The minimum thickness of the fraction lines inem. Default is0.04. - output
- (
string) Determines the markup language of the output, one ofhtml,mathml, orhtmlAndMathml. Default ismathml.With
htmlandhtmlAndMathmlyou must include the KaTeX style sheet within theheadelement of your base template.<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.25/dist/katex.min.css" integrity="sha384-WcoG4HRXMzYzfCgiyfrySxx90XSl2rxY5mnVY5TwtWE6KLrArNKn0T/mOgNL0Mmi" crossorigin="anonymous" > - strict
- {{< new-in 0.147.6 />}}
- (
string) Controls how KaTeX handles LaTeX features that offer convenience but aren't officially supported, one oferror,ignore, orwarn. Default iserror.error: Throws an error when convenient, unsupported LaTeX features are encountered.ignore: Allows convenient, unsupported LaTeX features without any feedback.warn: {{< new-in 0.147.7 />}} Emits a warning when convenient, unsupported LaTeX features are encountered.
The
newLineInDisplayModeerror code, which flags the use of\\or\newlinein display mode outside an array or tabular environment, is intentionally designed not to throw an error, despite this behavior being questionable. - throwOnError
- (
bool) Whether to throw aParseErrorwhen KaTeX encounters an unsupported command or invalid LaTeX. Default istrue.
Error handling
There are three ways to handle errors:
- Let KaTeX throw an error and fail the build. This is the default behavior.
- Set the
throwOnErroroption tofalseto make KaTeX render the expression as an error instead of throwing an error. See options. - Handle the error in your template.
The example below demonstrates error handing within a template.
Example
Instead of client-side JavaScript rendering of mathematical markup using MathJax or KaTeX, create a passthrough render hook which calls the transform.ToMath function.
- Step 1
- Enable and configure the Goldmark passthrough extension in your site configuration. The passthrough extension preserves raw Markdown within delimited snippets of text, including the delimiters themselves.
{{< code-toggle file=hugo copy=true >}}
[markup.goldmark.extensions.passthrough]
enable = true
[markup.goldmark.extensions.passthrough.delimiters]
block = [['[', ']'], ['$', '$']]
inline = '(', ')'
{{< /code-toggle >}}
Note
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.
- Step 2
- Create a passthrough render hook to capture and render the LaTeX markup.4
{{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }}
{{- with try (transform.ToMath .Inner $opts) }}
{{- with .Err }}
{{- 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 }}
{{- else }}
{{- .Value }}
{{- $.Page.Store.Set "hasMath" true }}
{{- end }}
{{- end -}}
- Step 3
- In your base template, conditionally include the KaTeX CSS within the head element.
<head> {{ $noop := .WordCount }} {{ if .Page.Store.Get "hasMath" }} <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.25/dist/katex.min.css" integrity="sha384-WcoG4HRXMzYzfCgiyfrySxx90XSl2rxY5mnVY5TwtWE6KLrArNKn0T/mOgNL0Mmi" crossorigin="anonymous" > {{ end }} </head>In the above, note the use of a noop statement to force content rendering before we check the value of
hasMathwith theStore.Getmethod.Note
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 calls the
ContentorSummarymethods while ranging through its page collection, the list page will not load the KaTeX CSS.If this affects your site, use this conditional logic instead:
{{ $noop := .WordCount }} {{ if or (.Page.Store.Get "hasMath") .IsNode }} <link rel="stylesheet" href="..."> {{ end }} - Step 4
- Add some mathematical markup to your content, then test.
This is an inline \(a^*=x-b^*\) equation. These are block equations: \[a^*=x-b^*\] $$a^*=x-b^*$$
Chemistry
{{< new-in 0.144.0 />}}
You can also use the transform.ToMath function to render chemical equations, leveraging the \ce and \pu functions from the mhchem package.
$$C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}$$
C_p[\ce{H2O(l)}] = \pu{75.3 J // mol K}