Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/_default/hugo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mediaTypes:
font/woff:
suffixes:
- woff
font/woff2:
suffixes:
- woff2
41 changes: 41 additions & 0 deletions exampleSite/content/english/post/math-typesetting.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,44 @@ Chemical equations:
\]

**Note:** Use the online reference of [Supported TeX Functions](https://katex.org/docs/supported.html)

### Mathematical formulae inside code blocks

As an alternative to the standard syntax used above, formulae can also be
authored using a
[GLFM math block](https://docs.gitlab.com/ee/user/markdown.html#math):

````markdown
The probability of getting \(k\) heads when flipping \(n\) coins is:

```math
\tag*{(1)} P(E) = {n \choose k} p^k (1-p)^{n-k}
```
````

This `math` block renders to:

The probability of getting \(k\) heads when flipping \(n\) coins is:

```math
\tag*{(1)} P(E) = {n \choose k} p^k (1-p)^{n-k}
```

### Chemical equations inside code blocks

You can use a code block adorned with `chem` in order to render
a chemical equation:

````markdown
```chem
\tag*{(2)} \ce{Hg^2+ ->[I-] $\underset{\mathrm{red}}{\ce{HgI2}}$ ->[I-] $\underset{\mathrm{red}}{\ce{[Hg^{II}I4]^2-}$}
```
````

This `chem` block renders to:

<!-- prettier-ignore-start -->
```chem
\tag*{(2)} \ce{Hg^2+ ->[I-] $\underset{\mathrm{red}}{\ce{HgI2}}$ ->[I-] $\underset{\mathrm{red}}{\ce{[Hg^{II}I4]^2-}}$}
```
<!-- prettier-ignore-end -->
1 change: 1 addition & 0 deletions layouts/_default/_markup/render-codeblock-chem.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ partial "math.html" . -}}
1 change: 1 addition & 0 deletions layouts/_default/_markup/render-codeblock-math.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ partial "math.html" . -}}
17 changes: 1 addition & 16 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,6 @@
<!-- KaTeX-->
{{ $noop := .WordCount }}
{{ if .Page.Store.Get "hasMath" }}
{{ $katex_css_url := printf "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex%s.css" (cond hugo.IsProduction ".min" "") -}}
{{ with try (resources.GetRemote $katex_css_url) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}}
{{ else with.Value -}}
{{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . }}
{{ $secureCSS := . | resources.Fingerprint "sha512" -}}
<link
rel="stylesheet"
href="{{- .RelPermalink -}}"
integrity="{{- $secureCSS.Data.Integrity -}}"
crossorigin="anonymous"
/>
{{ end -}}
{{ end -}}
{{ end -}}
{{ partial "katex.html" . }}
{{ end }}
</head>
48 changes: 48 additions & 0 deletions layouts/partials/katex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{/* stylesheet */ -}}

{{ $cssFile := cond hugo.IsProduction "katex.min.css" "katex.css" -}}
{{ $cssUrl := printf "https://unpkg.com/katex@latest/dist/%s" $cssFile -}}
{{ with try (resources.GetRemote $cssUrl) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}}
{{ else with .Value -}}
{{ with resources.Copy (printf "css/%s" $cssFile) . -}}
{{ $cssHash := . | fingerprint "sha512" -}}
<link
rel="stylesheet"
href="{{- .RelPermalink -}}"
integrity="{{- $cssHash.Data.Integrity -}}"
crossorigin="anonymous">
{{ end -}}
{{ end -}}
{{ end -}}

{{/* font files */ -}}

{{ $fontFiles := slice -}}
{{ $data := dict -}}
{{ $url := "https://unpkg.com/katex@latest/dist/fonts?meta" -}}
{{ with try (resources.GetRemote $url) -}}
{{ with .Err -}}
{{ errorf "%s" . -}}
{{ else with ( .Value | unmarshal ) -}}
{{ range .files -}}
{{ $fontFiles = $fontFiles | append .path -}}
{{ end -}}
{{ else -}}
{{ errorf "Unable to get remote resource %q" $url -}}
{{ end -}}
{{ end -}}

{{ range $fontFile := $fontFiles -}}
{{ $fontUrl := (printf "https://unpkg.com/katex@latest%s" $fontFile) -}}
{{ with try (resources.GetRemote $fontUrl) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve KaTeX font file from CDN. Reason: %s." . -}}
{{ else with .Value -}}
{{ with resources.Copy (printf "css/fonts/%s" (replace $fontFile "/dist/fonts/" "")) . -}}
{{ .Publish -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
9 changes: 9 additions & 0 deletions layouts/partials/math.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{ $opts := dict "output" "htmlAndMathml" "displayMode" (not (eq ($.Type) "inline")) -}}
{{ 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." . $.Position -}}
{{ else -}}
{{ .Value -}}
{{ $.Page.Store.Set "hasMath" true -}}
{{ end -}}
{{ end -}}