transform.ToMath
    
  
    New in v0.132.0
  
  
  
    
  
    
      
  
  
  
  
  
  
  
參數
- EXPRESSION
- 使用 KaTeX 渲染的數學表達式。
- OPTIONS
- 一個包含零個或多個選項的映射。
選項
以下是 KaTeX 選項 的一部分:
- output
- (string)。決定輸出的標記語言。可以是html、mathml或htmlAndMathml。預設為mathml。若使用 html或htmlAndMathml,必須在基礎模板的head元素中包含 KaTeX 的 CSS。例如:<head> ... <link rel="stylesheet" href="https://cdn.jsdelivr.net/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 }}
錯誤處理
KaTeX 提供 3 種錯誤處理方式:
- 讓 KaTeX 拋出錯誤並使建置失敗(預設行為)。
- 在模板中處理錯誤。參見下方的渲染掛載範例。
- 將 throwOnError選項設為false,讓 KaTeX 將表達式渲染為錯誤而非拋出錯誤。詳見 選項。
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 }}
{{- /* 移除結尾換行符號 */ -}}