1ad3c75ad theme: Run ncu -u to update dependencies (#3311) 2942d3753 content: Update version refs e298fdfaf content: Remove ref to old version c1fc876ea content: Improve taxonomic weight example d706a013d content: Document additional functions 5751c37cc content: Standardize string function signatures d65e6fae7 misc: Add data/docs.yaml to ignorePaths in cspell config ba1f98a32 content: Improve collections.First/Last string examples d7b5a2efd content: Improve collections.First/Last examples e94e49db2 content: Add Smart Hugo to editor plugins af204597a content: Clarify config description 9d1cb33cd content: Fix typo dded23962 content: Clarify note on rare use case for PageRef method 8af9e3be4 content: Update the Homebrew installation description be56390b3 content: Clarify command description fa228e9f0 content: Clarify AsciiDoc extensions setting description 3a23a4af2 docs: Add best practices for manual summary marker placement 050d7c75e misc: Fix cspell configuration 74642b18d content: Miscellaneous edits 75a2c8425 content: Add missing reference definitions6303f27e1content: Improve description of the SITE.Home method4dfbd647fRevert the minimal analytics changes (#3265)c369e534dtheme: Move tracking to turbo:load88b451022theme: Move tracking to turbo:render7040dc5eeMove to minimalanalytics for GA tracking4e8a523fbcontent: Update KaTeX link attributes to v0.16.25 (phase 2)21046b942content: Update KaTeX link attributes to v0.16.25 (phase 1)b8df9d941theme: Use Path for the glossary link destinationacd6c2954theme: Use page Path when generating Markdown links543f78340content: Fix formattingaef89d192content: Correct page collection quick-reference data08645236acontent: Use RSS output format in shortcode lookup example58654c106content: Fix archetype lookup order9f79bacc4content: Describe valid uses of the nil keyword41f139d14theme: Remove the root view transition6ab407bb9content: Improve links on lang.Translate page91e724065content: Add missing fragments to links in the module mounts description2f6b4a58ccontent: Clarify ByCount sort order8594590b2Update netlify.toml811574060content: Update version referencesa12bcf7ddcontent: Update version references23ca199edUpdate netlify.toml5d3da2351content: Update version references1617ee75bmisc: Update Netlify config to build with v0.152.06467bda93content: Remove outdated new-in badges4b3ef4bd5content: Improve description of default mount behavior5a861f500content: Revise partials.IncludeCached description and signature2ad9d705acontent: Correct the partials.IncludeCached signature9f611b879Update netlify.tomlca21f5fbdcontent: Add warning about automatic summaries2065faddecontent: Update commit message guidelinesa0784f261content: Remove outdated new-in badgesce5a249f2content: Remove Codeberg Pages documentation3424b95f2content: Update CLI docs27fd350a6content: Change KaTeX version references to 0.16.23ba8c5fd3bcontent: Change MathJax version reference from v3 to v4c83465222content: Fix broken linkc6cde1074content: Update version references467277939content: Fix typoe424b6b19content: Document new features in v0.151.0a46898f79Update netlify.tomlf8407c3b5content: Enhance content view template details974175690content: Optionally disable whitespace trimming in fenced code blocks25bffc4c4Update netlify.toml98e9ef5f0theme: Move initColorScheme() to heada1566495btheme: Preload fonts (#3209)c0271d266content: Add missing word in deploy-with-rsync.md47099c4facontent: Remove outdated new-in badges439b5c18bcontent: Fix collections.D seed exampled2486ba67content: Update description of IsNode method4de8815e9content: Update description of the cascade front matter field0801e9b3fcontent: Fix broken link9bbbfb59dcontent: Update Netlify hosting guide and version refs4f444fb53misc: Update docs.yamla7a9a7563Update module.mda7909fe37Update netlify.toml47a2c2038content: Update collections.D page to remove casting noteecca5ef48content: Update summaryLength description with default value310e11195content: Change alias examplebd6b53b5dcontent: Update minify config documentation21bb0bb66Update netlify.toml68912f1a3content: Update version reference28a47a50fcontent: Fix missing link to latest release under Prebuilt Binaries8eac2a120theme: Revise inline code span styling2e71b6d0aAdjust sponsord93340a78content: Update documentation guide9f6213ebecontent: Fix glob pattern-alternatives syntax in cascade.md1f61fd6becontent: Add seed examples to collections.D65385a5d0content: Fix Cloudflare deploy not_found_handling value4f70df235content: Fix typofc216f2d9content: Fix broken linke98b340f2Update cascade.mde316dbf93content: Update version referencesf8e863e97misc: Remove textlint config file3b9c3e2a1content: Miscellaneous updates related to v0.149.03ccc439fcUpdate netlify.toml07f5140e8Merge branch 'tempv0.149.0'17f8d33d8content: Add format option to transform.Unmarshala6a971596content: Document the collections.D function66eee712bcontent: Update front matter configuration example863945e13content: Update fmt.Println example1d9a92bc7theme: Don't exclude current section from related content1386d8ed2Update introduction.md0ac89b015content: Replace inline URLs with autolinks54fad1e01content: Fix typoe09f6b0c5content: Wrap calls to eturl shortcode in angle brackets23efd9766Merge commit 'bfa74537929f409fca841540b971125b7678963a'e98754e72resources/page: Add :sectionslug and :sectionslugs permalink tokens git-subtree-dir: docs git-subtree-split: 1ad3c75ad0e997d66598c1453db9989fde7d9680
6.5 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. - 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}