# The restriction of 16 math fonts of TeX

I need to use multiple math fonts to do typesetting . I understand that, with the current version of TeX, we can only have 16 math fonts for each mathversion and there is no way to change this setting. One potential solution would be using \usefont to assign each desired glyph individually. However, it becomes very inefficient when it comes to a whole collection of font.

I intend to define new mathversions like \boldmath, and then attribute new fonts to each math mode so that the restriction of 16 fonts will be overcome. Are there systematic ways to achieve this goal ?

• do you mean \boldmath rather than \bm. The latter is not a change of mathversion.
– user4686
Mar 11 '15 at 17:50
• It isn't clear what you mean really. Especially the reference to bm as that works in exactly the opposite direction, normally you get bold math fonts from the separate bold mathversion (\boldmath) but bm copies the fonts from the bold math version into the standard one so that you don't have to change math versions, but it is very susceptible to hitting the 16 limit. Mar 11 '15 at 17:58
• Notice that all related LaTeX command are preamble-only (\DeclareSymbolFont, DeclareMathVersion,...) you might have to overcome that hurdle as well.
– user4686
Mar 11 '15 at 18:09
• Did you see tex.stackexchange.com/questions/100426/… ? Mar 12 '15 at 8:39

LaTeX lets you declare more than 16 math fonts.

You can declare more than 16 \DeclareMathAlphabet but they will only be allocated and take up one of the 16 fonts if they are actually used in a document. So you can have a package that declares fraktur and script and bold italic and .... and unless a user actually tries uses them all in the same document it is OK.

The allocation of symbol fonts is different. For efficiency reasons they are allocated as they are declared so that the numerical value 0-16 can be used when defining characters.

So one way to get the most fonts into a math version is not to use math alphabets just use the 16 families for symbol fonts. Then use \textit rather than \mathit and similarly define \text.. versions for any other math alphabets that you need, \textit and \mathit use the same font but the former accesses the font via text mode and so does not use up one of the 16 math groups.

Alternatively of course one could use xelatex or lulatex where each font can have a million characters, up from 256 and you can have 256 of them in each math list, up from 16, so it's a lot less likely that you run out of slots.

If you are using xetex or luatex you can use 256 rather than 16 slots. You can only use \mathchardef with the first 16 slots so it is best to restrict \DeclareSymbolFont to that range, but math alphabets can use the full range.

This document has 17 math alphabets in addition to the standard symbol fonts, so 20 or so in all.

The redefinitions between \makeatletter and \makeatother below are not needed in LaTeX releases from 2015 onwards. New LaTeX formats detect xetex or luatex and allow the 256 math alphabets without needing any extra definitions.

\makeatletter
\ifx\e@alloc\@undefined
\def\new@mathgroup{\alloc@8\mathgroup\chardef\@cclvi}
\let\newfam\new@mathgroup
\fi

\def\select@group#1#2#3#4{%
\ifx\math@bgroup\bgroup\else\relax\expandafter\@firstofone\fi
{%
\ifmmode
\ifnum\csname c@mv@\math@version\endcsname<256 %\sixt@@n
\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<256 %\sixt@@n
\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}%
}%
}

\def\DeclareSymbolFont#1#2#3#4#5{%
\@tempswafalse
\edef\reserved@b{#2}%
\def\cdp@elt##1##2##3##4{\def\reserved@c{##1}%
\ifx\reserved@b\reserved@c \@tempswatrue\fi}%
\cdp@list
\if@tempswa
\@ifundefined{sym#1}{%
%% only allow symbol fonts below 16
\ifnum\count18<15 %
\expandafter\new@mathgroup\csname sym#1\endcsname
\expandafter\new@symbolfont\csname sym#1\endcsname
{#2}{#3}{#4}{#5}%
\else
\@latex@error{Too many symbol fonts declared}\@eha
\fi
}%
{%
\@font@info{Redeclaring symbol font #1'}%
\def\group@elt##1##2{%
\noexpand\group@elt\noexpand##1%
\expandafter\ifx\csname sym#1\endcsname##1%
\expandafter\noexpand\csname#2/#3/#4/#5\endcsname
\else
\noexpand##2%
\fi}%
\xdef\group@list{\group@list}%
\def\version@elt##1{%
\expandafter
\SetSymbolFont@\expandafter##1\csname#2/#3/#4/#5\expandafter
\endcsname \csname sym#1\endcsname
}%
\version@list
}%
\else
\@latex@error{Encoding scheme  #2' unknown}\@eha
\fi
}

\makeatother
\documentclass{article}
\usepackage{amsfonts,amssymb}
\usepackage[mathscr]{euscript}
\DeclareSymbolFont{bsymbols}     {OMS}{cmsy}{b}{n}
\DeclareMathAlphabet{\mathrs}{U}{rsfso}{m}{n}
\DeclareMathAlphabet{\mathbbx}{U}{bbold}{m}{n}
\DeclareMathAlphabet{\mathbit}{OT1}{cmr}{bx}{it}
\DeclareMathAlphabet{\mathbsf}{OT1}{cmss}{bx}{n}
\DeclareMathAlphabet{\mathtm}{OT1}{ptm}{m}{n}
\DeclareMathAlphabet{\mathbtm}{OT1}{ptm}{bx}{n}
\DeclareMathAlphabet{\mathdh}{OT1}{cmdh}{m}{n}
\begin{document}

$1+2\left(\sum\limits_0^nx_i\right\} A \mathit{A} \mathbit{A} \mathtt{A} \mathsf{A} \mathbsf{A} \mathrm{A} \mathbf{A} \mathscr{A} \mathbb{A} \mathbbx{A} \mathcal{A} \mathfrak{A} \mathrs{A} \mathtm{A} \mathbtm{A} \mathdh{A}$

\end{document}

• Yes, I am using XelaTeX in fact. But it would become much better if we can figure out a method to use math fonts directly. As you said, the 16 restriction will be exceeded if there are used in one document simultaneously. I indeed had this situation. After I implement a new math mode command , for example \mathscr{J}, the console indicates error because of restriction. However for the same case, a different version of this font can be used, like \bm{\mathscr{J}}, without generating any error. This fact makes me think the possibility to circumvent fonts restriction by defining new mathversions.
– Qian
Mar 12 '15 at 4:57
• @Elias well (1) if you are using xelatex you have 256 not 16 families so it is less of an issue (you may need to fix \newfam so it knows about xelatex) and (2) \mathscr is exactly the kind of command I meant, if you implemented it like \textit rather than \mathit it would not use a slot at all. It is only symbol fonts where you need individual commands for each glyph like \rightarrow where the problem occurs. Mar 12 '15 at 8:30
• @Elias \bm does not use a different math version (unless you have run out of slots and it falls back to being \boldsymbol the whole point of that package is to avoid accessing bold by a new math version. Mar 12 '15 at 8:32
• @Elias added an example using 20 math fonts in a single version Mar 12 '15 at 21:52
• @DavidCarlisle possibly related github.com/phst/lualatex-math/issues/7 (there are some issues I never understood, and my posts on the linked page were not clear even to me).
– user4686
Mar 12 '15 at 22:35