10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

unicode-math でフォントを使いこなしたい

Last updated at Posted at 2023-09-01

unicode-math では OpenType フォントを利用して Unicode な数式を出力することが出来ます。このフォントを簡単に変更できます。

本記事では、unicode-math を利用してフォントを変更したり、フォントの機能を利用する方法を紹介します。

本記事における unicode-math のバージョンは 0.8r です。

フォントを変更する

unicode-math でフォントを変更する場合、\setmathfont を利用して OpenType フォントを指定します。

\setmathfont{<font>}

後述しますが、\setmathfont はオプション引数から OpenType フォントの機能を利用することが出来ます。

ここで、<font> はフォント名あるいはフォントのファイル名を指していますが、一般に一覧化されていません。
そのため、次節では TeX Live に集録されている OpenType フォントの一覧にします。

unicode-math で使えるフォント

unicode-math で利用できる OpenType フォントを luafindfont で探しました。

$ luafindfont math
  No.                   Filename         Symbolic name                                                                  Path
   1.             asana-math.otf             asanamath          d:/texlive/2023/texmf-dist/fonts/opentype/public/asana-math/
   2.                  asana.ttc             asanamath          d:/texlive/2023/texmf-dist/fonts/truetype/public/asana-math/
   3.                cambria.ttc           cambriamath                                                     c:/windows/fonts/
    ⋮
  29.          xitsmath-bold.otf              xitsmath                d:/texlive/2023/texmf-dist/fonts/opentype/public/xits/
  30.       xitsmath-regular.otf              xitsmath                d:/texlive/2023/texmf-dist/fonts/opentype/public/xits/

これによって、TeX ツリー内では以下の 19 のフォントが数式フォントとして利用できることが分かりました。(フォント名はフォントのファミリ名です)

フォント名 ファイル名
Asana Math Asana-Math.otf
Concrete Math Concrete-Math.otf
Erewhon Math Erewhon-Math.otf
Euler Math Euler-Math.otf
Fira Math FiraMath-Regular.otf
Garamond-Math Garamond-Math.otf
GFS Neohellenic Math GFSNeohellenicMath.otf
KpMath Regular KpMath-Regular.otf
Libertinus Math LibertinusMath-Regular.otf
Latin Modern Math latinmodern-math.otf
NewComputerModernMath NewCMMath-Regular.otf
STIX Two Math STIXTwoMath-Regular.otf
TeX Gyre Bonum Math texgyrebonum-math.otf
TeX Gyre DejaVu Math texgyredejavu-math.otf
TeX Gyre Pagella Math texgyrepagella-math.otf
TeX Gyre Schola Math texgyreschola-math.otf
TeX Gyre Termes Math texgyretermes-math.otf
XCharter Math XCharter-Math.otf
XITS Math XITSMath-Regular.otf
フォント基本情報(折りたたみ)

フォント情報は wakamaifondue.com で調べました。

Font features の詳細については、以下を参照してください。

フォント名 Characters (Glyphs) Font features
Asana Math 3445 (3447) dtls, onum, ssty, salt, kern,
Concrete Math 2234 (2236) dtls, onum, ssty, kern, cv01, cv02, cv03, cv04, cv05, cv06, cv07, cv08, cv09, cv10, ss03, ss04, ss05, ss06,
Erewhon Math 2862 (2864) dtls, lnum, onum, pnum, ssty, tnum, kern, cv00, cv01, cv02, cv03, cv04, cv05, cv06, cv07, cv08, cv09, cv10, cv11, cv20, cv21, cv22, ss03, ss04, ss05, ss06, ss07, ss09,
Euler Math 3220 (3222) dtls, onum, ssty, salt, kern, cv01, cv02, cv03, cv04, cv05, cv06, cv09, cv10, ss05, ss06,
Fira Math 1565 (2094) dtls, pnum, ssty, liga, ss01, ss02, ss03,
Garamond-Math 2595 (5243) dtls, pnum, ssty, kern, ss01, ss02, ss03, ss04, ss05, ss06, ss07, ss08, ss09, ss10, ss11,
GFS Neohellenic Math 2795 (3546) dtls, lnum, ssty, tnum, c2sc, kern, smcp,
KpMath 3446 (3448) dtls, lnum, onum, ssty, cv00, cv01, cv02, cv03, cv04, cv05, cv06, cv07, cv08, cv09, cv10, ss01, ss02, ss03, ss04, ss05, ss06, ss07,
Libertinus Math 3647 (4130) ssty, zero, dlig, hlig, liga, locl, kern, mark, mkmk, ss08,
Latin Modern Math 2044 (4802) dtls, ssty, aalt,
NewComputerModernMath 4007 (7041) dtls, ssty, aalt, cv01, ss01, ss02,
STIX Two Math 4604 (6760) dtls, flac, ssty, aalt, locl, case, ccmp, kern, cv01, cv02, cv03, cv04, ss01, ss02, ss03, ss04, ss05, ss06, ss07, ss08, ss09, ss10, ss11, ss12, ss13, ss14, ss15, ss16, ss20,
TeX Gyre Bonum Math 2105 (4246) dtls, ssty, aalt,
TeX Gyre DejaVu Math 2111 (4280) dtls, ssty, aalt,
TeX Gyre Pagella Math 2105 (4246) dtls, ssty, aalt,
TeX Gyre Schola Math 2105 (4246) dtls, ssty, aalt,
TeX Gyre Termes Math 2105 (4246) dtls, ssty, aalt,
XCharter Math 2674 (2676) dtls, lnum, onum, ssty, cv00, cv01, cv02, cv03, cv04, cv05, cv06, cv07, cv08, cv09, cv10, cv20, cv21, cv22, ss03, ss04, ss05, ss06, ss07,
XITS Math 3561 (4559) dtls, ssty, kern, rtls, cv01, ss01, ss02, ss03, ss04, ss05, ss06, ss07, ss08, ss09, ss10,

