マクロツイーター

はてダから移行した記事の表示が崩れてますが、そのうちに直せればいいのに(えっ)

\mathcode と \omathcode と \Umathcode と

LuaTeX で mathcode を設定する方法は、\mathcode(8 ビット TeX 互換)、\omathcode(Omega 互換)、\Umathcode(ネイティブ、3 値で指定)、\Umathcodenum(packed 32 ビット値で指定)の 4 系統がある。奇妙なことに、値を読み出す際には、読出時に使うプリミティブに関わらず、最後の書込時に用いた「値」が返ってくる。(\Umathcode\Umathcodenum に準じた扱い。)従って、機能的には「同値な」設定であっても読み出した値が異なるという現象が起こる。

(luatex 対話モード)
*\def\showthecode{\message{%

*\the\mathcode`a/\the\omathcode`a/\the\Umathcodenum`a/\the\Umathcode`a}}

*\mathcode`a="7161 \showthecode
29025/29025/29025/29025
*\omathcode`a="7010061 \showthecode
117506145/117506145/117506145/117506145
*\Umathcodenum`a="1E00061 \showthecode
31457377/31457377/31457377/31457377
*\Umathcode`a="7"1"61 \showthecode
31457377/31457377/31457377/31457377

"7161 = 29025;"7010061 = 117506145;"1E00061 = 31457377 である。)上の 4 つは mathcode の設定としては等価であるが、\the で読み出した場合は、「最後の設定に用いた値」が返る(\Umathcode\Umathcodenum と同じ扱い)。

某所で bm パッケージが LuaLaTeX で動かないことが話題になっているが、この理由は、ある時期から(8 ビットの範囲の)文字の mathcode の既定の設定が「\mathcode で設定されたもの」から「\Umathcode で設定されたもの」に変更され、それにより \mathcode の返す値が変わったことにある。アドホックな対処としては、このページのようなものが考えられる。これは従来の 8 ビットの範囲の文字について、Lua のインタフェース*1で mathcode を読み出した上で、それと等価な設定を \mathcode行うという処理をしている。

ところで、私は最初この「設定」の変更が、LuaTeX エンジンそのものの初期設定の変更によるものだと考えていたが、実際には、「plain と LaTeX の既定値の設定」をするために用意された luatex-unicode-latters.tex というファイルによるもののようだ。

なお、XeTeX には \mathcode\XeTeXmathcode\XeTeXmathcodenum がある((3 引数で設定する \XeteXmathcode は「読出」には使えない。))が、XeTeX で同様のことを試すと、対照的に、\mathcode で読み出す時には \mathcode での指定で使われる値が返る。

(xetex 対話モード)
*\def\showthecode{\message{%

*\the\mathcode`a/\the\XeTeXmathcodenum`a}}

*\mathcode`a="7161 \showthecode
29025/31457377
*\XeTeXmathcodenum`a="1E00061 \showthecode
29025/31457377
*\XeTeXmathcode`a="7"1"61 \showthecode
29025/31457377

\XeTeXmathcode

ちなみに、符号位置部分を 256 以上にして \mathcode で読もうとするとエラーになる。

*\XeTeXmathcode`a="7"1"24D0 \showthecode
! Extended mathchar used as mathchar (31466704).
<to be read again>
                   /
\showthecode ->\message {\the \mathcode `a/
                                           \the \XeTeXmathcodenum `a}
<*> \XeTeXmathcode`a="7"1"24D0 \showthecode

?
0/31466704

*1:これは「最後に設定に使われたプリミティブ」に依存しない。