No table numbering with pander in bookdown:html_document2
Asked Answered
R

4

5

pander does not include table numbering when used with bookdown::html_document2. Did I miss some option?

---
title: "Pander Table Numbering"
output: bookdown::html_document2
---

# Chapter 1

```{r}
library(knitr)
library(pander)
# Table 1.1
kable(head(iris), caption = "Iris with kable")
# Another Table 1.1 (ok, same chunk = same table)
kable(head(mtcars), caption = "Mtcars kable")
```

```{r}
# No table number
pander(head(iris), caption = "Iris with pander")
```

```{r}
# Table 1.2
kable(head(mtcars), caption = "Mtcars kable")
```
Ramer answered 12/8, 2017 at 14:35 Comment(2)
@daroczig Too bad I somehow missed you at user2017Ramer
Hope to see you next time :) Regarding this problem, I've opened a GH ticket and hope to resolve that with some help. Unfortunately, I'm not very familiar with the bookdown internals, but maybe adding a new param to pander might solve this pretty easily.Booster
H
3

First, I am an extremely grateful user of bookdown!

I have tried to achieve the same thing using pander instead of kable. The reason for this is that kable or kableExtra do not render markup text in tables of pdf output, which is a huge current drawback... So for pdf output, kable will not render things like literature references such as @smith2018-so or *italic* and so on... which is a pain. Hopefully, a patch will be provided soon. To take the same code above, I was able to have the referencing work with pander provided several changes in the code above. Another thing about kable, one must add longtable=T otherwise the table floats down to the bottom of a page in pdf output.

First I added documentclass: article in the YAML, and then I named the the code chunks. But the thing that really make it work in pander is by changing the caption to caption = '(\\#tab:chunkname) Iris with pander'). The trick was to add the double \\ which I could not find in the bookdown reference. So in the end, a code that worked for both html and pdf outputs is :

---
title: "Pander Table Numbering"
documentclass: article
output: 
  bookdown::html_document2: default
  bookdown::pdf_document2: default
---

# Chapter 1

Table \@ref(tab:TabKable1)

```{r "TabKable1", results='asis'}
library(knitr)
library(pander)
# Table 1.1
knitr::kable(head(iris), caption = 'Iris with kable', longtable =T)
# Another Table 1.1 (ok, same chunk = same table)
#kable(head(mtcars), caption = "Mtcars kable")
```

Table \@ref(tab:TabPander1)

```{r "TabPander1"}
# No table number
pander(head(iris), caption = 'Iris with pander')
```

Table \@ref(tab:TabPander2)

```{r "TabPander2"}
# table number this time!!
pander(head(iris), caption = '(\\#tab:TabPander2) Iris with pander')
```


Table \@ref(tab:TabKable2)

