[Tex/LaTex] Context : How to install and use new opentype (math) fonts

contextmath-fontsopentype

Motivation : I'm a die-hard user of LaTeX which is intrigued by ConTeXt and try to learn enough of it to be able to judge on its usability. For now, I'm limiting myself to Mark IV/LuaTeX (Mark II seems to have stopped evolving). I use the current TeXLive distribution (as packaged by Debian).

I recently stumbled on an unexpected difficulty : Opentype math fonts. The current situation is that a lot of the (few) available Opentype Math fonts can be used "out of the box" ; a bit of exploration shows that some support scripts for these fonts exists in $TEXMF/tex/context/fonts/mkiv/.

However, I have been unable to read, guess or divine the goals or structure of these files. As a consequence, I have been unable to cajole Context to use the following couples of Opentype tex / Opentype math fonts, which have recently become available :

  • GFS Neohellenic / GFS Neohellenic Math (CTAN)
  • Fira Sans / Fira Math
  • STIX Two family (CTAN).

I have been able to use the text fonts ; for example :

\definefontfamily [gfsneohellenic] [serif] [GFSNeohellenic]
\setupbodyfont [gfsneohellenic]

but any attempt to use math fails early. However, using the first two of these may allow to get a correct typesetting of maths with a sans typeface consistent with the text font (see this question for amplification…)

[ On the other hand, using them in LuaLaTeX is a piece of cake : link them to the "right" place, re-run font-cache, and you're set… ]

I am not (yet) familiar enough with the ConTeXt documentation to understand the mkiv/.lfg scripts which seem to be the origin of ConTeXt support for these math fonts.

Question(s) : Where should I look for documentation of this feature ? Or is there some "magic" script allowing for the creation of such scripts ?

Best Answer

Where should I look for documentation of this feature?

There is a manual which is distributed with ConTeXt standalone, called Fonts out of ConTeXt. It is also sometimes referred to as “the new font manual”.

Or is there some "magic" script allowing for the creation of such scripts?

No, there is no magic script to generate these so-called typescripts. But the structure is straight-forward and it is really easy to roll your own.

The procedure is always the same. You define a typescript for a certain family and assign font files to predefined names. For example

\starttypescript [sans] [fira]
  \definefontsynonym [Sans] [file:FiraSans-Regular.otf] [features=default]
\stoptypescript

This tells ConTeXt that when the Sans version is requested it should load FiraSans-Regular.otf and apply the default font features to it.

The .lfg files (Lua Font Goodies) are more complicated but you mostly need those if you have to patch math fonts, you want to expose special math font features to ConTeXt, or you have to define virtual fonts. In a first approximation they are usually not needed.

Here is a full, yet simple example for the Fira font.

\starttypescriptcollection [fira]

  \starttypescript [sans] [fira]
    \setups[font:fallback:sans]
    \definefontsynonym [Sans]           [file:FiraSans-Regular.otf]       [features=default]
    \definefontsynonym [SansItalic]     [file:FiraSans-RegularItalic.otf] [features=default]
    \definefontsynonym [SansBold]       [file:FiraSans-Bold.otf]          [features=default]
    \definefontsynonym [SansBoldItalic] [file:FiraSans-BoldItalic.otf]    [features=default]
    \definefontsynonym [SansCaps]       [file:FiraSans-Regular.otf]       [features={default,smallcaps}]
  \stoptypescript

  \starttypescript [mono] [fira]
    \setups[font:fallback:mono]
    \definefontsynonym [Mono]     [file:FiraMono-Medium.otf] [features=default]
    \definefontsynonym [MonoBold] [file:FiraMono-Bold.otf]   [features=default]
  \stoptypescript

  \starttypescript [math] [fira]
    \definefontsynonym [MathRoman] [file:Fira-Math.otf] [features=mathextra]
  \stoptypescript

  \starttypescript [fira]
    \definetypeface [\typescriptone] [rm] [serif] [modern] [default]
    \definetypeface [\typescriptone] [ss] [sans]  [fira]   [default]
    \definetypeface [\typescriptone] [tt] [mono]  [fira]   [default]
    \definetypeface [\typescriptone] [mm] [math]  [fira]   [default]
    \quittypescriptscanning
  \stoptypescript

\stoptypescriptcollection

\setupbodyfont[fira,sans]

\starttext

\input knuth

\startformula
  R_{\mu\nu} - \frac{1}{2} R g_{\mu\nu} + \Lambda g_{\mu\nu} = \frac{8 \pi G}{c^4} T_{\mu\nu}
\stopformula

\starttyping
#include <stdio.h>

int main() {
    printf("Hello World!\n");
}
\stoptyping

\stoptext

enter image description here

Related Question