微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 R Markdown Caption 不能带“&”

如何解决为什么 R Markdown Caption 不能带“&”

我正在逐步构建 R Markdown (.RMD) 文件,边做边学。我能够插入几个表,但其中一个有问题。初始设置是:

---
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)
```

产生错误的原始代码

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

错误信息是:

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

如果我从标题删除“&”,问题就解决了,它变成

caption="SP Historical Performance Statistics"

不过,我还是想要标题中的“&”。有没有办法保留它?我尝试在它前面放一个转义字符“”,但没有用。关于如何保留“&”的任何建议?

解决方法

根据wiki,有些字符需要转义

enter image description here


这里是经过测试的markdown代码

---
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")
```

-输出

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。