数式フォントに関するサンプルを作成しました。以下の記事を参照してください。

Character variant と Stylistic set

OpenType フォントには、代替となる字形を選択できる Character variantStylistic set という機能があります。この機能を利用することで、いくつかの数学記号の代替字形をサポートしています。

Character variant は特定の 1 字、Stylistic set は複数文字のセットが対象になっています。
また、Character variant は cv01~cv99 までの 99、Stylistic set は ss01~ss20 までの 20 の代替字形組があります。

フォントの Character variant・Stylistic set の詳細を知りたい場合は、パッケージガイドまたは wakamaifondue.com を参照してください。具体的な字形の違いを見ることが出来ます。

unicode-math では、\setmathfont のオプションから CharacterVariantStylisticSet を有効にできます。

\setmathfont{<font>}[
    CharacterVariant = <cv_number>,
    StylisticSet = <ss_number>,
  ]

Stylistic set に関して、特定の字形範囲のみを指定する場合は次のように指定します。

\setmathfont{<font>}
\setmathfont{<font>}[
    range = { <character> },
    StylisticSet = <ss_number>,
  ]

range では文字コードまたは字形組を指定します。
字形組は次の 17 を指します。(固有コマンド接頭辞とは異なることに注意してください)

upbfupitbfitbbbbitcalbfcalfrakbffrakscrbfscrsfupbfsfupsfitbfsfittt

また、字形組の <character> の後に / に続けて latinLatingreekGreek を置くことでラテン文字やギリシャ文字のみにフォーカスすることが出来ます。

\setmathfont{<font>}[
    range = <character>/{greek, Greek},
    % 小文字ギリシャ文字と大文字ギリシャ文字のみのスタイルにフォーカス
  ]

Character variant・Stylistic set に割り当てられる機能は、フォントによって大きく異なることに注意してください。

また、cvss 番号と機能もフォントによって異なります。

この節では Character variant・Stylistic set に関する主要な例を紹介します。

\hslash\emptyset

これまでの LaTeX では、“ℏ” はスラッシュが斜めの \hslash と水平の \hbar\emptyset “∅” は円が ○ の \varnothing と 0 の \emptyset の 2 つの字形があります。
しかし、これらは Unicode 上で異なる文字として登録されていません。

したがって、unicode-math では同一記号を出力します。

hslash and emptyset

これらは Character variant・Stylistic set を利用することで切り替えることが出来ます。

TeX Live に集録されているフォントでは次のようになっていました。

フォント名 \hslash \emptyset
Concrete Math cv01 cv02
Erewhon Math cv01 cv02
Euler Math cv01 cv02
Garamond-Math ss06 -
KpMath cv01 cv02
STIX Two Math ss03 cv03
XCharter Math cv01 cv02
XITS Math ss10 -

XCharter Math を利用しているときに \hslashcv01 の代替字形に変更する場合:

\usepackage{unicode-math}
\setmathfont{XCharter Math}[
    CharacterVariant = 1,
  ]

Character variant は range を指定しないため、\setmathfont を重複して読み込む必要がありません。

Calligraphic 体と Script 体

これまでの LaTeX では、\mathcal\mathscr のような異なる 2 つの Calligraphic 体と Script 体があります。
しかし Unicode はこの 2 つの字形を区別せず、同じ文字コードとして登録されています。そのため、フォントをそのままを利用する際には、この 2 つの字形を同時に使うことが出来ません。

