在 bookdown:html_document2 中没有 table 带有 pander 的编号

No table numbering with pander in bookdown:html_document2

pander 在与 bookdown::html_document2 一起使用时不包括 table 编号。我错过了一些选项吗?

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

来自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:).

这里的原因是 pander::pander() 没有产生正确的 (\#tab:***)。您可以将错误报告给 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

首先,我非常感谢bookdown!

的用户

我曾尝试使用 pander 而不是 kable 来实现相同的目的。这样做的原因是 kablekableExtra 不会在 tables 的 pdf 输出中呈现标记文本,这是一个巨大的电流缺陷......所以对于 pdf 输出,kable 不会呈现诸如 @smith2018-so*italic* 等文献参考之类的东西……这很痛苦。希望尽快提供补丁。为了采用上面的相同代码,我能够使用 pander 进行引用工作,并在上面的代码中提供了一些更改。关于 kable 的另一件事,必须添加 longtable=T 否则 table 会在 pdf 输出中向下浮动到页面底部。

首先我在 YAML 中添加了 documentclass: article,然后我将代码块命名为。但真正让它在 pander 中起作用的是将标题更改为 caption = '(\#tab:chunkname) Iris with pander')。诀窍是添加我在 bookdown 参考中找不到的双 \ 。所以最后,适用于 html 和 pdf 输出的代码是:

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

基于上面的示例,我创建了一个函数,使用 bookdown cross-ref 格式

为 html 和乳胶创建适当的标签
---
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'))
```