R语言 如何在kable中插入latex表达式

3j86kqsm  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(164)

我想在kable的标题选项中包含一个latex表达式。但是,我收到一个未定义的错误消息。我可以在函数add_header_above中包含latex表达式,并按预期工作,但是当我在kable(caption=)中尝试相同的表达式时,rmarkdown失败。下面是一个可重现的示例:第一张table很好用,但第二张不行。

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

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

R Markdown

This is an R Markdown document.

LATEX EXPRESSION WORKS FINE

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

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

目标是在表格标题旁边得到一个向上和向下的乳胶箭头。我可以通过在表格上添加额外的标题来实现这一点,但我更喜欢在标题旁边得到箭头。见下面的快照:
![](https://i.stack.imgur.com/jC4X8.png)
nxowjjhe

nxowjjhe1#

你必须使用\\,而不是\\\\,来转义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 = "")
library(kableExtra)

R Markdown

This is an R Markdown document.

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

![](https://i.stack.imgur.com/PuUrr.png)

相关问题