I discovered by trial and error that when using R's `gt` package (for creating tables), labels wrapped in the `gt::html()` function that have special characters such as the $ sign -- common when producing a table with dollar amounts -- must have the special character escaped, or the table will not render in pdf even though it will render in html.

The minimal reproducible example below illustrates that (1) code with a $ sign, not wrapped in `html()`, will render in both html and pdf, (2) code with a $ sign wrapped in `html()` will only render in html, and (3) code that escapes a $ sign wrapped in `html()` will render in both html and pdf - however, in my tests so far, to get the pdf to render, two escape slashes are required. But when two escape slashes are used, in the html output one of the slashes used to escape the $ sign will appear.

It's not a huge problem once the user is alerted to this, although having to accept either an unwanted slash in the html output or a non-rendering pdf is not ideal. **If anyone knows of a way to escape the special characters and render the pdf without having escape characters show in the html, I would much appreciate advice.**

It would be ideal (low priority of course) if quarto could detect this and invisibly solve the problem by escaping special characters wrapped in the `gt::html()` function (without having escape characters show in html rendering). Of course, there may be other situations in which this issue arises beyond the one I found here (other R packages and situations).

---------- Code below suitable for copying into qmd file ----------------------------------------------------------------------------

# Create data

```{r}
#| label: setup
#| include: false

library(tidyverse)
library(gt)

df <- tibble(rank=1:10, money=seq(100, 1000, 100))

```

# This renders in html and pdf format

Note that, in html format and in pdf format:

-   labels work for plain text, whether designated as html or not, and

-   labels work for text with a \$ sign, when not designated as html.

```{r}
df %>% 
  gt() %>% 
  cols_label(rank=html("Rank"), money="Amount $")
```

# This renders as html but generates error as pdf

Here "Amount \$" is enclosed in html(). The error is:

> You may need to \$ \$ around an expression in this file.

```{r}
#| eval: false

# set eval to true to generate error
# set eval to false to run the chunk after this one and render as pdf without error

df %>% 
  gt() %>% 
  cols_label(rank=html("Rank"), money=html("Amount $"))

```


# Escaping the $ allows this to render as pdf

However, note that in the html output, one of the slashes will appear. So this is not ideal.

```{r}

df %>% 
  gt() %>% 
  cols_label(rank=html("Rank"), money=html("Amount \\$"))

```




2022-04-19T15:18:51Z
donboyd5
Future
open
rendering to pdf when using the html function from the gt package in R requires escaping special characters
https://github.com/quarto-dev/quarto-cli/issues/685