如何在kable中插入latex表达式

问题描述 投票:0回答:1

我想在 kable 的标题选项中包含一个乳胶表达式。但是,我收到一条未定义的错误消息。我能够在函数

add_header_above
中包含乳胶表达式并按预期工作,但是当我在 kable(caption=) 中尝试相同的表达式时,rmarkdown 失败。我在下面有一个可重现的例子:第一个表工作正常但第二个不工作。

---
title: "Latex Expression"
output: pdf_document
date: '2023-02-24'
---

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

```{r}
library(kableExtra)
```

## R Markdown

This is an R Markdown document. 

**LATEX EXPRESSION WORKS FINE**
```{r}
kable(mtcars[1:5,], format = "latex", booktabs = TRUE, caption="Another table with Acrobat") %>% 
  kable_styling(latex_options = c("striped","HOLD_position")) %>%
  add_header_above(c("\\\\Acrobatmenu{GoBack}{\\\\color{blue}{$\\\\updownarrow$}}"=12), align = "r", escape = FALSE)
```

**GET AN ERROR HERE**
```{r}
kable(mtcars[1:5,], format = "latex", booktabs = TRUE, caption="This is a graphic \\\\Acrobatmenu{GoBack}{\\\\color{blue}{$\\\\updownarrow$}}") %>% 
  kable_styling(latex_options = c("striped","HOLD_position"))
```

目标是在表格标题旁边获得上下乳胶箭头。我可以通过在表格上添加一个额外的标题来完成此操作,但我更喜欢在标题旁边添加箭头。请参阅下面的快照:

r latex r-markdown kable kableextra
1个回答
0
投票

您必须使用

\\
,而不是
\\\\
,才能在
kable(caption = "....")
中转义 LaTeX 命令。此外,您必须
\protect
标题中的命令
\Acrobatmenu{GoBack}{\color{blue}{$\updownarrow$}}

---
title: "Latex Expression"
output: pdf_document
date: '2023-02-24'
---

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

```{r}
library(kableExtra)
```

## R Markdown

This is an R Markdown document. 

```{r}
kable(
  mtcars[1:5, ],
  format = "latex",
  booktabs = TRUE,
  caption = "This is a graphic \\protect\\Acrobatmenu{GoBack}{\\color{blue}{$\\updownarrow$}}"
) %>%
  kable_styling(latex_options = c("striped", "HOLD_position"))
```

© www.soinside.com 2019 - 2024. All rights reserved.