# What is the easiest way to circumvent TeX's restriction on 16 math fonts?

I need to use math symbols from different fonts, exceeding the count of 16. What is the easiest way to adapt my setup to be able to use more math fonts?

I'm looking for either of the following:

• hacks that let me cheat around the restriction
• another setup that is likely to be most compatible with what I'm currently using, namely pdflatex

I know that there are systems/engines such as LuaTeX and XeTeX (both allowing way more than 16 math fonts), but I'm really looking for something that is as conservative as possible an extension of LaTeX. The reason is that I'm using a lot of packages written for LaTeX, and switching over to something like XeTeX for my current project is likely to break things in subtle ways.

If the answer is "there is no way", that's also okay.

Here is a minimal example of uncompilable code:

\documentclass{article}
\usepackage{amsmath} % for the \mspace command and other things (necessity of this package not demonstrated in this minimal example)
\usepackage{dsfont} % for the \mathds command
\usepackage{txfonts} % for \coloneqq; also: if I omit this line, I get an error message saying that "Command \lhd not provided in base LaTeX2e" (I need \lhd)
\usepackage[only,llbracket,rrbracket,llparenthesis,rrparenthesis]{stmaryrd} % imports: [| |] (| |)
\usepackage[mathb]{mathabx} % for \sqbullet and \blackdiamond

% for \lcurvearrowdown:
\DeclareFontFamily{U}{MnSymbolA}{}
\DeclareFontShape{U}{MnSymbolA}{m}{n}{
<-6> MnSymbolA5
<6-7> MnSymbolA6
<7-8> MnSymbolA7
<8-9> MnSymbolA8
<9-10> MnSymbolA9
<10-12> MnSymbolA10
<12-> MnSymbolA12}{}
\DeclareFontShape{U}{MnSymbolA}{b}{n}{
<-6> MnSymbolA-Bold5
<6-7> MnSymbolA-Bold6
<7-8> MnSymbolA-Bold7
<8-9> MnSymbolA-Bold8
<9-10> MnSymbolA-Bold9
<10-12> MnSymbolA-Bold10
<12-> MnSymbolA-Bold12}{}
\DeclareSymbolFont{MnSyA}{U}{MnSymbolA}{m}{n}
\DeclareMathSymbol{\lcurvearrowdown}{\mathrel}{MnSyA}{187}

% for \sqsubseteq and \nsqsubseteq; I don't like the appearance of these symbols from txfonts, so I am using the respective MnSymbol ones, except I'm naming them \sqeq and \nsqeq
% THIS IS THE CODE BLOCK THAT FIRST MADE MY FILE UNCOMPILABLE WHEN I ADDED IT
\DeclareFontFamily{U}{MnSymbolD}{}
\DeclareFontShape{U}{MnSymbolD}{m}{n}{
<-6>  MnSymbolD5
<6-7>  MnSymbolD6
<7-8>  MnSymbolD7
<8-9>  MnSymbolD8
<9-10> MnSymbolD9
<10-12> MnSymbolD10
<12->   MnSymbolD12}{}
\DeclareFontShape{U}{MnSymbolD}{b}{n}{
<-6>  MnSymbolD-Bold5
<6-7>  MnSymbolD-Bold6
<7-8>  MnSymbolD-Bold7
<8-9>  MnSymbolD-Bold8
<9-10> MnSymbolD-Bold9
<10-12> MnSymbolD-Bold10
<12->   MnSymbolD-Bold12}{}
\DeclareSymbolFont{MnSyD}{U}{MnSymbolD}{m}{n}
\DeclareMathSymbol{\sqeq}{\mathrel}{MnSyD}{90}
\DeclareMathSymbol{\nsqeq}{\mathrel}{MnSyD}{210}

\begin{document}

$$\mathds{N}$$ % WITHOUT THIS LINE, THE CODE COMPILES

$$\coloneqq$$ % this line is not required to get a minimal example of uncompilable code, but I need this symbol

$$\llbracket,\rrbracket,\llparenthesis,\rrparenthesis$$ % this line is not required to get a minimal example of uncompilable code, but I need these symbols

$$\sqbullet, \blackdiamond$$ % this line is not required to get a minimal example of uncompilable code, but I need these symbols

