1

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

Community
  • 1
  • 1
balin
  • 1,554
  • 1
  • 12
  • 26

3 Answers3

1

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.
balin
  • 1,554
  • 1
  • 12
  • 26
  • would you also have some recommendations for the following Qs: https://stackoverflow.com/questions/60058692/table-in-bookdown-huskydown-with-several-features-citation-caption-url-png-f/60058693#60058693? many thanks in advance! – mavericks Feb 04 '20 at 13:49
0

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

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Thank you for your comment. The "\\textbf{}" bit works indeed, albeit at the cost of portability (no html), which is annoying. However, the table-internal formatting (both the superscripting and the reference) do NOT work in the context of bookdown ... – balin Jun 10 '16 at 21:33
  • As proposed [here](http://tex.stackexchange.com/a/211638), ` header-includes: - \usepackage{booktabs}` solves the booktabs issue. – balin Jun 10 '16 at 21:46
0

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