Skip to content

Switch math rendering from MathJax to server-side KaTeX#1

Merged
joshlk merged 2 commits into
mainfrom
feat/katex-math
May 8, 2026
Merged

Switch math rendering from MathJax to server-side KaTeX#1
joshlk merged 2 commits into
mainfrom
feat/katex-math

Conversation

@joshlk

@joshlk joshlk commented May 8, 2026

Copy link
Copy Markdown
Owner

Summary

Replaces client-side MathJax with build-time KaTeX rendering via the jektex plugin. This applies the approach from cotes2020/jekyll-theme-chirpy#2603 directly to this site (since the theme is consumed as a gem).

  • Gemfile / _config.yml — add jektex to :jekyll_plugins, configure cache dir + ignore globs (feeds/manifests/JS/CSS), and set math.engine: katex.
  • _includes/head.html & _includes/js-selector.html — local overrides of the Chirpy v7.5.0 templates: load the KaTeX stylesheet and skip the MathJax scripts when math.engine == 'katex' (still falls back to MathJax if you flip the config).
  • _plugins/katex-inline-math.rb — kramdown only recognises $$ … $$ as math, and the old MathJax config papered over this by also scanning the page for $ … $ in the browser. This pre-render hook upgrades single-dollar inline math to $$ … $$ (skipping fenced code blocks) so all existing posts render unchanged. Only runs on pages with math: true.
  • assets/css/jekyll-theme-chirpy.scss — add .katex-display overflow/padding so wide display equations scroll horizontally instead of overflowing the post column.
  • .gitignore — ignore .jektex-cache.

Why

Math is now rendered at build time, so posts no longer ship MathJax JS (~1 MB) and equations appear immediately on load instead of flashing raw LaTeX.

Notes

  • KaTeX doesn't support \label / \eqref — confirmed no posts use them.
  • The Chirpy include overrides are pinned to v7.5.0; re-sync them when bumping the theme.

Test plan

  • bundle install && JEKYLL_ENV=production bundle exec jekyll b -d _site succeeds (Ruby 3.4)
  • bundle exec htmlproofer _site --disable-external … passes
  • All 10 math posts render with class="katex" spans and katex.min.css linked; 0 katex-error elements
  • No leftover unrendered $…$ / \( \) / \[ \] outside code/script blocks
  • No MathJax/mathjax script references in any post
  • Non-math pages (home, about, categories) contain no KaTeX/MathJax references
  • feed.xml / sitemap.xml still well-formed and unaffected
  • Visually checked KL-divergence and tensor-calculus posts in a browser — inline + display math look correct
  • Confirm GitHub Pages deploy succeeds (CI uses Ruby 3.3 + node for execjs)

joshlk added 2 commits May 8, 2026 16:44
Render LaTeX at build time with the jektex plugin instead of loading
MathJax in the browser. Posts no longer ship any math JS and equations
appear immediately on page load.

- Add jektex to the Gemfile (:jekyll_plugins) and configure it in
  _config.yml; cache lives in .jektex-cache (gitignored).
- Override _includes/head.html and _includes/js-selector.html (from
  Chirpy v7.5.0) to load the KaTeX stylesheet and skip the MathJax
  scripts when math.engine is "katex".
- Add _plugins/katex-inline-math.rb: a pre-render hook that rewrites
  single-dollar inline math to kramdown's `$$ ... $$` form on pages
  with `math: true`, so existing posts keep working without edits.
- Add .katex-display overflow/padding so wide display equations scroll
  instead of overflowing the post column.
Records why the theme's MathJax was replaced with build-time KaTeX
(jektex), how the rendering pipeline fits together, the authoring
rules for math posts, and the step-by-step procedure for re-syncing
the vendored _includes overrides when bumping jekyll-theme-chirpy.
@joshlk joshlk merged commit b920bb3 into main May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant