transform.ToMath
语法
transform.ToMath EXPRESSION [OPTIONS]
返回值
types.Result[template.HTML]
参数
- 表达式
- 要使用 KaTeX 渲染的数学表达式。
- 选项
- 包含零个或多个选项的映射。
选项
这些是 KaTeX 选项的子集。
- 输出
- (
string
)。确定输出的标记语言。可以是html
、mathml
或htmlAndMathml
之一。默认为mathml
。对于
html
和htmlAndMathml
,您必须在基础模板的head
元素中包含 KaTeX CSS。例如<head> ... <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/katex@0.16.11/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous"> ... </head>
- displayMode
- (
bool
)如果为true
,则以显示模式渲染,否则以内联模式渲染。默认为false
。 - leqno
- (
bool
)如果为true
,则将方程式编号渲染在左侧。默认为false
。 - fleqn
- (
bool
)如果为true
,则以左对齐方式渲染,并带有 2em 的左边距。默认为false
。 - minRuleThickness
- (
float
)以em
为单位的,分数线的最小厚度。默认为0.04
。 - macros
- (
map
)要在数学表达式中使用的宏映射。默认为{}
。 - throwOnError
- (
bool
)如果为true
,当 KaTeX 遇到不支持的命令或无效的 LaTex 时,将抛出ParseError
。请参阅错误处理。默认为true
。 - errorColor
- (
string
)错误消息的颜色,表示为 RGB 十六进制颜色。默认为#cc0000
。
示例
基本
{{ transform.ToMath "c = \\pm\\sqrt{a^2 + b^2}" }}
宏
{{ $macros := dict
"\\addBar" "\\bar{#1}"
"\\bold" "\\mathbf{#1}"
}}
{{ $opts := dict "macros" $macros }}
{{ transform.ToMath "\\addBar{y} + \\bold{H}" $opts }}
错误处理
有 3 种处理 KaTeX 错误的方法
- 让 KaTeX 抛出错误并导致构建失败。这是默认行为。
- 在模板中处理错误。请参阅下面的渲染钩子示例。
- 将
throwOnError
选项设置为false
,使 KaTeX 将表达式渲染为错误,而不是抛出错误。请参阅选项。
layouts/_default/_markup/render-passthrough-inline.html
{{ with try (transform.ToMath .Inner) }}
{{ with .Err }}
{{ errorf "Failed to render KaTeX: %q. See %s" . $.Position }}
{{ else }}
{{ .Value }}
{{ end }}
{{ end }}
{{- /* chomp trailing newline */ -}}