r - 使用 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
任何人都可以帮助解决这个问题吗?
解决方案
对我来说,如果我使用(转义)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
推荐阅读
- reactjs - 提交作品与第二次尝试
- listview - 多维数组的嵌套列表视图
- twitter-bootstrap-3 - 如何将文本字段与其标签和按钮垂直对齐
- c - 如何在数组中的某个位置插入
- java - 如何在不登录的情况下使用 Firebase OTP 验证验证多个电话号码?
- c# - 从特定位置从 C# 中的字符串中提取值
- uikit - UIButton touchUp 被吞没
- vba - 访问 SQL(或 VBA 代码)以列出具有至少一个附件字段的所有表
- javascript - 在 Javascript 中从没有 eval 的变量创建自定义代码
- python - 用 C 语言写入文件,同时在 python 中读取文件