# Details on latex' math alphabets

I understand a bit how the nfss work for text fonts, but I have a hard time following the code in latex.ltx for math fonts (and the documentation in source2e.pdf is a bit sparse there). Hence I have some questions:

If I recursively look at the definition of eg \mathcal via latexdef I get

\mathcal : macro:->\relax \ifmmode \else \non@alpherr \mathcal  \fi \use@mathgroup \M@OMS \symsymbols
\use@mathgroup: macro:#1#2->\relax \ifmmode \math@bgroup \expandafter \ifx \csname M@\f@encoding \endcsname #1\else #1\fi \mathgroup #2\relax \expandafter \math@egroup \fi
\mathgroup: \fam

From my understanding \T@OMS and \M@OMS respectively are the loading code specified in \DeclareFontEncoding {encoding} {text-settings} {math-settings} and by default do nothing.

So essentially in math mode \mathcal switches to \M@OMS is OMS was not the current encoding already and uses the math family encoded by \symsymbols, which makes sense because \mathcal is defined via \DeclareSymbolFontAlphabet{\mathcal}{symbols}.

For \mathit this is essentially the same once it has been used, before it is \select@group \mathit \M@OT1 \OT1/cmr/m/it and a mathgroup with this font is created when \mathit is first used.

But I am missing something. For instance in \mathrm{ab-de} the - is still typeset as an operator rather than in the rm font. So I am guessing \mathrm only change the mathcode of some letters to use the corresponding symoprators math family, but not all of them, and I cannot find where in the source.

Second question: since \mathrm is defined via \DeclareSymbolFontAlphabet{\mathrm}{operators} what happen if we do both a \SetSymbolFont{operators}{bold}... and a \SetMathAlphabet\mathrm{bold}...? Are these equivalent, both changing the \symoperators math group in math version bold?

• Welcome to Tex StackExchange. You might want to look up what the TeX primitive \fam does in math mode. May 15, 2020 at 17:23

All the work of \mathrm eventually boils down to do \fam=n, where n stands for a one-byte integer (from 0 to 15), in a group. The default is 0, because \mathrm is defined via

\DeclareSymbolFontAlphabet{\mathrm}{operators}

and fontmath.ltx has

\DeclareSymbolFont{operators}{OT1}{cmr}{m}{n}

as its first declaration of a symbol font. Such declarations can later be overridden by font packages, but operators will always point to math family 0.

During the execution of \mathrm there is no mathcode change. It's a primitive feature of TeX that if a character has a mathcode "7xyz (the most significant byte is 7), then TeX will typeset the character in slot "yz of the font in the current math family, if the current value of \fam is between 0 and 15. Otherwise the character will use the font in math family "x.

Side note: LaTeX defines \mathgroup as an alias for \fam.

Indeed, we have in fontmath.ltx

\DeclareMathSymbol{a}{\mathalpha}{letters}{a}

and similarly for the other letters. This is LaTeX lingo for the corresponding declaration in plain TeX

\mathcodea="7197

because the declaration for letters is the second in fontmath.ltx, so corresponding to math family 1.

Since no math code is changed and - is declared as

\DeclareMathSymbol{-}{\mathbin}{symbols}{"00}

which corresponds to \mathcode`-="2300, it will not be affected by the current value of \fam.

The \operatorname macro of amsmath does indeed change math codes so that - appears as a real hyphen.

• Thank you, this is a very clear explanation. This explains why \mathrm only affects symbols of type \mathalpha. I should really read the TeX book someday. By the way I think you meant that letters corresponds to math family 1. May 16, 2020 at 10:56

Concerning my second question:

Second question: since \mathrm is defined via \DeclareSymbolFontAlphabet{\mathrm}{operators} what happen if we do both a \SetSymbolFont{operators}{bold}... and a \SetMathAlphabet\mathrm{bold}...? Are these equivalent, both changing the \symoperators math group in math version bold?

I can now answer. Using \SetSymbolFont{operators}{bold}... update the font for the operator symbol in bold version, and keeps the link between \mathrm and operators. However, doing \SetMathAlphabet\mathrm{bold}... makes \mathrm a new (standard) math alphabet. Indeed in this case the following snippet of SetMathAlphabet@ gets executed:

\edef\reserved@a{%
\noexpand\in@{\string\use@mathgroup}{\meaning#4}}%
\reserved@a
\ifin@ % if we were a symbol font alphabet
\def\reserved@b##1\use@mathgroup##2##3{%
\def\reserved@b{##3}\def\reserved@c{##2}}%
\expandafter\reserved@b#4% reserved@b now holds the value of the symbol math group, and reserved@c is M@enc
\begingroup
\def\install@mathalphabet##1##2{%
}% we copy the existing alphabets
\def\getanddefine@fonts##1##2{% we copy the existing symbol fonts
\ifnum##1=\reserved@b %if the symbol font is the one we are replacing, use it to set up the font of our new alphabet
\expandafter
\expandafter{\expandafter\install@mathalphabet
\expandafter#4\expandafter
{\expandafter\select@group\expandafter
#4\reserved@c##2}}%
\fi
}%
\def\version@elt##1{%
\toks@{}%
##1%
\xdef##1{\the\toks@}%
}%
\version@list
\endgroup
\expandafter\gdef\expandafter\alpha@list\expandafter
{\alpha@list
\alpha@elt #4\no@alphabet@error \no@alphabet@error}%
\gdef#4{\no@alphabet@error #5}% fake things :-)
\SetMathAlphabet@{#1}{#2}{#3}#4#5% set up the font requested for this version

So we see that we get a new \mathrm standard alphabet, without default (ie \no@alphabet@error) but the existing fonts definition for operators in the current math version get copied for \mathrm (and of course the version specified in SetMathAlphabet gets updated).

Consider the result of latexdef -o '\SetMathAlphabet{\mathrm}{bold}{T1}{lmr}{xb}{sc}' 'mv@bold', we get \install@mathalphabet \mathrm {\select@group \mathrm \M@T1 \T1/lmr/xb/sc } as expected. More interesting, the result of latexdef -o '\SetMathAlphabet{\mathrm}{bold}{T1}{lmr}{xb}{sc}' 'mv@normal' yields \mathrm {\select@group \mathrm \M@OT1 \OT1/cmr/m/n }. We see that the font for the new \mathrm alphabet in normal mode was copied from the value of the operators symbol font.