この問題を Stylistic set から解消することが出来ます。

以下のフォントには、Calligraphic/Script 体を切り替える Stylistic set があります。

  • scr, bfscr:
    • NewComputerModernMath (ss01)
    • STIX Two Math (ss01)
  • cal, bfcal:
    • XITS Math (ss01)
    • KpMath (ss01)
    • Garamond-Math (ss03, ss08)

NewComputerModernMath と STIX Two Math は、元の字形が \mathcal に相当するため、Stylistic set の有効にする範囲を scr, bfscr のみに限定します。

\setmathfont{STIX Two Math}
\setmathfont{STIX Two Math}[
    range = { scr, bfscr },
    StylisticSet = 1
  ]

一方で、XITS Math、KpMath、Garamond-Math は、元の字形が \mathscr に相当するため、Stylistic set の有効にする範囲を cal, bfcal のみに限定します。

\setmathfont{XITS Math}
\setmathfont{XITS Math}[
    range = { cal, bfcal },
    StylisticSet = 1
  ]

上のように range のスタイルをフォントによって変えない場合、\mathcal\mathscr\symcal\symscr の対応が逆転します。

STIX Two Math と Garamond-Math (ss08) は、小文字も切り替えの対象になっています。また、Garamond-Math (ss08) は bfcalcal の太字に対応しません。

フォントサンプル(折りたたみ)

NewComputerModern-01

STIXTwoMath-01

XITSMath-01

KpMath-01

Garamond-Math-03

Garamond-Math-08

黒板太字のセリフ・サンセリフ体

黒板太字には、セリフ体とサンセリフ体があります。これを切り替える Stylistic set が与えられているフォントがあります。

  • Garamond-Math (ss01)
  • KpMath (ss02)

Garamond-Math を利用しているときに黒板太字をセリフ体に変更する場合:

\setmathfont{Garamond-Math}[
    StylisticSet = 1
  ]
フォントサンプル(折りたたみ)

KpMath-02

Garamond-Math-02


ちなみに、黒板太字の斜体が使いたい場合、\symbbit で指定できるシンボルは “ⅅ”・“ⅆ”・“ⅇ”・“ⅈ”・“ⅉ” の 5 つのみです。もしもすべてのラテン文字で斜体黒板太字を利用したい場合は XITS Math の ss06 を利用すると \symbb が斜体黒板太字になります。(ss07 は太字斜体の黒板太字です)

その他

Character variant・Stylistic set には上記の他に以下のような代替字形があります。

  • Character variant:
    • “0” の代替字形 (e.g. cv00)
    • ギリシャ文字の代替字形 (e.g. cv03~cv10)
    • \partial (𝜕) の代替字形 (e.g. cv11)
    • \symscr における “E”・“Q”・“T” の代替字形 (e.g. cv20~cv22)
  • Stylistic set:
    • upint (e.g. ss03)
    • leqslant (e.g. ss04)
    • smaller (e.g. ss05)
    • subsetneq (e.g. ss06)
    • parallelslant (e.g. ss07)

代替字形の詳細については割愛しますが、簡易的に知りたい場合は、Erewhon Math または XCharter Math のパッケージガイドを参照してください。

STIX Two Math はより多くの代替字形を有しています。

複数のフォントを導入する

別のフォントを含める場合、シンボルのウェイト等の部分で元のフォントとの雰囲気が合致しない可能性があることに注意してください。

\setmathfont では、range を利用して特定の範囲のシンボルを別のフォントに切り替えることが出来ます。

例えば、Stylistic set で Calligraphic/Script 体を切り替えることのできない Latin Modern Math を利用している場合を考えます。
Latin Modern Math では、\mathcal に相当する字形のみが提供されるため、\mathscr に相当する字形を別のフォントから導入する必要があります。

以下のようにすると、\symscr\symbfscr だけ XITS Math に変更することが出来ます。

\setmathfont{Latin Modern Math}
\setmathfont{XITS Math}[
    range = { scr, bfscr }
  ]

他にも、StylisticSet = 1 を含めて NewComputerModernMath の Script 体に変更することも出来ます。

\setmathfont{Latin Modern Math}
\setmathfont{NewComputerModernMath}[
    range = { scr, bfscr },
    StylisticSet = 1
  ]

range は文字コードの指定もできます。

例えば、NewComputerModernMath を利用しながら、大型演算子だけ Concrete Math に変更する場合を考えます。