```{r "TabKable2"}
# Table 1.2
kable(head(mtcars), caption = "Mtcars kable", longtable=T)
Hutchinson answered 7/6, 2018 at 16:20 Comment(2)
I have accepted the workaround as the current answer now.Ramer
The workaround produces correct output only in html. Verbatim output in Word, no output at all in pdfRamer
W
5

From https://bookdown.org/yihui/bookdown/tables.html

If you decide to use other R packages to generate tables, you have to make sure the label for the table environment appears in the beginning of the table caption in the form (\#label) (again, label must have the prefix tab:).

The reason here is that pander::pander() doesn't produce proper (\#tab:***). You can report the bug to the author of pander.

kable(head(iris), caption = "Iris with kable")

Table: (\#tab:unnamed-chunk-1)Iris with kable

Sepal.Length   Sepal.Width   Petal.Length   Petal.Width  Species 
-------------  ------------  -------------  ------------  --------
        5.1           3.5            1.4           0.2  setosa  
        4.9           3.0            1.4           0.2  setosa  
        4.7           3.2            1.3           0.2  setosa  
        4.6           3.1            1.5           0.2  setosa  
        5.0           3.6            1.4           0.2  setosa  
        5.4           3.9            1.7           0.4  setosa  

pander(head(iris), caption = "Iris with pander")

-------------------------------------------------------------------
Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
    5.1            3.5           1.4            0.2       setosa  

    4.9             3            1.4            0.2       setosa  

    4.7            3.2           1.3            0.2       setosa  

    4.6            3.1           1.5            0.2       setosa  

    5             3.6           1.4            0.2       setosa  

    5.4            3.9           1.7            0.4       setosa  
-------------------------------------------------------------------

Table: Iris with pander
Wellbred answered 13/8, 2017 at 1:26 Comment(2)
Thanks for finding the right spot in the docs. Since the package author @Booster is one of the fastest SO responders and sometimes corrects these bugs within hours, he probably is on vacation.Ramer
@daroczig: Any progress on github.com/Rapporter/pander/issues/307. I still cannot use pander when tables must be numberedRamer
H
3

First, I am an extremely grateful user of bookdown!

I have tried to achieve the same thing using pander instead of kable. The reason for this is that kable or kableExtra do not render markup text in tables of pdf output, which is a huge current drawback... So for pdf output, kable will not render things like literature references such as @smith2018-so or *italic* and so on... which is a pain. Hopefully, a patch will be provided soon. To take the same code above, I was able to have the referencing work with pander provided several changes in the code above. Another thing about kable, one must add longtable=T otherwise the table floats down to the bottom of a page in pdf output.

First I added documentclass: article in the YAML, and then I named the the code chunks. But the thing that really make it work in pander is by changing the caption to caption = '(\\#tab:chunkname) Iris with pander'). The trick was to add the double \\ which I could not find in the bookdown reference. So in the end, a code that worked for both html and pdf outputs is :

---
title: "Pander Table Numbering"
documentclass: article
output: 
  bookdown::html_document2: default
  bookdown::pdf_document2: default
---

# Chapter 1

Table \@ref(tab:TabKable1)

```{r "TabKable1", results='asis'}
library(knitr)
library(pander)
# Table 1.1
knitr::kable(head(iris), caption = 'Iris with kable', longtable =T)
# Another Table 1.1 (ok, same chunk = same table)
#kable(head(mtcars), caption = "Mtcars kable")
```

Table \@ref(tab:TabPander1)

```{r "TabPander1"}
# No table number
pander(head(iris), caption = 'Iris with pander')
```

Table \@ref(tab:TabPander2)

```{r "TabPander2"}
# table number this time!!
pander(head(iris), caption = '(\\#tab:TabPander2) Iris with pander')
```


Table \@ref(tab:TabKable2)

```{r "TabKable2"}
# Table 1.2
kable(head(mtcars), caption = "Mtcars kable", longtable=T)
Hutchinson answered 7/6, 2018 at 16:20 Comment(2)
I have accepted the workaround as the current answer now.Ramer
The workaround produces correct output only in html. Verbatim output in Word, no output at all in pdfRamer
V
1

Based on the example above, I created a function that creates the appropriate label for html and latex, using the bookdown cross-ref formating

---
title: "Pander Table Numbering"
documentclass: article
output: 
  bookdown::html_document2: default
  bookdown::pdf_document2: default
---
```{r setup, include = FALSE}
addLabel <- function(caption = "", tag = "tab") {
  chunkLabel <- knitr::opts_current$get("label")
  pretag <- if (knitr::is_latex_output()) {
    paste0("\\label{", tag, ":",chunkLabel , "}")
  } else {
      paste0("(\\#", tag, ":", chunkLabel, ")"))
    }
  paste0(pretag, caption)
}
```

# Chapter 1

Table \@ref(tab:TabPander1)

```{r "TabPander1"}
pander(head(iris), caption = addLabel('Iris with pander'))
```

Table \@ref(tab:TabPander2)

```{r "TabPander2"}
pander(head(iris), caption = 'addLabel('Iris with pander'))
```
Vaginismus answered 4/6, 2022 at 7:50 Comment(0)
S
1

The magic seems to be the : prefix, i.e. pander(caption = ': (\\#tab:your-label) Your caption') allows you to refer to the table along the lines of Table \@ref(tab:your-label) ....

Spatter answered 14/2, 2023 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.