简体   繁体   中英

Why R Markdown Caption cannot take "&"

I am gradually building an R Markdown (.RMD) file, learning by doing. I was able to insert a couple of tables, but I had a problem with one of them. The initial setup is:

---
title: "Untitled"
author: "Me"
date: "5/10/2021"
output: bookdown::pdf_book
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE,
               fig_align = "left",
               fig_width = 7,
               fig_height = 7,
               dev = "png",
               cache = FALSE)
```

The original code that generated an error was

```{r sphistperf}
kable(stock_index_stats,
      format="latex",
      caption="S&P Historical Performance Statistics")
```

The error message is:

output file: TestCenter.knit.md

! Misplaced alignment tab character &.
<argument> ...}{\relax }}\label {tab:sphistperf}S&
                                                  P Historical Performance S...
l.202 ...rf}S&P Historical Performance Statistics}

Error: LaTeX failed to compile TestCenter.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See TestCenter.log for more info.
Error during wrapup: 
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

The problem is fixed if I remove the "&" from the caption, which becomes

caption="SP Historical Performance Statistics"

Still, I want the "&" in my caption. Is there a way to keep it? I tried putting an escape character "" before it and that did not work. Any suggestions on how to keep the "&"?

According to wiki , there are some characters that needs escaping

在此处输入图像描述


Here, is a tested version of the markdown code

---
title: "testing"
author: "akrun"
date: "10/05/2021"
output: bookdown::pdf_book
---

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

```{r cars}
library(kableExtra)
kable(summary(cars), format = 'latex',  caption="Dummy S\\&P Performance")
```

-output

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM