Change equations font in RMarkdown

cafachag

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?

Kat

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change font size in RMarkdown PDF

How to change the font for hyperlinks in RMarkdown?

Change font size for all inline equations R markdown

How to change font in RMarkdown when using "rticles" package?

How to change font size of table in Rmarkdown, LaTeX and .pdf?

How do you change the font size in RMarkdown for Knitr

Making pretty equations in RMarkdown with LaTeX

Calligra font with RMarkdown

rmarkdown pdf font not available

Rmarkdown font size and header

rmarkdown PDF custom font

Writing/Compiling inline equations within lists in RMarkdown?

How to change the font color of title, author, date and make it bold in yaml of Rmarkdown?

How to change font style font with Rmarkdown-PDF , already sent error : " Failed to find a package that contains Times New Roman"

Rmarkdown Will Not Knit Montserrat Font to PDF

YAML title font and color in Rmarkdown

Set font family and size of equations in flextable

Do not render equations in rmarkdown documents when converting to plain markdown

How can I auto-number math equations in RMarkdown?

rmarkdown beamer reduce font size for references

Rmarkdown table of contents changes font color

Code chunk font size in Rmarkdown with knitr and latex

RMarkdown: Customize Color and Font Size of (automated) Citations

RMarkdown - different font types in table using kable?

Changing font size in Rmarkdown presentation using revealjs

Font issue in rmarkdown slides with Metropolis theme

Calibri font for a particular set of line in Rmarkdown

Changing font family for a paragraph in latex rmarkdown

Change Font Family, Rename Font