6

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?

0

4 Answers 4

8

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

1
  • 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
    Commented Oct 2, 2016 at 23:16
7

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.

7
  • 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
    Commented Oct 2, 2016 at 21:26
  • @GustavoMezzetti I didn't want to go into the finer detail.
    – egreg
    Commented Oct 2, 2016 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
    Commented Oct 2, 2016 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
    Commented Oct 2, 2016 at 23:13
  • 1
    @Maëlan Yes, LaTeX does \let\mathgroup\fam. It seems that the new name was introduced with NFSS.
    – egreg
    Commented May 19, 2022 at 22:44
2

When Unicode Math font is used then it is possible (and it is typical) to have single math font for all characters used in math formulae. This differs from the old concept (with 7bit fonts) where the set of all math characters were divided to more fonts registered by math-family numbers. This old concept was explained in another answers here.

For example, OpTeX reserves the math-family number 1 for main Unicode math font. So, you can get its name by

\fontfam[lm]
\fontname\textfont1
\bye

Other math-family numbers can be used for additional fonts. It is usable if the main Unicode math font doesn't include all desired characters needed in math formulae.

0

This answer covers the case the user uses LaTeX.

Saying "there's no current math font" is half-accurate in that

  • in the low level of TeX the \textfont etc. are not set in advance, but
  • users of (La)TeX usually doesn't care too much about the low-level.

In reality, what happens is that LaTeX maintains an internal table of,

  • the current mathversion in \math@version (normal or bold) taken here,
  • for each math version (e.g. bold, normal, or if you use unicode-math and load appropriate fonts you may get mathversions like times or xitsmath etc.),
    • a mapping from math alphabet (e.g. \mathbf) to a text font name exclude the size -- that is, ⟨encoding, family, series, shape⟩ e.g. \mathbf → (OT1, cmr, bx, n⟩
    • a mapping from each ⟨sym-font⟩ to a text font name as above. e.g. symbols → ⟨OMS, cmsy, m, n⟩
  • in addition, a size information is kept (set by \DeclareMathSizes)

In order to access these information, you can access some internal macros of LaTeX -- whose names can be found by reading the documentation:

  • the mapping "(math version, math alphabet) → font" is modified by \DeclareMathAlphabet (set font of math alphabet for all math version), \SetMathAlphabet (set font of math alphabet for a specific math version), \DeclareSymbolFont, \SetSymbolFont.
  • the command to define which ⟨sym-font⟩ a symbol belong to is \DeclareMathSymbol (there's also \DeclareMathDelimiter etc.)

Of course, accessing these programmatically is quite fragile, but then the LaTeX kernel has been fixed for a very long time anyway.

So for example

$ latexdef math@version

\math@version:
macro:->normal

$ latexdef alpha  # check which symbol font "\alpha" belong to

\alpha:
\mathchar"10B

# ⟹ actually it's not unambiguously defined, we only know it belong to some symbol font
# which correspond to mathgroup 1

$ latexdef symletters   # this shows ⟨sym-font⟩ = "letters" → mathgroup 1

\symletters:
\char"1

$ latexdef mathbf  # check the math alphabet "mathbf" (→ OT1/cmr/bx/n)

\mathbf:
macro:->\protect \mathbf  


\mathbf :
macro:->\select@group \mathbf  \M@OT1 \OT1/cmr/bx/n 

$ latexdef "mv@bold"  # check the math version "bold" (self explanatory)

\mv@bold:
macro:->\getanddefine@fonts \symoperators \OT1/cmr/bx/n \getanddefine@fonts \symletters \OML/cmm/b/i
t \getanddefine@fonts \symsymbols \OMS/cmsy/b/n \getanddefine@fonts \symlargesymbols \OMX/cmex/m/n \
install@mathalphabet \mathbf  {\select@group \mathbf  \M@OT1 \OT1/cmr/bx/n }\install@mathalphabet \m
athsf  {\select@group \mathsf  \M@OT1 \OT1/cmss/bx/n }\install@mathalphabet \mathit  {\select@group 
\mathit  \M@OT1 \OT1/cmr/bx/it }\install@mathalphabet \mathtt  {\select@group \mathtt  \M@OT1 \OT1/c
mtt/m/n }

$ latexdef group@list  # list of all math groups defined so far (available in preamble only)

\group@list:
macro:->\@latex@error {Can be used only in preamble}\@eha 


(in preamble)
\group@list:
macro:->\group@elt \symoperators \OT1/cmr/m/n \group@elt \symletters \OML/cmm/m/it \group@elt \symsy
mbols \OMS/cmsy/m/n \group@elt \symlargesymbols \OMX/cmex/m/n 

# so "letters" → OML/cmm/m/it etc.

Another thing one may want to check is whether you're inside \mathbf or outside, etc.

While each symbol font is fixed to a mathgroup number as shown above, the allocation of mathalphabet → mathgroup number is on-demand:

fntguide documentation

(which you can test yourself: if you do $\mathbf{\the\fam} \mathit{\the\fam}$ at the very start of the document you get mathbf → 4 and mathit → 5, but if you do $\mathit{\the\fam} \mathbf{\the\fam}$ first then mathit → 4 and mathbf → 5 instead. This may be dependent on which LaTeX version is used however. -- LaTeX can afford to do this because you can set e.g. \textfont4 halfway through the formula and the whole math formula is rendered using the setting in effect at the end of the formula)

Because the mapping to \mathgroup is not fixed, we need to know where the internal lookup table is defined as well.

For this, reading the source code of \select@group is useful. The documentation may be a bit misleading though (e.g. it says the command takes 4 arguments while in fact it takes just 3)

select@group documentation

So the mapping is in the command definition itself after the command is used the first time.

%! TEX program = pdflatex
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}

\expandafter\meaning\csname mathbf \endcsname

\makeatletter\the\c@mv@normal\makeatother

$\mathbf{a}$

\makeatletter\the\c@mv@normal\makeatother

\expandafter\meaning\csname mathbf \endcsname

\end{document}

You see:

output

which means:

  • initially, \mathbf is not assigned to any mathgroup. When it's first called \select@group will be used to choose a mathgroup.
  • initially, 4 math groups are used.
  • after typesetting the bold a in math mode, 5 math groups are used.
  • now \mathbf is assigned to mathgroup 4.

Unfortunately this answer does not (yet) cover the case where unicode-math package is used -- it has the concept of \setmathfont also available as \mathversion.


For some useful readings, refer to texdoc fntguide as well as the documents/books linked from there:

fntguide documentation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .