1

How it is possible to check the math font loaded in a document? Like \fontname\font is used to check the font loaded, how is the command for printing the current math font?

4

Font selection in math mode works differently from that in text mode. While in text mode you have a "current font", you don't necessarily have in math mode. In math mode TeX uses one of at most 16 font families, where each family has three variants \textfont, \scriptfont and \scriptscriptfont, used for the various positions characters can occur in in math formulas.

When TeX sets a character in math mode, it first of all checks the math code associated with that character. You can check the math code e.g. for the letter a with \showthe\mathcode`a. TeX will then print

*\showthe\mathcode`a
> 29025.

which in hexadecimal form is 7161. The fist digit stands for the math class associated with the math character, the second digit for the font family and the third and fourth for the position of the character in the font table. So in this case, the letter for a has the default font family 1.

However, it's more complicated than just using this font family because the character also has class 7. Class 7 means that TeX first checks if the value of the font family register \fam is in the range 0-15. If so, TeX assumes the class to be 0 instead (an ordinary math character/symbol), and uses the font family \fam instead for typesetting this character. If \fam isn't in said range (as it is by default when math mode is started), TeX uses the associated font family for the letter, 1 in this case.

Now that we know at which font family to look at, we can finally check the font names for each of the three family variants in math mode. If we put

$\fontname\textfont1, \fontname\scriptfont1, \fontname\scriptscriptfont1$

in a simple LaTeX document, the output will look like

cmmi10, cmmi7, cmmi5

which are the standard Computer Modern italic math font variants.

The font families are only set in math mode, so if you need the value in text mode, use something like

$\xdef\famone{\fontname\textfont1}$
\ttfamily\famone

and similarily for the script and scriptscript variants.


