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")
```
Dieter Menne
  • 10,076
  • 44
  • 67
  • 1
    @daroczig Too bad I somehow missed you at user2017 – Dieter Menne Aug 12 '17 at 14:36
  • 1
    Hope to see you next time :) Regarding this problem, I've opened a [GH ticket](https://github.com/Rapporter/pander/issues/307) 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. – daroczig Aug 13 '17 at 22:27

4 Answers4

4

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
Zhuoer Dong
  • 629
  • 4
  • 11
  • Thanks for finding the right spot in the docs. Since the package author @daroczig is one of the fastest SO responders and sometimes corrects these bugs within hours, he probably is on vacation. – Dieter Menne Aug 13 '17 at 07:33
  • @daroczig: Any progress on https://github.com/Rapporter/pander/issues/307. I still cannot use pander when tables must be numbered – Dieter Menne Dec 18 '18 at 17:45
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)
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'))
```
Ceres
  • 163
  • 7
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) ....

Paul
  • 527
  • 5
  • 17