简体   繁体   English

为什么 R Markdown 标题不能带“&”

[英]Why R Markdown Caption cannot take "&"

I am gradually building an R Markdown (.RMD) file, learning by doing.我正在逐步构建一个 R Markdown (.RMD) 文件,边做边学。 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根据wiki ,有些字符需要escaping

在此处输入图像描述


Here, is a tested version of the markdown code这里是 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")
```

-output -输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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