transform.ToMath
Syntax
transform.ToMath EXPRESSION [OPTIONS]
Returns
types.Result[template.HTML]
Arguments
- EXPRESSION
- The math expression to render using KaTeX.
- OPTIONS
- A map of zero or more options.
Options
These are a subset of the KaTeX options.
- output
- (
string
). Determines the markup language of the output. One ofhtml
,mathml
, orhtmlAndMathml
. Default ismathml
.With
html
andhtmlAndMathml
you must include KaTeX CSS within thehead
element of your base template. For example:<head> ... <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous"> ... </head>
- displayMode
- (
bool
) Iftrue
render in display mode, else render in inline mode. Default isfalse
. - leqno
- (
bool
) Iftrue
render with the equation numbers on the left. Default isfalse
. - fleqn
- (
bool
) Iftrue
render flush left with a 2em left margin. Default isfalse
. - minRuleThickness
- (
float
) The minimum thickness of the fraction lines inem
. Default is0.04
. - macros
- (
map
) A map of macros to be used in the math expression. Default is{}
. - throwOnError
- (
bool
) Iftrue
throw aParseError
when KaTeX encounters an unsupported command or invalid LaTex. See error handling. Default istrue
. - errorColor
- (
string
) The color of the error messages expressed as an RGB hexadecimal color. Default is#cc0000
.
Examples
Basic
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
Macros
{{ $macros := dict
"\\addBar" "\\bar{#1}"
"\\bold" "\\mathbf{#1}"
}}
{{ $opts := dict "macros" $macros }}
{{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }}
Error handling
There are 3 ways to handle errors from KaTeX:
- Let KaTeX throw an error and make the build fail. This is the default behavior.
- Handle the error in your template. See the render hook example below.
- Set the
throwOnError
option tofalse
to make KaTeX render the expression as an error instead of throwing an error. See [options].
layouts/_default/_markup/render-passthrough-inline.html
{{ with transform.ToMath .Inner }}
{{ with .Err }}
{{ errorf "Failed to render KaTeX: %q. See %s" . $.Position }}
{{ else }}
{{ . }}
{{ end }}
{{ end }}
{{- /* chomp trailing newline */ -}}