9

How can I query informations about the current math font?

I tried the following in a document using unicode-math and Cambria Math font

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{Cambria Math}

\begin{document}
Try to get math font name inside math mode.
\[
  \directlua{
    local font = fonts.identifiers[font.current()]
    local fullname = font.fullname
    tex.sprint(fullname)
  }
\]
\end{document}

However, it is the font name of surrounding text font being printed in the math font, as seen in the following picture. enter image description here

It seems that font.current() returns the current text font id instead of the math font even the code is inside math mode.

Next, I serialized the fonts.identifiers table and get the id of math font. I replaced font.current() with an explicit number, 28 in this example, and the above code print the math font name correctly, as seen in the following. enter image description here

So I guess the math font does have an ID in the fonts.identifiers table. But how do I get this ID automatically?

6
  • There is no notion for the current math font in TeX and, I believe, also in LuaTeX; fonts are assigned at the end of the formula using the concept of math family. In standard TeX, for example, $a$ and $\Gamma$ are selected from different fonts.
    – egreg
    Commented Sep 25, 2011 at 16:27
  • @egreg thanks for the comment. I modified the question to make it clear that I was using unicode opentype math font with the unicode-math package. I should have included this info when I ask the question. Does this make any difference?
    – Yan Zhou
    Commented Sep 25, 2011 at 16:44
  • No, it doesn't.
    – egreg
    Commented Sep 25, 2011 at 16:46
  • @egreg but should not the math font used be contained in lua's font table or somewhre? For example, after serialize the fonts.identifier table, I found that key 38 is a table containing infos, etc of Cambria math. Shouldn't i be able to find this number 38 without serialize the fonts table and look into it manually?
    – Yan Zhou
    Commented Sep 25, 2011 at 16:58
  • @egreg Could you make your comment into an answer? (I'd not that Unicode adds 'extra fun' to math fonts, as standard LaTeX at least has a notion of 'bold math', whereas in Unicode this is a property of the symbol, and so you can't even pick that up!)
    – Joseph Wright
    Commented Sep 25, 2011 at 18:16

1 Answer 1

12

There is no concept of "current math font" in TeX and the same is true for LuaTeX and XeTeX. The symbols are typeset using fonts assigned at the end of the formula using the concept of math family.

However, it's possible to access at the math font chosen via \setmathfont making it the current text font in a slighly tricky way:

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{XITS Math}
\begin{document}
\sbox0{$$}\the\textfont2

\directlua{
  local font = fonts.identifiers[font.current()]
  local fullname = font.fullname
  tex.sprint(fullname)
}

\end{document}

This will print "XITS Math-2". This is because \the\textfont2 produces a font identifier that makes the font in family 2 the current text font. In traditional TeX, \textfont2 is the font used for the most common symbols in text size; unicode-math uses it for all the known symbols, but for technical reasons it has to maintain a copy of it also as \textfont3, hence the suffix "2".

The \sbox0{$$} is needed because LaTeX doesn't actually assign fonts to math families until it has to typeset a formula. It would be possible also to say

\makeatletter\check@mathfonts\makeatother

because \check@mathfonts is the command used to assign fonts to families based on the current context.

Other Lua facilities can be used to access the font number in the internal tables, I guess.

You must log in to answer this question.

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