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

时间:2016-06-10 15:06:45

标签: r rstudio knitr r-markdown bookdown

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

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

最终产品的详细信息如下: enter image description here

这有多个问题:

  1. "粗体"在标题中没有标记为rmarkdown格式
  2. " ^ 2 ^"没有产生预期的上标(特别奇怪,因为" - "被理解为en-dash)
  3. 表格中没有理解引文(在文本中,表格代码上方,它工作得很好但不包括在屏幕截图中)
  4. 另一个问题是,目前生产的乳胶不会产生对#ta; booktabs"包,可能需要正确使用" booktabs = TRUE"对kable的论证(直接来自booktabs文档,因此应该起作用)。

    请告诉我如何实现我的目标...

    荷兰Joh

3 个答案:

答案 0 :(得分:1)

切换到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"
)
```

结果如下: Pander result

  1. 标题格式为markdowny。
  2. “^ 2 ^”等已被正确理解。
  3. 引用很好。

答案 1 :(得分:0)

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

因此,您需要使用乳胶说明格式化文本。

试试这个:

  1. 将chunk选项设置为results = 'asis'
  2. 使用\\textbf{}生成粗体
  3. 例如:

    ```{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 -- ;"
    
    )
    ```
    

    enter image description here

答案 2 :(得分:0)

我很高兴找到这篇文章,虽然我无法复制Andrie的回答。我想通过修改标题,添加也可以使用pander引用该表: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"
)
```