balin
balin

Reputation: 1686

Multiple bookdown/rmarkdown/knitr issues around kable-tables

In RStudio I'm trying to use rmarkdown in conjunction with bookdown (mostly for the capabilitites to reference tables and figures) and am running into trouble with the formatting in tables and captions. Please consider the following example:

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

A detail of the resulting product is: enter image description here

This has multiple issues:

  1. "Bold" in the caption isn't rmarkdown formatted
  2. "^2^" does not produce the expected superscripting (particularly strange, as "--" is understood as en-dash)
  3. The citation is not understood within the table (in the text, above the table code, it works just fine but is not included in the screen shot)

A further issue is that the currently produced latex does not produce a reference to the "booktabs" package, which is presumably needed to properly use the "booktabs = TRUE" argument to kable (which comes directly from the booktabs documentation and thus ought to work).

Please let me know how I may achieve what I am trying ...

Joh

Upvotes: 1

Views: 1416

Answers (3)

François Birgand
François Birgand

Reputation: 1019

I was glad to find this post, although I just could not replicate Andrie's answer. I would like to add that it is also possible to have the table referenced using pander by modifying the caption as such: caption = "(\\#tab:test-table) *Not bold* in a caption; **bold** in a caption;",

I modified the code to produce an article pdf document rather than a book and this code works for me:

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

Upvotes: 0

balin
balin

Reputation: 1686

Switching to pander does the trick:

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

Here's the result: Pander result

  1. Caption formatting is markdowny.
  2. "^2^" etc. is properly understood.
  3. Citing works just fine.

Upvotes: 1

Andrie
Andrie

Reputation: 179558

Since you are knitting to a PDF, the output of kable() will automatically detect this, and be formatted to produce latex.

Thus you need to use latex instructions to format your text.

Try this:

  1. set the chunk option to results = 'asis'
  2. use \\textbf{} to produce bold

For example:

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

Upvotes: 0

Related Questions