Get math symbols from main font in lualatex

fontspecunicode-math

I'm trying to use a few math symbols from my main font as described here:
How to set a font everywhere (including math mode) in XeLaTeX? (I'm using lualatex.)

(I don't have to use that method, if there's a better one.)

However, my main font is set up in a complicated way, largely to accommodate regular, italic, bold, etc. fonts that are in different files. There are also different files for different optical sizes. SourceSerif4 comes in 60 .otf files!

Below I've a MWE that just uses 4 of those 60: regular and italic in two optical sizes.

I define "myfont" with \defaultfontfeatures, and then set that as the main font with \setmainfont{myfont}, and all works well.

But I cannot do the same with \setmathfont{myfont}. I get the error:

! Package fontspec Error: The font "myfont" cannot be found.

All this works if I use \setmathfont{SourceSerif4-Regular} instead of myfont. But then I would lose the optical size selection.

Sorry I don't know how to reproduce this with a font that comes with Texlive (though SourceSerif4 is free here: https://github.com/adobe-fonts/source-serif)

Is there a way I can get the SourceSerif4 font used in math? I'm not really going to do much math, I mostly want the prime symbol, +, -, and =. (And digits and letters)

MWE:

\documentclass{article}
\usepackage{fontspec}
\usepackage{unicode-math}
\defaultfontfeatures[myfont]{
  Path        = /Users/ded/Library/Fonts/,
  Numbers     = {Proportional,OldStyle},
  UprightFont = SourceSerif4-Regular ,
  UprightFeatures={
    SizeFeatures={
      {Size={-9.5}, Font=SourceSerif4SmText-Regular},
      {Size={9.5-}, Font=SourceSerif4-Regular},
    },
  },
  ItalicFont = SourceSerif4-It ,
  ItalicFeatures={
    SizeFeatures={
      {Size={-9.5}, Font=SourceSerif4SmText-It,
                    SmallCapsFont=SourceSerif4SmText-Regular},
      {Size={9.5-}, Font=SourceSerif4-It,
                    SmallCapsFont=SourceSerif4-Regular},
    },
    SmallCapsFeatures={Letters=SmallCaps, FakeSlant=0.2},
  },
  Ligatures = TeX ,
  Extension = .otf
}
\setmainfont{myfont}
\setmathfont[slash-delimiter=frac]{latinmodern-math.otf}
\setmathfont[range={"0000-"FFFF}]{myfont}                 % error is here
\begin{document}
The quick brown fox jumps over the lazy dog.
$2 + 2 = 4$
\end{document}

Best Answer

You don’t say what isn’t working, but [range={"0000-"FFFF} is wrong. This will overwrite the entire Basic Multilingual Plane with a font that has no OpenType math table.

You almost certainly want to use \setmathfont{My Font italic}[range=it] for italic math variables, or [range=up] for upright, bfup for bold upright, and bfit for bold italic. (To use an upright font by default in math mode, load unicode-math with the [math-style=upright] option.)

You might need to add the font options for the Italic face, such as optical sizes. Either add them as options along with range, or use \defaultfontfeatures{My Font Italic}.

Update

Based on your comment, you want something like (untested)

\setmathfont{SourceSerif4-It}[
  range=it,
  SizeFeatures={
    {Size={-9.5}, Font=SourceSerif4SmText-It},
    {Size={9.5-}, Font=SourceSerif4-It}
  },
  Extension=.otf]

The other options are irrelevant to math mode. The default font for math variables is \symit, not upright. If you also want to use \symup or \usepackage[math-style=upright]{unicode-math}, you would declare a corresponding command for range=up. If you are using bold for vectors, you might want to set a command for range=bfit or range=bfup as well.