rstudio/gt

gt table does not respect the headers when knitted to pdf

Closed this issue · 5 comments

I updated gt to the latest version of the package and then I noticed, that my tables float away from their correct position. It looks as if headings were not respected by gt.

Example:

# Title 1

## Title 1.1 

### Title 1.1.1

head(mtcars) %>% gt()

Before the update, my table got correctly rendered within the section Title 1.1.1. Now it floats away before Title 1.
When I uninstalled the latest version of gt and returned to the older version 0.11.0, everything returned to the normal, table is rendered correctly.
Just noting once again that it behaves this way only when document is rendered to pdf, HTML works just fine.

Session info:

locale:
[1] LC_COLLATE=Slovak_Slovakia.utf8  LC_CTYPE=Slovak_Slovakia.utf8    LC_MONETARY=Slovak_Slovakia.utf8 LC_NUMERIC=C                    
[5] LC_TIME=Slovak_Slovakia.utf8    
system code page: 65001

time zone: Europe/Bratislava
tzcode source: internal

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

other attached packages:
 [1] gt_0.11.0        remotes_2.5.0    flextable_0.9.6  huxtable_5.5.7   patchwork_1.3.0  ggh4x_0.2.8      ggtext_0.1.2     hrbrthemes_0.8.7
 [9] rmarkdown_2.28   RPostgres_1.4.7  DBI_1.2.3        here_1.0.1       lubridate_1.9.3  forcats_1.0.0    stringr_1.5.1    dplyr_1.1.4     
[17] purrr_1.0.2      readr_2.1.5      tidyr_1.3.1      tibble_3.2.1     ggplot2_3.5.1    tidyverse_2.0.0  tinytex_0.53     pacman_0.5.1    

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1        farver_2.1.2            blob_1.2.4              fastmap_1.2.0           fontquiver_0.2.1       
 [6] digest_0.6.37           timechange_0.3.0        lifecycle_1.0.4         magrittr_2.0.3          compiler_4.3.1         
[11] rlang_1.1.4             sass_0.4.9              tools_4.3.1             utf8_1.2.4              yaml_2.3.10            
[16] data.table_1.16.2       knitr_1.48              askpass_1.2.1           bit_4.5.0               xml2_1.3.6             
[21] pkgload_1.4.0           withr_3.0.1             grid_4.3.1              fansi_1.0.6             gdtools_0.4.0          
[26] colorspace_2.1-1        extrafontdb_1.0         scales_1.3.0            cli_3.6.1               crayon_1.5.3           
[31] ragg_1.3.3              generics_0.1.3          rstudioapi_0.16.0       tzdb_0.4.0              commonmark_1.9.2       
[36] cachem_1.1.0            assertthat_0.2.1        vctrs_0.6.5             jsonlite_1.8.9          fontBitstreamVera_0.1.1
[41] hms_1.1.3               bit64_4.5.2             systemfonts_1.1.0       jquerylib_0.1.4         glue_1.8.0             
[46] stringi_1.8.4           gtable_0.3.5            extrafont_0.19          munsell_0.5.1           pillar_1.9.0           
[51] htmltools_0.5.8.1       openssl_2.2.2           R6_2.5.1                textshaping_0.4.0       rprojroot_2.0.4        
[56] evaluate_1.0.1          markdown_1.13           gridtext_0.1.5          fontLiberation_0.1.0    bslib_0.8.0            
[61] Rcpp_1.0.13             zip_2.3.1               uuid_1.2-1              Rttf2pt1_1.3.12         officer_0.6.7          
[66] xfun_0.47               pkgconfig_2.0.3        

My header:

---
title: "Report for `r lubridate::month(Sys.time(), label = TRUE, abb = TRUE)` `r lubridate::year(Sys.time())`" 
mainfont: Roboto
geometry: "left=1cm,right=1cm,top=2cm,bottom=2cm"
output:
  pdf_document:
    includes:
      in_header: 'gt_packages.sty'
    extra_dependencies: ["xcolor", "float"]
    toc: false
    toc_depth: 3
    number_sections: true
    fig_caption: true
  html_notebook: default
  html_document:
    df_print: paged
    toc: true
    number_sections: true
    fig_caption: true
params:
  doc_version: paid
header-includes:
      - \usepackage{caption} 
---

Thank you.

We changed the default environment to using flaoting tables.

You can use the following.

library(gt)
exibble |> 
  gt() |>
  tab_options(latex.use_longtable = TRUE)

Or if you have multiple tables, you van define this in the setup chunk

gt <- function(...) {
  gt(...) |> tab_options(latex.use_longtable = TRUE)
}

There is also the latex.tbl_pos option. tab_options(latex.tbl.pos = "h!") could help you if you want to use floating tables

@olivroy : Thank you for your response.
So this behavior is an intention. In my environment all tables get floated before the nearest H1 heading and don´t respect sections. What is the meaning of such behavior, who would want it for what purpose please?

Setting using latex.use_longtable = TRUE in each table tab_options() works fine, tables behave as needed. Thanks.
However I have a lot of tables so setting this for all of them at one place would be desired. Your solution with function in setup chunk does not work for me unfortunately.
I am getting error:

processing file: presentation_intro.Rmd
  |........................                           |  48% [test]            Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
                                                                                                             

Quitting from lines 141-142 [test] (presentation_intro.Rmd)
Execution halted

Do you have any idea what is wrong please?
Thank you

Apologies, you have to pass another argument at least.

gt <- function(..., longtable = TRUE) {
  gt(...) |> tab_options(latex.use_longtable = longtable)
}

I tested that this works!

@olivroy It works, thank you very much for your help. All the best!