首页 > 解决方案 > 使用 Bookdown 创建带有下标和彩色行的表格

问题描述

我正在尝试从包含复杂表的 Bookdown 脚本生成 PDF。该表包括一些带有下标的参数名称。我还想为一些行着色。示例脚本如下所示:

---
title: "Example problem"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
  #bookdown::gitbook: default
  bookdown::pdf_book: default
always_allow_html: yes  
---
This is a test example for the problem.

```{r}
library(magrittr)
library(knitr)
library(kableExtra)
df <- data.frame(Parameter = c("NO~x~ emissions", "SO~2~ emissions", "CO~2~     emissions"), "Value mg/Nm^3^" = c(800,900,1000),check.names=F)

knitr::kable(df,escape = F, caption = 'Example table!',  booktabs = TRUE, format = "latex") %>% #
  row_spec(0, bold = T, color = "white", background = "#045a8d") %>%
  row_spec(c(2), bold = T, color = "white", background = "#3690c0")
```
blah blah

我可以使用 kable 格式作为 'format = "html"' 运行脚本,结果看起来很好,包括彩色行和下标。当我将格式更改为 Latex 时,下标在生成的 pdf 中显示不正确。

我尝试将参数 escape = F 添加到 kable,但构建过程失败。

Quitting from lines 14-23 (_main.Rmd) 
Error in kable_latex(x = c("$NO_{x}$ emissions", "SO2 emissions", "CO2 emissions",  : 
  unused argument (example = FALSE)
Calls: <Anonymous> ... eval -> %>% -> eval -> eval -> <Anonymous> -> do.call

任何人都可以帮助解决这个问题吗?

标签: rbookdownkable

解决方案


对我来说,如果我使用(转义)LaTeX 语法,它会起作用:

---
title: "Example problem"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::pdf_book: default
  #bookdown::gitbook: default
always_allow_html: yes  
---
This is a test example for the problem.

```{r}
library(magrittr)
library(knitr)
library(kableExtra)
df <- data.frame(Parameter = c("NO\\textsubscript{x} emissions", "SO\\textsubscript{2} emissions", "CO\\textsubscript{2}     emissions"),
                 "Value mg/Nm\\textsuperscript{3}" = c(800,900,1000),
                 check.names = F)

knitr::kable(df,escape = F, caption = 'Example table!',  booktabs = TRUE, format = "latex") %>% #
  row_spec(0, bold = T, color = "white", background = "#045a8d") %>%
  row_spec(c(2), bold = T, color = "white", background = "#3690c0")
```
blah blah

推荐阅读