Reputation: 10215
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")
```
Upvotes: 5
Views: 1401
Reputation: 556
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) ...
.
Upvotes: 1
Reputation: 167
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'))
```
Upvotes: 1
Reputation: 1019
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)
Upvotes: 3
Reputation: 639
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 prefixtab:
).
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
Upvotes: 5