ちょこっと MathJax 番外編: V4 へのアップグレード

no extension
  1. ちょこっと MathJax: 初期設定
  2. ちょこっと MathJax: 基本的な数式表現
  3. ちょこっと MathJax: インライン数式と別行立て数式
  4. ちょこっと MathJax 番外編: mathcomp パッケージの代替え
  5. ちょこっと MathJax 番外編: V4 へのアップグレード ← イマココ

先月の話だが MathJax の v4 がリリースされていた。

主な内容は以下の通り:

  • Extended font support (11 new fonts)
  • A new default font with much greater character coverage.
  • Line-breaking support (for in-line as well as displayed equations)
  • An updated expression explorer that is now on by default.
  • Separation of speech generation into a webworker thread for better performance.
  • Support for HTML embedded in TeX and MathML expressions.
  • Availability as ES6 modules as well as CommonJS modules.
  • New TeX extensions, including the begingroup extension from v2 (with more features), and several font-related extensions.
  • Inclusion of the textmacros extension in all combined component files.
  • Updated mathtools package to include the changes from 2022 and 2024.
  • Improvements in the baseline alignment of text in CHTML output in WebKit-based browsers.
  • Improved stretchy delimiters in CHTML output.
  • More promise-based conversion and typesetting calls at the document level.

本来なら Release セクションに書くべきなんだろうけど,今回は「ちょこっと MathJax」シリーズの番外編として主にアップグレード時の注意点をいくつか記しておく。 といっても v3 から v4 へのアップグレードで考慮すべき点は多くない。 詳しくは以下を参照のこと:

URL に注意

v4 では CDN の URL は以下の通りになっている。

<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>

v4 は ES6 モジュールとして実装されているため,パスから es5 が除かれている。 あと async から defer に変わっている。 id 指定もなくなってるな。

バージョンを厳密に指示したい場合は

<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4.0.0/tex-mml-chtml.js"></script>

といった感じに指定する。

利用可能なコンポーネントは以下の通り。

Component Input Output Font
tex-chtml TeX CHTML
tex-svg TeX SVG
tex-mml-chtml TeX, MathML CHTML
tex-mml-svg TeX, MathML SVG
mml-chtml MathML CHTML
mml-svg MathML SVG
tex-chtml-nofont TeX CHTML
tex-svg-nofont TeX SVG
tex-mml-chtml-nofont TeX, MathML CHTML
tex-mml-svg-nofont TeX, MathML SVG
mml-chtml-nofont MathML CHTML
mml-svg-nofont MathML SVG

v3 にあった -full 付きのコンポーネントはなくなった。 その代わり autoload 拡張機能で必要な機能を自動的に読み込むため,通常は気にする必要はない1

利用可能なフォントが増えた

v4 では11種類のフォントが利用可能になった。

Font Name Original Source
mathjax-newcm Based on New Computer Modern (now the default font)
mathjax-asana A version of the Asana-Math font
mathjax-bonum A version of the Gyre Bonum font
mathjax-dejavu A version of the Gyre DejaVu font
mathjax-fira A version of the Fira and Fira-Math fonts
mathjax-modern A version of Latin-Modern
mathjax-pagella A version of the Gyre Pagella font
mathjax-schola A version of the Gyre Schola font
mathjax-stix2 A version of the STIX2 font
mathjax-termes A version of the Gyre Termes font
mathjax-tex The original MathJax TeX font

このうち mathjax-newcm については前節で が付いているコンポーネントに含まれている。 mathjax-newcm 以外のフォントを利用したい場合は -nofont が付いているコンポーネントを指定した上で,以下のように設定する。

<script>
MathJax = {
  output: {
    font: 'mathjax-stix2'
  }
};
</script>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml-nofont.js"></script>

ちなみに mathjax-tex は v3 の既定フォントなので, v3 の環境に合わせたい場合はこれを指定する。

残念ながら,みんな大好き Euler フォントはない2。 また複数のフォントを組み合わせることもできなさそうだ。

MathJax 設定例

設定例として,このブログサイトにおける設定を挙げておく(2025-09-01 時点)。

<script>
MathJax = {
  loader: {
    load: ['[tex]/ams', '[tex]/textcomp']
  },
  tex: {
    packages: {'[+]': ['ams', 'textcomp']},
    inlineMath: {'[+]': [['$', '$']]},
    tags: 'ams',
    macros: {
      ssqrt: ['\\sqrt{\\smash[b]{\\mathstrut #1}}', 1],
      tcdegree: ['\\unicode{xb0}'],
      tccelsius: ['\\unicode{x2103}'],
      tcperthousand: ['\\unicode{x2030}'],
      tcmu: ['\\unicode{x3bc}'],
      tcohm: ['\\unicode{x3a9}']
    }
  },
  chtml: {
    displayAlign: "left",
    displayIndent: "2em"
  },
  output: {
    font: 'mathjax-stix2'
  }
};
</script>
<script defer src="//cdn.jsdelivr.net/npm/mathjax@4/tex-chtml-nofont.js"></script>

参考図書

photo
[改訂第9版]LaTeX美文書作成入門
奥村 晴彦 (著), 黒木 裕介 (著)
技術評論社 2023-12-09 (Release 2023-12-09)
単行本(ソフトカバー)
4297138891 (ASIN), 9784297138899 (EAN), 4297138891 (ISBN)
評価     

2023年末に出てるのに気が付かなかった orz 今回は版元で PDF 版を買った。

reviewed by Spiegel on 2024-04-07 (powered by PA-APIv5)


  1. ams, bbm, bboldx, begingroup, cases, centernot, colortbl, dsfont, empheq, mathtools, physics, require, setoptions, textcomp, textmacros, units, upgreek 各機能は autoload の対象にならない。新しく追加される拡張機能は autoload の対象にならない場合があるそうで,将来的に autoload が廃止される可能性もあるらしい。どうなるやら。 ↩︎

  2. Font Extensions” に Euler フォントの指定方法が書かれていたがサンプルのままでは上手く行かなかった。拡張用のフォントは自前で用意しろってことらしい。 ↩︎