unimath-symbols.pdf を参照することで、大型演算子の文字コード (USV) を書き出すと、次のリストのようになることが分かります。
(unimath-symbols.pdf ではまとめられていますが、文字コードはバラバラになっていることに注意してください)

  • U+2140
  • U+220F~U+2211
  • U+222B~U+2233
  • U+22C0~U+22C3
  • U+27D5~U+27D9
  • U+29F8~U+29F9
  • U+2A00~U+2A21
  • U+2AFC
  • U+2AFF
  • U+1EEF0~U+1EEF1

元の USV では U+02140 のように 5 桁で表現されていますが、頭が 0 の場合は削っても大丈夫です。
また、実際に指定する際には U+" に置き替えます。

以下のように、NewComputerModernMath に加えて Concrete Math を range で指定します。

\setmathfont{NewComputerModernMath}
\setmathfont{Concrete Math}[
    range = {
      "2140,
      "220F-"2211,
      "222B-"2233,
      "22C0-"22C3,
      "27D5-"27D9,
      "29F8-"29F9,
      "2A00-"2A21,
      "2AFC, "2AFF,
      "1EEF0-"1EEF1,
    }
  ]

これに加えて、Concrete Math には積分記号を立体にする ss03 があります。これを有効にしたい場合は以下のようにします。

\setmathfont{NewComputerModernMath}
\setmathfont{Concrete Math}[
    range = {
      "2140,
      "220F-"2211,
      "222B-"2233,
      "22C0-"22C3,
      "27D5-"27D9,
      "29F8-"29F9,
      "2A00-"2A21,
      "2AFC, "2AFF,
      "1EEF0-"1EEF1,
    },
    StylisticSet = 3,
  ]

ただし、あまりにも多くのフォントを利用しようとすると、タイプセットが遅くなり成果物の PDF のサイズが大きなものになります。

Stylistic set とリテラル入力

Stylistic set で変更した字形は、\sym~ で変更する字形にのみ影響します。

そのため、TeX ソースへのリテラルな入力や固有コマンドを直接打つ場合には有効になりません。(元の字形が出力されます)

scr StylisticSet bb StylisticSet

余談

unicode-math を使うために Unicode や OpenType フォントの知識もちょっと必要になるバグ。

TeX Live には otfinfo がありますが、情報がテキストのみなので、あまり有益な情報を得ることが出来ませんでした。
wakamaifondue.com は OpenType フォントファイルをアップロードすることで、フォントに関する情報を視覚的に得ることができ、非常に便利でした。

ローカル内のフォントの探し方

fontspec パッケージでは、otfinfo を使ってフォント情報を得ろと書いてありました。フォントファイルのあるフルパスが分かる場合はこれで十分です。

$ otfinfo -i `kpsewhich lmroman10-regular.otf`
Family:              LM Roman 10
Subfamily:           Regular
Full name:           LMRoman10-Regular
PostScript name:     LMRoman10-Regular
Preferred family:    Latin Modern Roman
Preferred subfamily: 10 Regular
Mac font menu name:  LM Roman 10 Regular
Version:             Version 2.004;PS 2.004;hotconv 1.0.49;makeotf.lib2.0.14853
Unique ID:           2.004;UKWN;LMRoman10-Regular
Trademark:           Please refer to the Copyright section for the font trademark attribution notices.
Copyright:           Copyright 2003, 2009 B. Jackowski and J. M. Nowacki (on behalf of TeX users groups). This work is released under the GUST Font License --  see http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt for details.
Vendor ID:           UKWN

-i-f にするとフォント機能 (font feature) を知ることができます。

任意のフォントを探したい場合は、luafindfont を利用すると良いです。
TeX ツリー外のフォントも探せるため便利です。

$ luafindfont concrete
  No.          Filename Symbolic name                                                            Path
   1.       cmunobi.otf  cmuconcrete    d:/texlive/2023/texmf-dist/fonts/opentype/public/cm-unicode/
   2.       cmunobx.otf  cmuconcrete    d:/texlive/2023/texmf-dist/fonts/opentype/public/cm-unicode/
   3.       cmunorm.otf  cmuconcrete    d:/texlive/2023/texmf-dist/fonts/opentype/public/cm-unicode/
   4.       cmunoti.otf  cmuconcrete    d:/texlive/2023/texmf-dist/fonts/opentype/public/cm-unicode/
   5. concrete-math.otf concretemath  d:/texlive/2023/texmf-dist/fonts/opentype/public/concmath-otf/

ちなみに、ややこしいですが OpenType フォントの拡張子は .otf の他にも .ttf.ttc もあります。拡張子だけで OpenType フォントではない(TrueType フォントだ)と決めつけないように注意してください。(参考
実際、Windows に標準インストールされている Cambria Math は .ttc です。

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?