Writing Latex formulas in Hugo

Writing formulas in Latex

Inline formulas: This is $E = mc^2, \frac{a}{b} = \sqrt{c}$

Block formulas: $$ E = mc^2, \frac{a}{b} = \sqrt{c} $$

Changes

  • Added " mathjax = true " to article settings

  • Modified layouts/partials/mathjax.html in mainroad’s theme as follows

{{/* MathJax v3 loader (MainroadCompatibility)
   Condition:
     - The page itself mathjax: true
     - or the entire Site params.mathjax: true
     - Or home/section/taxonomy/term with at least one article under mathjax: true 
*/}}

{{ $.Scratch.Set "need" (or (.Param "mathjax") (.Site.Params.mathjax)) }}

{{ if not ($.Scratch.Get "need") }}
  {{ $kind := .Kind }}
  {{ if eq $kind "home" }}
    {{/* Top page: Judged from regular articles throughout the site*/}}
    {{ $.Scratch.Set "need" (gt (len (where .Site.RegularPages "Params.mathjax" true)) 0) }}
  {{ else if or (eq $kind "section") (eq $kind "taxonomy") (eq $kind "term") }}
    {{/* List system: Judging from the .Pages of that page (if none, fall back to .RegularPages)*/}}
    {{ $pages := .Pages }}
    {{ if not $pages }}{{ $pages = .RegularPages }}{{ end }}
    {{ $.Scratch.Set "need" (gt (len (where $pages "Params.mathjax" true)) 0) }}
  {{ end }}
{{ end }}

{{ if $.Scratch.Get "need" }}
  <script> 
    window.MathJax = {
      tex: {
        inlineMath: [['$', '$'], ['\\(', '\\)']],
        displayMath: [['$$','$$'], ['\\[','\\]']],
        processEscapes: true
      },
      options: { skipHtmlTags: ['script','noscript','style','textarea','pre','code'] },
      svg: { fontCache: 'global' }
    };
  </script> 
  <script defer src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script> 
{{ end }}