0

Despite the huge amount of questions on this subject and despite having read the manual, including the last paragraph, I can't get my head around it. I found a solution for pdf-output, but would like it to work for html-output too (ideally an approach that works for both pdf- and html-output - like my attempt below using opts_knit$get("rmarkdown.pandoc.to") that sadly fails for html).

Using bookdown, it is easy to reference to kabel(Extra)- or flextable- tables, but my workaround for pander-tables only works for pdf-output. Can anyone guide me on how to get this working with html as well?

The MWE example below works with pdf, but the reference to the last table breaks when outputted to html.

MWE:

---
title: "Untitled"
output:
    bookdown::pdf_document2:
        keep_tex: yes
    bookdown::html_document2: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(knitr)
library(tidyverse)
library(pander)
library(flextable)
```


```{r}

df <- expand.grid(A = LETTERS[1:2],
              B = letters[3:5], 
                 stringsAsFactors = FALSE)%>%
              mutate( x = round(rnorm(nrow(.)),1))%>%
              arrange(A,B)


```


```{r mypanderworkaround}

addlabref <- function(fcap, flab){

   if (opts_knit$get("rmarkdown.pandoc.to") %in% 
    c("beamer", "latex")){

    lab <- sprintf("\\label{tab:%s}",flab)

    sprintf("%s %s", lab, fcap)

  }else{

    lab <- sprintf("\\#tab:%s",flab)

    sprintf("<caption>%s %s</caption>", lab, fcap)

  }

}

```


```{r c1}

kable(df, caption = "table with kable")

```

```{r c2}

flextable(df)%>%set_caption(caption = "table with flextable")

```


```{r c3}

pander(df, caption = addlabref("table with pander", "c3") )
```


kable figure is \@ref(tab:c1).

flextable figure is \@ref(tab:c2)

pander figure is \@ref(tab:c3)
Dries
  • 470
  • 4
  • 24

2 Answers2

1

This should help:

Our table:

<br>
<div style="text-align: center">
   <caption><span id="tab:c3">Table 3: </span>My table is here</caption>
</div>

<div style="width: 50%; margin: 0 auto;">
```{r c3}
pander(head(mtcars[1:5]))
```
</div>

Our ref:

pander figure is <a href="#tab:c3">3</a>

...looks like
enter image description here

With your table is a same story:

<br>
<div style="text-align: center">
   <caption><span id="tab:c3">Table 3: </span>My table is here</caption>
</div>

<div style="width: 10%; margin: 0 auto;">
```{r c3}
pander(df)
```
</div>

enter image description here


Works everywhere:

```{r c3}
pander(df, caption = "(\\#tab:table-label) Table caption" )
```

pander figure is \@ref(tab:table-label)
manro
  • 3,529
  • 2
  • 9
  • 22
  • Thanks! The problem is that it cannot easily be incorporated in a flexible output-format approach. Ideally, it would be something to give to `caption = ...` as in my MWE for pander – Dries Jan 13 '22 at 14:06
  • @Dries Do you want to find an universal solution for pdf and html? – manro Jan 13 '22 at 14:08
  • Indeed. I try to have my rmd-docs to be as output format independent as possible – Dries Jan 13 '22 at 14:10
  • @Dries I'll try one thing later. And inform you, – manro Jan 13 '22 at 14:17
  • @Dries Look to an addition – manro Jan 13 '22 at 16:02
  • 1
    Great! I thought I had tried every combination resembling this, but clearly I missed the correct one – Dries Jan 13 '22 at 17:29
  • @Dries No problems ;) Now I know about pander's table too. If not a secret, why do you want to use this table package? KableExtra, gt etc look more powerful... – manro Jan 13 '22 at 18:38
  • 1
    for simple tables, it has better defaults I think. No need e.g. for `booktabs = TRUE` to avoid an awful looking table. Also, I believe there is no quick kable-alternative for `pander(tab, split.table=Inf)`. – Dries Jan 13 '22 at 18:53
  • I needed a leading colon for this to generate the correct `` element and `id` attribute, i.e. `pander(df, caption = ": (\\#tab:table-label) Table caption" )` – Paul Jan 03 '23 at 16:57
1

Based on @manro's answer, I updated the addlabref-function in my MWE to

addlabref <- function(fcap){

sprintf("(\\#tab:%s) %s", knitr::opts_current$get("label"), fcap)

}

By using knitr::opts_current$get("label"), it keeps the behavior of kable and flextable to use the chunk-name as label. This function can be used to add the label to reference to the pander-caption like

pander( df, caption = addlabref("my caption") )
Dries
  • 470
  • 4
  • 24