davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
554 stars 79 forks source link

Flextable breaks auto-numbering when floating in Quarto pdf #582

Closed atchley-sha closed 7 months ago

atchley-sha commented 11 months ago

When setting float = "float" in either set_flextable_defaults() or set_table_properties(opts_pdf = list()) in a Quarto document, flextable sets the table in a longtable environment within a table environment. My understanding is that longtable is not intended to float (see e.g. https://tex.stackexchange.com/a/266698), and so this should likely be tabular or something similar.

The tables do seem to float correctly despite this, but the auto-numbering captions skip numbers, I assume because LaTeX sees two "tables" per flextable, one with table (captioned) and another with longtable (uncaptioned).

E.g.:

---
format: pdf
keep-tex: true
---

```{r}
library(flextable)
set_flextable_defaults(float = "float")
```

```{r}
#| label: tbl-ft1
#| tbl-cap: "ft1"

flextable(head(mtcars))
```

```{r}
#| label: tbl-ft2
#| tbl-cap: "ft2"

flextable(head(mtcars))
```

@tbl-ft1 @tbl-ft2

produces:

...
\begin{table}
\caption{\label{tbl-ft1}ft1 }\tabularnewline

\providecommand{\ascline}[3]{\noalign{\global\arrayrulewidth 1}\arrayrulecolor[HTML]{#2}\cline{#3}}

\begin{longtable}[c]{|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}|p{0.75in}}

<<table content>>
...

and Screenshot 2023-10-19 at 16 09 02

I believe this numbering issue would be fixed using tabular or something similar with floating tables, as this is how e.g. kableExtra does it and the numbering works fine there.

sessionInfo():

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.0

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Denver
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] flextable_0.9.4.001

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.7          compiler_4.3.1          crayon_1.5.2           
 [4] promises_1.2.1          zip_2.3.0               Rcpp_1.0.11            
 [7] xml2_1.3.5              later_1.3.1             fontquiver_0.2.1       
[10] systemfonts_1.0.5       textshaping_0.3.7       uuid_1.1-1             
[13] fastmap_1.1.1           mime_0.12               R6_2.5.1               
[16] gfonts_0.2.0            gdtools_0.3.4           curl_5.1.0             
[19] knitr_1.44              openssl_2.1.1           crul_1.4.0             
[22] shiny_1.7.5.1           rlang_1.1.1             httpcode_0.3.0         
[25] httpuv_1.6.11           xfun_0.40               cli_3.6.1              
[28] magrittr_2.0.3          digest_0.6.33           grid_4.3.1             
[31] rstudioapi_0.15.0       xtable_1.8-4            askpass_1.2.0          
[34] lifecycle_1.0.3         evaluate_0.22           glue_1.6.2             
[37] data.table_1.14.8       fontLiberation_0.1.0    officer_0.6.2          
[40] ragg_1.2.6              fontBitstreamVera_0.1.1 rmarkdown_2.25         
[43] tools_4.3.1             ellipsis_0.3.2          htmltools_0.5.6.1

This also occurs with the current CRAN version of flextable (0.9.3)

davidgohel commented 11 months ago

thanks for the report and the insight, we will try to fix

davidgohel commented 9 months ago

I'm not sure how to deal with this problem. It only happens with Quarto and not with 'R Markdown'.

Capture d’écran 2023-12-10 à 22 08 58

Quarto doesn't seem to detect the structure as 'R Markdown' does.

The impact of changing from the 'longtable' environment to 'tabular' (if it works) only for quarto is quite substantial in terms of time. When I tried to test the dev version, I also noticed that PDF captions no longer worked and that an error appeared even without specifying a floating type.

So we will wait for a new stable release of quarto and adapt as we can when the new version will be ready.

davidgohel commented 7 months ago

This issue can be closed, it is now working as expected with Quarto 1.4.549

github-actions[bot] commented 4 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.