$$\lcurvearrowdown$$ % this line is not required to get a minimal example of uncompilable code, but I need this symbol

$$a \sqeq b \nsqeq c$$ % this line is not required to get a minimal example of uncompilable code, but I need these symbols

$$\mathit{abc}$$ % WITHOUT THIS LINE, THE CODE COMPILES

$$\mathsf{a}$$ % WITHOUT THIS LINE, THE CODE COMPILES

\end{document}


(I know this is an arbitrary example, but I think it serves the purpose of illustrating the issue.)

-
There is no way. If you define a math alphabet for a few symbols there's the possibility to avoid allocating one, but if the usage is extensive, the overhead would be noticeable. –  egreg Jul 19 '12 at 12:12
do you need more than 16 fonts in one expression ? because it is possible (if tiresome) to shuffle the assignments so that each expression has a different set of 16 fonts –  David Carlisle Jul 19 '12 at 13:00
@DavidCarlisle I only need a couple per expression, though as a casual user I do not exactly know which mathfonts I am using for which symbols. I am currently at the limit, having loaded many necessary packages, and when I follow the advice here to load the \sqsubseteq and \nsqsubseteq symbols from the MnSymbolD font, I get the error Too many math alphabets used in version normal. –  Lover of Structure Jul 20 '12 at 2:19
As always questions are much easier to answer if the question contains a complete (small) working (or not) document. Please edit the question with a document with the minimal number of packages you have loaded to get this error and a couple of math expressions you need to typeset. 16 is in most cases more than enough (I don't think I have had this error on a real as opposed to test) document more than a couple of times in 25 years of tex use. –  David Carlisle Jul 20 '12 at 8:21
@user14996 You are in the situation described by Peter Breitfeld: don't allocate a math symbol font just for a few symbols. –  egreg Jul 21 '12 at 9:35

## 4 Answers

If you don't need the whole font, but only some glyphs you can call them directly with usefont. This doesn't increase the number of math fonts:

\documentclass[a4paper,11pt]{article}
\usepackage{mathpazo,
amsmath}               %to use \text
\newcommand{\cmvarpi}{\text{\usefont{OML}{cmr}{m}{it}\symbol{36}}}
\begin{document}
\LARGE
$x=\varpi=\cmvarpi=y$
\end{document}


-
But this will break accent and sub/superscript positing of those symbols, can't be used with big operators and so on, so it is of very limited utility. –  Khaled Hosny Jul 19 '12 at 12:27

There is no way. Even switching to engines that allow more than 16 families will not automatically help you, because the format of \mathchar (and similar math commands) can’t handle more than 16 families, so you will need to also switch to extended math commands (e.g. using unicode-math package).

-

Here I show how one can remove a predefined math alphabet; one can \UndeclareMathAlphabet{\mathtt}, for instance, thus saving one of them. Unfortunately, an approach with regexpatch is difficult for various reasons.

\documentclass{article}
\usepackage{xparse,l3regex}

\makeatletter
\ExplSyntaxOn
\NewDocumentCommand{\UndeclareMathAlphabet}{m}
{
\tl_set:Nx \l_tmpa_tl {\exp_not:N \install@mathalphabet \exp_not:c {\cs_to_str:N #1~}}
\regex_replace_once:nnNT { \u{l_tmpa_tl} \cB. \c[^BE].* \cE. } {} \mv@normal { \addtocounter{mv@normal}{-1} }
\regex_replace_once:nnNT { \u{l_tmpa_tl} \cB. \c[^BE].* \cE. } {} \mv@bold { \addtocounter{mv@bold}{-1} }
}
\ExplSyntaxOff
\@onlypreamble\UndeclareMathAlphabet
\makeatother

\typeout{\expandafter\meaning\csname mv@normal\endcsname}
\typeout{\expandafter\meaning\csname mv@bold\endcsname}
\UndeclareMathAlphabet{\mathtt}
\typeout{\expandafter\meaning\csname mv@normal\endcsname}
\typeout{\expandafter\meaning\csname mv@bold\endcsname}
\UndeclareMathAlphabet{\mathsf}
\typeout{\expandafter\meaning\csname mv@normal\endcsname}
\typeout{\expandafter\meaning\csname mv@bold\endcsname}


The output is

macro:->\getanddefine@fonts \symoperators \OT1/cmr/m/n \getanddefine@fonts \sym
letters \OML/cmm/m/it \getanddefine@fonts \symsymbols \OMS/cmsy/m/n \getanddefi
ne@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\select
@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathsf  {\select@g
roup \mathsf  \M@OT1 \OT1/cmss/m/n }\install@mathalphabet \mathit  {\select@gro
up \mathit  \M@OT1 \OT1/cmr/m/it }\install@mathalphabet \mathtt  {\select@group
\mathtt  \M@OT1 \OT1/cmtt/m/n }
macro:->\getanddefine@fonts \symoperators \OT1/cmr/bx/n \getanddefine@fonts \sy
mletters \OML/cmm/b/it \getanddefine@fonts \symsymbols \OMS/cmsy/b/n \getanddef
ine@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\selec
t@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathsf  {\select@
group \mathsf  \M@OT1 \OT1/cmss/bx/n }\install@mathalphabet \mathit  {\select@g
roup \mathit  \M@OT1 \OT1/cmr/bx/it }\install@mathalphabet \mathtt  {\select@gr
oup \mathtt  \M@OT1 \OT1/cmtt/m/n }
macro:->\getanddefine@fonts \symoperators \OT1/cmr/m/n \getanddefine@fonts \sym
letters \OML/cmm/m/it \getanddefine@fonts \symsymbols \OMS/cmsy/m/n \getanddefi
ne@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\select
@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathsf  {\select@g
roup \mathsf  \M@OT1 \OT1/cmss/m/n }\install@mathalphabet \mathit  {\select@gro
up \mathit  \M@OT1 \OT1/cmr/m/it }
macro:->\getanddefine@fonts \symoperators \OT1/cmr/bx/n \getanddefine@fonts \sy
mletters \OML/cmm/b/it \getanddefine@fonts \symsymbols \OMS/cmsy/b/n \getanddef
ine@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\selec
t@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathsf  {\select@
group \mathsf  \M@OT1 \OT1/cmss/bx/n }\install@mathalphabet \mathit  {\select@g
roup \mathit  \M@OT1 \OT1/cmr/bx/it }
macro:->\getanddefine@fonts \symoperators \OT1/cmr/m/n \getanddefine@fonts \sym
letters \OML/cmm/m/it \getanddefine@fonts \symsymbols \OMS/cmsy/m/n \getanddefi
ne@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\select
@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathit  {\select@g
roup \mathit  \M@OT1 \OT1/cmr/m/it }
macro:->\getanddefine@fonts \symoperators \OT1/cmr/bx/n \getanddefine@fonts \sy
mletters \OML/cmm/b/it \getanddefine@fonts \symsymbols \OMS/cmsy/b/n \getanddef
ine@fonts \symlargesymbols \OMX/cmex/m/n \install@mathalphabet \mathbf  {\selec
t@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \mathit  {\select@
group \mathit  \M@OT1 \OT1/cmr/bx/it }


which shows that both \mathtt and \mathsf have been removed successfully.

-

I deleted my first answer. It is possible to modify LaTeX's internal math structures, but as you noted I hot it wrong (and haven't time to fix at present) @egreg has a more robust version.

However to return to the theme of using text fonts to avoid using up the 16 math font slots. perhaps the fonts for which the lack of math setup are the math alphabets rather than the symbol fonts. If you add

 \def\mathsf#1{\text{\textsf{#1}}}


to your preamble then \mathsf will use the same font as before but via a nested text box, and your file processes without error.

-
Will the result be similar to using to using a "proper" math font though? –  mSSM Jul 22 '12 at 0:04
the font will be identical as will the horizontal spacing, what you mostly lose is positioning of accents and subscripts as \dot{\mathsf{a}} or \mathsf{a}_1 can use the font metric information to adjust the positioning but after the redefinition they are being applied to a box like difference between \mathrm{a}_1 and \mbox{a}_1. but in many documents \mathsf or \mathtt just used for identifiers only occasionaly used with scripts –  David Carlisle Jul 22 '12 at 0:33
I think this is a fantastic solution, thank you! –  Joe Corneli Jun 1 '13 at 12:51