# When using more than 16 math alphabets, \mathtt uses italic font

I know that in TeX, you can only use 16 math alphabets but XeTeX extends this so I was curious to enlarge the number of my math alphabets and I had to redefine some internals of LaTeX. Here is my minimal working example:

\documentclass{article}
\DeclareSymbolFont{AMSa}{U}{msa}{m}{n}
\DeclareSymbolFont{AMSb}{U}{msb}{m}{n}
\DeclareSymbolFont{MnSyA}         {U}  {MnSymbolA}{m}{n}
\DeclareSymbolFont{MnSyB}         {U}  {MnSymbolB}{m}{n}
\DeclareSymbolFont{MnSyC}         {U}  {MnSymbolC}{m}{n}
\DeclareSymbolFont{MnSyD}         {U}  {MnSymbolD}{m}{n}
\DeclareSymbolFont{MnSyE}         {U}  {MnSymbolD}{m}{n}
\makeatletter
\def\select@group#1#2#3#4{%
\ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
{%
\ifmmode
\ifnum\csname c@mv@\math@version\endcsname<\@cclvi
\begingroup
\escapechar\m@ne
\getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
\globaldefs\@ne  \math@fonts
\endgroup
\init@restore@version
\xdef#1{\noexpand\use@mathgroup\noexpand#2%
{\number\csname c@mv@\math@version\endcsname}}%
\else
\let#1\relax
\@latex@error{Too many math alphabets used in
version \math@version}%
\@eha
\fi
\else \expandafter\non@alpherr\fi
#1{#4}%
}%
}
\def\document@select@group#1#2#3#4{%
\ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
{%
\ifmmode
\ifnum\csname c@mv@\math@version\endcsname<\@cclvi
\begingroup
\escapechar\m@ne
\getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
\globaldefs\@ne  \math@fonts
\endgroup
\expandafter\extract@alph@from@version
\csname mv@\math@version\expandafter\endcsname
\expandafter{\number\csname
c@mv@\math@version\endcsname}%
#1%
\else
\let#1\relax
\@latex@error{Too many math alphabets used
in version \math@version}%
\@eha
\fi
\else \expandafter\non@alpherr\fi
#1{#4}%
}%
}
\makeatother

\DeclareSymbolFont{testa}   {U}{cmr10} {m}{n}
\DeclareSymbolFont{testb}{U}{cmr10}{bx}{n}
\DeclareSymbolFont{testc}{U}{cmr10}{m}{it}
\DeclareSymbolFont{testd}{U}{cmr10}{m}{n}
\DeclareSymbolFont{lettersA}{U}{txmia}{m}{it}

\begin{document}
$\mathtt{Test}$
\end{document}


But this makes the word test to appear in italic. You can delete any of the \DeclareSymbolFont and then \mathtt worked as expected. This shows that this only happens when the number of math alphabets exceeds 16. Why does this happen and how should one fix this?

Update1: I tried the same thing with LuaTeX and I got expected result so it seems that something is wrong with XeTeX.

Update2: Another minimal working example:

\documentclass{article}

\usepackage{amsfonts}
\usepackage{MnSymbol}

\makeatletter
\def\select@group#1#2#3#4{%
\ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
{%
\ifmmode
\ifnum\csname c@mv@\math@version\endcsname<\@cclvi
\begingroup
\escapechar\m@ne
\getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
\globaldefs\@ne  \math@fonts
\endgroup
\init@restore@version
\xdef#1{\noexpand\use@mathgroup\noexpand#2%
{\number\csname c@mv@\math@version\endcsname}}%
\else
\let#1\relax
\@latex@error{Too many math alphabets used in
version \math@version}%
\@eha
\fi
\else \expandafter\non@alpherr\fi
#1{#4}%
}%
}
\def\document@select@group#1#2#3#4{%
\ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
{%
\ifmmode
\ifnum\csname c@mv@\math@version\endcsname<\@cclvi
\begingroup
\escapechar\m@ne
\getanddefine@fonts{\csname c@mv@\math@version\endcsname}#3%
\globaldefs\@ne  \math@fonts
\endgroup
\expandafter\extract@alph@from@version
\csname mv@\math@version\expandafter\endcsname
\expandafter{\number\csname
c@mv@\math@version\endcsname}%
#1%
\else
\let#1\relax
\@latex@error{Too many math alphabets used
in version \math@version}%
\@eha
\fi
\else \expandafter\non@alpherr\fi
#1{#4}%
}%
}

\makeatother

\DeclareSymbolFont{testa}{U}{txmia}{m}{it}
\DeclareSymbolFont{testb}{U}{txmia}{m}{it}
\DeclareSymbolFont{testc}{U}{txmia}{m}{it}
\DeclareSymbolFont{testd}{U}{txmia}{m}{it}

\DeclareSymbolFont{lettersA}{U}{txmia}{m}{it}

\begin{document}
$\mathtt{Test} \mathcal{L}$
\end{document}


No problem with LuaLaTeX, but with XeLaTeX, this time, \mathtt is fine but you get italic with \mathcal.

-
Adding \showthe\textfont16 after the formula produces a Bad number (16) error with XeLaTeX and \OMS/MnSymbolS/m/n/10 with LuaLaTeX, so this seems like a bug in XeTeX. –  egreg Jun 24 '12 at 9:35

XeTeX’s \fam primitive does not seem to have been extended to accept higher values, and since the legacy mathcode format does allow higher families as well (e.g. with \mathchar and \mathcode), the only way to access higher math families for now is through extended math primitives. A simpler test:

\tracingonline=1
\font\cmscten=cmcsc10 at 10pt
\font\dunhten=cmdunh10 at 10pt
\textfont15=\cmscten
\textfont16=\dunhten
$$\fam15 a \fam16 A \XeTeXmathchar"0 16 A \showlists$$
\bye


Even if \fam was extended, it is still unreliable to use higher math families with legacy math setup because of the limitations of other math primitives (like \mathchar and \mathcode) which can not be extended in a backward compatible way, and thus existing packages will work unexpectedly when used with higher math families. Also, extended math primitives are not direct substitute for some legacy ones, for example \XeTeXdelcode can’t be used as a substitute for \delcode because it requires an OpenType math font and will not work as expected with legacy math fonts.

So, for all practical purposes, the check in LaTeX kernel should be kept, unless one is moving to a Unicode/OpenType–only math setup where higher math families can be used reliably (but then one is unlikely to ever need more than 16 \fam’s because Unicode/OpenType requires much less \fam’s than the legacy setup).

Update: In XeTeX git repository,\fam now handles numbers < 256 properly, and the \showthe\textfont issue (pointed by Enrico) is fixed is well, and both fixes are now included in TeX Live 2012.

-
Seems something to be solved: \textfont accepts 16 as argument, but then \the\textfont16` raises an error. –  egreg Jun 24 '12 at 12:57
I saw your above comment, I’m working on it already. –  Khaled Hosny Jun 24 '12 at 13:42
I had no doubt! Thanks! Unfortunately TeX Live 2012 seems to be already frozen. :( –  egreg Jun 24 '12 at 13:43
It is past being frozen, more like released-but-yet-to-be-announced. –  Khaled Hosny Jun 24 '12 at 13:48
@egreg: I think I fixed it, but it is too late for TL2012. –  Khaled Hosny Jun 24 '12 at 14:04