[Tex/LaTex] Check math font

fontsmath-mode

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?

Best Answer

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