60dd993a6content: Update GitHub Pages workflow6247e3590content: Fix typoa22255b0bcontent: Update version references21afec198content: Update version reference6226532c8content: Update version reference80d26621acontent: Update version referencecda7135b5content: Fix typo70c509e8acontent: Fix typo136b20310content: Change render hook example038246c2fcontent: Fix accidental removal441375fa4netlify: Hugo 0.147.94a620931ccontent: Note that config/env/cascade.xxx is not supported6d3a7587fconfig: Ignore front matter param override warnings623b48624content: Set searchable param to false for content/en/documentation.mdd1b2aad69content: Fix Codeberg deployment article5e3d849b8search: Use front matter parameter to control search indexingb4f90e39dcontent: Note that PATH segments must exclude project directorya81406e26content: Create aliases for deleted template pages7c3adc9adcontent: Change order of template typese093eb036content: Remove admonition from template types page0015e7a9bcontent: Update to align with v0.146.0 template system (phase 1)cf986240dcontent: Update templates.Defer noteab51fdcc7content: Use consistent templates.Defer examplesb4f8e492fcontent: Fix formatting62aa94addcontent: Fix typo83747f36bcontent: Update FAQsfc644d894Update netlify.toml4151f3a5bcontent: Fix typo9452bca92content: Update description ToMath strict option982b863c3content: Add instructions about deploying to root of user's Codeberg Pagesfc7cd2878theme: Generate QR code once per page784891b87content: Update links to the CommonMark specification741113fb5content: Fix incorrect link to Shortcodes tutorialc4962f1accontent: Update transform.ToMathad7b87223Update netlify.toml2d64e57aetheme: Remove bullet from new-in badge55a832f8eUpdate ToMath.mde1e624f49content: Document transform.ToMath strict mode2df6218f8netlify: Hugo 0.147.60259b88f1Revert "theme: Display date on news items from content adapter"43b35e56atheme: Adjust admonition dark mode colors2f15e9c34theme: Add link to Mastodond99e979d4theme: Display date on news items from content adapter6c93dac97content: Clarify the AddResource content value49006468econtent: Describe Codeberg's Forgejo Actionsbe80c3b82content: Update css.TailwindCSS optsb3ba4329ctheme: Update css.TailwindCSS opts and remove tailwind.config.js902e360b0content: Update stylesheet link for katex.js8b9e1ba3econtent: Update stylesheet link for katex.js7dae3b0e8content: Update TailwindCSS instructionscb093d210Update netlify.toml577d6fbb1content: Remove GitCMS for now8352af6cdUpdates for v0.147.42240f07a5content: Fix typo677b3fed3Correct openapi3.Unmarshal example580477aa5content: Fix typo0dc43b481content: Add GitCMS to commercial front ends listac4179f2bcontent: Fix relref shortcode examplesa699d267bFix typos7e28c4380Make sponsor logo responsive8e05f0425Update MaxInt64.mdf715265d3content: Document math.MaxInt640d57ff7d4Update netlify.tomld210e3476theme: Adjust sponsore73444b43content: Clarify page title valuesfd4c292b8content: Fix links to Pandocdd074dc67content: Bump version in GitHub Pages examplefe5537bb2content: Describe the pandoc format10834529etheme: Remove duplicate meta element838706d6etheme: More changes related to new template systemb0b6b1e69Update new-templatesystem-overview.mde719d2360Update new-templatesystem-overview.mdec2bbd395Update new-templatesystem-overview.md53319a681Add a one pager about the new template systeme17416c05Fix the spelling issue on the Host on Render descriptione8e1c82aacontent: Document range-over-int introduced in v0.123.0bc478f98fcontent: Fix editing errors606547b48Update netlify.tomle224f3f82content: Fix formatting24e4acc41content: Fix typo1e773f34bcontent: Fix typo5ef94a725content: Document alternative to shuffle for large collections7fe42d76ccontent: Fix sample code formatting5d601448fcontent: Update mathematical markup guidance63b4fe2c0content: Update version references722da91f1content: Fix GO_VERSION in Netlify hosting example8cc589c3atheme: Add current Hugo version to the header37aa24ac5netlify: Hugo 0.147.1a80b25e24content: Remove dated new-in89dcf0f92content: Fix path formatting1a5191d78content: Update transformation examples3327065abUpdate Text.md557c10dd2content: Miscellaneous updates for v0.147.00dae1a185netlify: Hugo 0.147.09b44821c3Merge branch 'tempv0.147.0'f1481e8c3content: Update GitLab Pages workflow example2e61a0360content: Update content format descriptionsfb42d7c2aimages: Add option for vertical alignment to images.Text git-subtree-dir: docs git-subtree-split:60dd993a65
6.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 href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css" rel="stylesheet"> - 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.
{{- $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 href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css" rel="stylesheet">
{{ end }}
</head>
In the above, note the use of a noop statement to force content rendering before we check the value of hasMath with the Store.Get method.
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}