围绕 kable-tables 的多个 bookdown/rmarkdown/knitr 个问题

Multiple bookdown/rmarkdown/knitr issues around kable-tables

在 RStudio 中,我尝试将 rmarkdown 与 bookdown 结合使用(主要是为了参考 table 和数字的功能)并且 运行 遇到了格式化问题在 table 和标题中。请考虑以下示例:

---
title: "Test"
knit: "bookdown::render_book"
output:
  bookdown::pdf_book:
    keep_tex: yes
link-citations: true
references:
- type: article-journal
  id: WatsonCrick1953
  author:
  - family: Watson
    given: J. D.
  - family: Crick
    given: F. H. C.
  issued:
    1953
  title: 'Molecular structure of nucleic acids: a structure for     deoxyribose
nucleic acid'
  container-title: Nature
  volume: 171
  issue: 4356
  page: 737-738
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```
@WatsonCrick1953
```{r test-table, tidy=FALSE, echo = FALSE}
kable(
  data.frame(
    Citation = c("@WatsonCrick1953"),
    Formatted.String = c("Some--Thing^2^")),
  caption = "*Bold* in a caption;"#, booktabs = TRUE
)
```

最终产品的详细信息是:

这有多个问题:

  1. 标题中的“粗体”不是 rmarkdown 格式
  2. "^2^" 没有产生预期的上标(特别奇怪,因为 "--" 被理解为 en-dash)
  3. 引用在 table 中不被理解(在文本中,在 table 代码上方,它工作正常但未包含在屏幕截图中)

另一个问题是当前生产的 Latex 不会产生对“booktabs”包的引用,这可能是正确使用 kable 的“booktabs = TRUE”参数所必需的(它直接来自 booktabs 文档因此应该可以工作)。

请告诉我如何实现我正在尝试的目标...

约翰

由于您正在编织成 PDF,kable() 的输出将自动检测到这一点,并格式化为生成乳胶。

因此您需要使用 Latex 指令来格式化您的文本。

试试这个:

  1. 将块选项设置为 results = 'asis'
  2. 使用\textbf{}生成粗体

例如:

```{r test-table, tidy=FALSE, echo = FALSE, results='asis'}
library(knitr)
kable(
  data.frame(
    Citation = c("@WatsonCrick1953"),
    Formatted.String = c("Some--Thing^2^")),
  caption = "\textbf{Bold} in a caption -- ;"

)
```

切换到 pander 就可以了:

---
title: "Test"
knit: "bookdown::render_book"
output:
  bookdown::pdf_book:
    keep_tex: yes
link-citations: true
references:
- type: article-journal
  id: WatsonCrick1953
  author:
  - family: Watson
    given: J. D.
  - family: Crick
    given: F. H. C.
  issued:
    1953
  title: 'Molecular structure of nucleic acids: a structure for deoxyribose nucleic acid'
  container-title: Nature
  volume: 171
  issue: 4356
  page: 737-738
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
library(pander)
```
@WatsonCrick1953
```{r test-table, tidy=FALSE, echo = FALSE}
pander(
  data.frame(
  Citation = c("@WatsonCrick1953"),
  Formatted.String = c("Some--Thing^2^")),
  caption = "*Not bold* in a caption; **bold** in a caption;",
  style = "simple",
  justify = "left"
)
```

结果如下:

  1. 字幕格式为 markdowny。
  2. "^2^"等正确理解。
  3. 引用效果很好。

我很高兴找到这个 post,尽管我无法复制 Andrie 的回答。我想补充 也可以 通过修改标题 pander 来引用 table:caption = "(\#tab:test-table) *Not bold* in a caption; **bold** in a caption;",

我修改了代码以生成一篇文章 pdf 文档而不是一本书,这段代码对我有用:

---
title: "Test"
output:
  bookdown::pdf_document2:
    keep_tex: yes
link-citations: true
references:
- type: article-journal
  id: WatsonCrick1953
  author:
  - family: Watson
    given: J. D.
  - family: Crick
    given: F. H. C.
  issued:
    1953
  title: 'Molecular structure of nucleic acids: a structure for deoxyribose nucleic acid'
  container-title: Nature
  volume: 171
  issue: 4356
  page: 737-738
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
library(pander)
```

@WatsonCrick1953 in Table \@ref(tab:test-table)

```{r test-table, tidy=FALSE, echo = FALSE}
pander(
  data.frame(
  Citation = c("@WatsonCrick1953"),
  Formatted.String = c("Some--Thing^2^")),
  caption = "(\#tab:test-table) *Not bold* in a caption; **bold** in a caption;",
  style = "simple",
  justify = "left"
)
```