1
votes

I am using the rmarkdown package to produce PDF slides with the Metropolis theme. Recently, I noticed that the equations started to appear differently - they use a different font.

Minimal example of *.Rmd file:

---
output: 
  beamer_presentation:
    theme: "metropolis"
    latex_engine: xelatex
    keep_tex: true
---

## Problem with font
$$f(x_i\mid\mu,\sigma^2) = \exp\left\{-\frac{(x_i-\mu)^2}{2\sigma^2}\right\}$$

which, when knitted in RStudio, produces: slide produced with rmarkdown

This looks different from what is obtained when compiling the same slide directly in LaTeX with xelatex:

\documentclass{beamer}
\usetheme{metropolis}

\begin{document}

\begin{frame}{Problem with font}

\[f(x_i\mid\mu,\sigma^2) = \exp\left\{-\frac{(x_i-\mu)^2}{2\sigma^2}\right\}\]

\end{frame}

\end{document}

which produces: slide produced with beamer

This does not look like a big difference, but in other equations some special characters are missing, and the font size is slightly different, affecting the whole layout of my slides.

After some investigation, it turns out that commenting out those two lines in the tex file produced by rmarkdown makes it better:

  %\usepackage{unicode-math}
  %\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}

Is the unicode-math package the (only) culprit here? How to solve this problem and make sure that Metropolis uses the right fonts without manually changing the tex files?

Thanks in advance for your help!

System configuration:

1
seems like rmarkdown sets the pandoc mathspec variable for some reason? Try unsetting it. I searched the default template (pandoc -D latex) for unicode-math and that came up... see pandoc.org/MANUAL.html#templatesmb21
Thanks for your feedback. Forcing pandoc to use the mathspec package solved the problem, as @tarleb suggested below.remek

1 Answers

3
votes

What you are seeing is, indeed, the effect of the unicode-math package. There is a simple way to work around this by forcing pandoc to use the mathspec package instead. This can be done by setting mathspec: true in your metadata

---
mathspec: true
output: …
---

or by setting the respective variable when calling pandoc

---
output: 
  beamer_presentation:
    theme: "metropolis"
    latex_engine: xelatex
    pandoc_args: ["--variable=mathspec"]
---

There is only a minor, very subtle, and mostly inconsequential difference between the two. I'd suggest to use the first version, as it is simpler.

See TeX StackExchange for a discussion of the differences between unicode-math and mathspec.