Edit: Here is a small macro \printmathfont that prints the fontname of the given math font variant (#1) and a (possibly implicit) math character (#2):

\documentclass{article}

\def\printmathfont#1#2{%
  \ifcat\relax#2\relax
    \count0=#2%
  \else
    \count0=\mathcode`#2%
  \fi
  \divide\count0 256%
  \count1=\numexpr\count0/16\relax                % class
  \count0=\numexpr\count0 - 16*(\count0/16)\relax % family
  \def\tempinfo##1{\xdef\temp{\fontname#1##1}}%
  $%
  \ifnum\count1=7\relax
    \ifnum\fam>0\relax
      \ifnum\fam<16\relax
        \tempinfo{\fam}%
      \else
        \tempinfo{\count0}%
      \fi
    \else
      \tempinfo{\count0}%
    \fi
  \else
    \tempinfo{\count0}%
  \fi
  $%
  \string#1 \string#2 ($#2$): \temp
}

\begin{document}

\ttfamily
\printmathfont\textfont a \par
\printmathfont\scriptfont a \par
\printmathfont\textfont\alpha \par
\printmathfont\textfont\cap \par
\printmathfont\textfont= \par
\string\bf\everymath{\bf}\printmathfont\textfont a \par
\string\sf\everymath{\sf}\printmathfont\textfont a \par

\end{document}

The output looks like this: enter image description here

  • Very good answer, indeed; but, notwithstanding this, I’ve chosen to upvote @egreg’s as more complete and detailed. I recall that some “meta” discussion suggested that upvoting all answers to a question is contrary both to the spirit and the purpose of Stack Exchange sites. – GuM Oct 2 '16 at 23:16
4

There is no “current math font”. Basically, each symbol in a math formula has attached to it a font choice.

Each character has a “mathcode”, for instance the mathcode of b is 0x7162 which means “typeset, as ordinary symbol, the character sitting at slot 0x62 of the font pointed at by the current value of \mathgroup or, if \mathgroup is −1, use the font defined by \mathgroup 1”. The mathcode of ; is 0x603B which means “typeset, as punctuation symbol, the character sitting at slot 0x3B of the font pointed at by \mathgroup 0”.

TeX maintains up to 16 math groups (families in plain TeX terminology, but here I'm talking LaTeX), but the limitation is raised to 256 in XeTeX and LuaTeX.

Each math group can be assigned up to three fonts; if the family number is n, we can refer to them by “\textfontn”, “\scriptfontn” and “\scriptscriptfontn”; as the names suggest, the different fonts are used in for symbol at subscript/superscript level zero, one or above respectively. So the formula $b^{b}$ will use two different fonts, namely \textfont1 and \scriptfont1, respectively for the base and the exponent.

On the other hand, $\mathrm{b}^{b} will again use two different fonts, but now they'll be \textfont0 for the base and \scriptfont1 for the exponent, because \mathrm{b} is an instruction that basically does

\begingroup\mathgroup=0 b\endgroup

thus temporarily setting \mathgroup to 0; the parameter's value when a formula is started is −1. In general, it is to be expected that the fonts assigned to the three variants for a math group differ just by the size, for obvious reasons.

There are other subtle points. There are two basic methods for assigning fonts to math groups:

\DeclareSymbolFont{<name>}{<encoding>}{<family>}{<series>}{<shape>}
\DeclareMathAlphabet{<control sequence>}{<encoding>}{<family>}{<series>}{<shape>}

The declarations

\SetSymbolFont{<name>}{<version>}{<encoding>}{<family>}{<series>}{<shape>}
\SetMathAlphabet{<control sequence>}{<version>}{<encoding>}{<family>}{<series>}{<shape>}

are the companions for the math version; I'll give no details about math versions and I'll stick to the “normal” math version (but it's not difficult to understand what happens).

Math groups 0, 1, 2 and 3 are reserved, in the sense that they are used by TeX for setting important math parameters; group 0 should refer to the “math roman” font, group 1 to the “math letters” font, group 2 to the “math symbol” font and group 3 to the “math extensions” font. Indeed, in fontmath.ltx we find

\DeclareSymbolFont{operators}   {OT1}{cmr} {m}{n}
\DeclareSymbolFont{letters}     {OML}{cmm} {m}{it}
\DeclareSymbolFont{symbols}     {OMS}{cmsy}{m}{n}
\DeclareSymbolFont{largesymbols}{OMX}{cmex}{m}{n}

as the first four declarations. Every package for math fonts should stick to this convention (but the assignment to the four font properties is free, provided the fonts assigned to math groups 1, 2 and 3 are designed for the respective roles, because they need proper parameters).

Every subsequent \DeclareSymbolFont declaration will allocate another math group. To the contrary, math groups corresponding to \DeclareMathAlphabet are only allocated on demand.

For instance, the document

\documentclass{article}
\begin{document}
$\mathbf{a}$ \fontname\textfont4
\end{document}

will print

a cmbx10

However, changing $\mathbf{a}$ into$a$` will print

a nullfont

The math group corresponding to \mathbf is not predictable, because it depends on what symbol font are allocated (for instance, amssymb will allocate more than the standard four) and on the order math alphabets are used. Continuing our example,

\documentclass{article}
\begin{document}
$\mathbf{a\xdef\whatfont{\fontname\textfont\mathgroup\space(math group \the\mathgroup)}}$
\whatfont
\end{document}

will print

a cmbx10 (math group 4)

but if we also load amssymb, we get

a cmbx10 (math group 6)

If we do

\documentclass{article}
\usepackage{kpfonts}
\begin{document}
$\mathbf{a\xdef\whatfont{\fontname\textfont\mathgroup\space(math group \the\mathgroup)}}$
\whatfont
\end{document}

we get

a jkpbn7t (math group 9)

exactly because kpfonts allocates more symbol fonts.

The other important subtle point is that before a math formula is typeset, no math font is declared for any math group, not even the four basic ones. There is a very good reason for this, as shown in the following example:

\documentclass{article}
\begin{document}
\footnotesize $a$ \fontname\textfont1
\end{document}

that will print

a cmmi8

Entering math mode triggers execution of \check@mathfonts which is responsible of going through the list of already allocated math groups and to provide the correct font assignments. The example

\documentclass{article}
\begin{document}
$a$ \footnotesize \fontname\textfont1
\end{document}

will print

a cmmi10

because no math formula has yet been typeset in \footnotesize. At begin document, no font has been assigned yet, which explains the “nullfont” above.

Note, finally, that in a formula such as

$a\oplus\mathrm{a}\bigg|$

four fonts are used; in order, \textfont1, \textfont2, \textfont0 and \textfont3.

  • When you refer to math group 1 as “need[ing] proper parameters”, do you intend to refer to the fact that \fontdimen2 is required to be null? – GuM Oct 2 '16 at 21:26
  • @GustavoMezzetti I didn't want to go into the finer detail. – egreg Oct 2 '16 at 21:49
  • I was asking because, when I edited your answer, I considered that statement for editing too, but I didn’t exactly because, in the sense specified above, it is correct. – GuM Oct 2 '16 at 23:10
  • @GustavoMezzetti Fonts for families 2 and 3 require several parameters; fonts in family 1 just need a particular setting. Going into the details would make an already long answer too long and boring. – egreg Oct 2 '16 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.