Change equations font in RMarkdown

Viewed 206

I have math equations on my RMarkdown document that I'm knitting to a PDF file using knitr. I'm using the Arial font, specified in my YAML as:

title: "My document"
subtitle: "Draft"
output:
  pdf_document:
    latex_engine: xelatex
mainfont: Arial
geometry: margin = 1in
fontsize: 11pt 

The issue is that my equations use the default font instead of the one I set up in the YAML.

For example:

$$\mathrm{P}(Rain \mid Sun ) < \mathrm{P}(Rain \mid Cloud)$$

So I have two different fonts in my overall document, which is not what I want. How do I change the font for mathematical expressions in RMarkdown?

2 Answers

I'm not all that savvy on latex. However, when I used mathfont: Arial it was ignored. (I tried using it with both xelatex and lualatex.)

I was able to get it to work, but some of the content was smashed together. This works so that it's readable, still math, and arial font.

---
title: "Untitled"
author: "me"
date: "2/19/2022"
output: 
  pdf_document:
    latex_engine: lualatex
header-includes:
  - \usepackage{amsmath}
  - \usepackage{unicode-math}
  - \usepackage{babel}
  - \defaultfontfeatures{ Scale=MatchLowercase, Ligatures = TeX }
  - \setmainfont{Arial}
  - \setsansfont{Arial}
  - \setmonofont{Andale Mono}
  - \setmathfont{GFSNeohellenicMath.otf}
  - \setmathfont[range=up]{Arial}
  - \setmathfont[range=it]{Arial Italic}
  - \setmathfont[range=bfup]{Arial Bold}
  - \setmathfont[range=bfit]{Arial Bold Italic}
  - \setmathfont[range=tt]{Andale Mono}
---

Here's some of the math I used to validate this:

$$A_{m,n} =
 \begin{pmatrix}
  a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
  a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  a_{m,1} & a_{m,2} & \cdots & a_{m,n}
 \end{pmatrix}$$

$$f(data | \lambda) \pi ( \lambda)$$

$$ answer ~=~ \frac{\pi}{d}$$

The output:

enter image description here

With only mathfont: Arial in the YAML, regardless of the engine:

enter image description here

Related