【问题标题】:Multiple bookdown/rmarkdown/knitr issues around kable-tables围绕 kable-tables 的多个 bookdown/rmarkdown/knitr 问题
【发布时间】:2016-10-11 14:49:09
【问题描述】:

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

最终产品的详细信息是:

这有多个问题:

  1. 标题中的“粗体”不是 rmarkdown 格式
  2. “^2^”不产生预期的上标(特别奇怪,因为“--”被理解为破折号)
  3. 表格中无法理解引用(在文本中,表格代码上方,它可以正常工作,但不包含在屏幕截图中)

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

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

【问题讨论】:

    标签: r rstudio knitr r-markdown bookdown


    【解决方案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"
    )
    ```
    

    结果如下:

    1. 字幕格式是降价的。
    2. “^2^”等正确理解。
    3. 引用效果很好。

    【讨论】:

    【解决方案2】:

    由于您正在编织 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 -- ;"
    
    )
    ```
    

    【讨论】:

    • 感谢您的评论。 "\\textbf{}" 位确实有效,尽管代价是可移植性(没有 html),这很烦人。但是,表格内部格式(上标和参考)在 bookdown 的上下文中不起作用...
    • 按照here 的建议,` header-includes: - \usepackage{booktabs}` 解决了 booktabs 问题。
    【解决方案3】:

    我很高兴找到这篇文章,尽管我无法复制 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"
    )
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2021-05-25
      • 2018-04-20
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多