How can I escape $ so I can hyperlink to it in R Markdown?

Viewed 148

I used $ as a name for one of my sections. I then noticed that the hyperlink from my table of contents to that section does not work. Can this be fixed? I've tried escaping it with \ and I've even tried $, but neither changed the outcome.

This is my header

output:
  github_document:
    toc: true
2 Answers

I have reproduced your problem in this code example with R Studio Version 4.1.

It seems to be a github_document thing, because it works when you knit to output: html_document.

A possible solution is to add a backslash-escaped space ( # $ \ )behind the dollar and it is parsed as a nonbreaking space.

---
title: "Repro Example Dollar Sign in title"
output:
  github_document:
    toc: TRUE
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


Some Example content to test whether shortcut works

<br>
<br>
<br>
<br>

Some Example content to test whether shortcut works

# Header One (normal text)

Some Example content to test whether shortcut works



# $


If $ is without text it doesn't work

Some Example content to test whether shortcut works

<br>
<br>
<br>
<br>

Some Example content to test whether shortcut works

# $ \

Add a backslash-escaped space behind the dollar and it is parsed as a nonbreaking space.

```{r cars}
summary(cars)
```

## $ Header 2 with dollar and text in title (shortcut works)

Some Example content to test whether shortcut works

# header 3

Some Example content to test whether shortcut works

<br>
<br>
<br>
<br>

Some Example content to test whether shortcut works

Versions that I tested which don't work:

  • using inline r code to print the dollar sign with two trailing spaces # `r stringr::str_pad("$", width = 3, side = "right")`
  • # $•• (double space)
  • generating the $ from a chunk: ```{r results='asis'} cat("#", " $", " ") ```

Rstudio Example

You can escape \,^,$,.,?,*,|,+,(,),[, and { with backslashes.

Related