Search code examples
latexpandocquartoxelatex

Letters and numbers in math do not use "mainfont" nor "mathfont"


I notice that, in math mode (i.e., in quarto, $$...$$ or $...), letters and numbers are being set in a default font irrespective of the mainfont or mathfont. Thus, variable names and numbers are being typeset in a different font.

mathfont does have effect on math operators, e.g. \log will be typeset using the mathfont. However, letters and numbers are not. Is this expected behavior?

I know a workaround. That is to use \mathit{} for the entire equation, then use \mathrm{} for numbers that need to be upright. Then it looks right, but it makes the coding so much more heavy (than it already may be).

Compile the following document to see the issue:

---
documentclass: article
mainfont: Lato
mathfont: Lato
---
$$ y = 123\cdot\log{x} $$
$$ \mathit{y = \mathrm{123}\cdot\log{x}} $$

(Eventually substitute your own font). The second equation looks how it ought to be. In the irst equation, only operators (log) use the mathfont. Other items use a default "latex" font (the same you have when not specifying a mainfont).


Solution

  • Not every font contains the necessary math symbols, Lato does not contain them. You'll see a warning like this in your .log file:

    Package fontspec Warning: Font "Lato" does not contain requested Script
    (fontspec)                "Math".
    

    In particular for sans serif fonts, there aren't a lot to choose from and the few which exist are often incomplete.

    One sans serif font which provides a fairly good coverage is Fira Math (... but it's design might be a bit polarizing)

    ---
    documentclass: article
    mainfont: Lato
    mathfont: Fira Math
    format:
      pdf
    ---
    $$ y = 123\cdot\log{x} $$
    $$ \mathit{y = \mathrm{123}\cdot\log{x}} $$
    

    enter image description here


    If you'd like to use the letters and numbers from the text font in math mode, you could use the mathastext package:

    % !TeX TS-program = lualatex
    \documentclass{article}
    
    \usepackage[no-math]{fontspec}
    \setmainfont{Lato}
    \usepackage{mathastext}
    
    \begin{document}
    
    \[ y = 123\cdot\log{x} \] 
    
    \end{document}
    

    enter image description here