3

I am building a Xaringan deck for a short course on R. I need students to learn the character sequences for assignment <- and relational operators like !=.

However, knitting to slides replaces several character sequences with more elegant display characters. For example, <- becomes a left-pointing arrow (←).

Can I preserve the original characters in slide output?

I still get the replacement inside single backticks (`) for code formatting and even inside R code chunks (```). Escaping the characters with \ does not help.

---
# Two ways to save variables to memory

R has two ways to assign variables: `=` and `<-`.  

The following expressions are equivalent:

`turnout_rate = .598` and `turnout_rate <- .598`

When knit to slides, all <- above are replaced with a left arrow (←) . I would like to keep the original characters <-.

Thanks in advance for your help!

dizhihong
  • 105
  • 3
  • 3
    This does not happen for me. Maybe you have a font like [Fire Code](https://github.com/tonsky/FiraCode) installed where `<-` is rendered as a ligature. You can test this by using `Right Click -> Inspect Element` in the preview/browser. Which font is used for code segments? – Ralf Stubner Aug 27 '19 at 21:03
  • Thank you @RalfStubner - that was it. I am using the Metropolis theme which uses Fira Sans in ordinary text and (apparently) Fira Code for code. I installed [Fira Mono](https://fonts.google.com/specimen/Fira+Mono?selection.family=Fira+Mono), which does not impose ligatures and added the following to my custom CSS: `.remark-code { font-family: 'Fira Mono'; }`. This fixed my problem. LMK if you want to add an answer so I can upvote. – dizhihong Aug 27 '19 at 21:30
  • Previous comment incomplete. To get the inline code too, you need the following in the custom CSS: `.remark-inline-code { font-family: 'Fira Mono'; }` – dizhihong Aug 27 '19 at 21:40

1 Answers1

2

You probably have a CSS style sheet in place that calls for a font like Fira Code where <- is rendered as a ligature. You can test this by using Right Click -> Inspect Element in the preview/browser.

You can change the used font via some custom CSS code, e.g.:

.remark-code, .remark-inline-code {
    font-family: 'Fira Mono', 'Source Code Pro', monospace;
}

You have to specify the custom CSS file in the YAML header, e.g. for the default theme and a file named custom.css:

---
output:
  xaringan::moon_reader:
    css: ["default", "custom.css"]
---

References:

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75