rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
1.99k stars 201 forks source link

LaTeX Table Position Argument not used in qmd #1853

Open szimmer opened 3 weeks ago

szimmer commented 3 weeks ago

Prework

Description

I am using the most recent development version of gt which includes an argument in tab_options() for latex.tbl.pos. It works interactively but when I use in qmd, the position argument is no longer there.

Reproducible example

Here's a simple qmd document.

---
title: "gt latex examp"
format: 
  pdf:
    keep-tex: true
    header-includes:
      - \usepackage{booktabs}
      - \usepackage{caption}
      - \usepackage{longtable}
      - \usepackage{colortbl}
      - \usepackage{array}
      - \usepackage{anyfontsize}
      - \usepackage{multirow}
---

## Example code

```{r}
#| label: make-table

library(gt)

# Modify the `airquality` dataset by adding the year
# of the measurements (1973) and limiting to 10 rows
airquality_m <- 
  airquality |>
  dplyr::mutate(Year = 1973L) |>
  dplyr::slice(1:10)

# Create a display table using the `airquality`
# dataset; arrange columns into groups
gt_tbl <- 
  gt(airquality_m) 
#| label: tabprint
#| results: asis
gt_tbl |>
  tab_options(latex.tbl.pos = "h!", latex.use_longtable = FALSE) |>
  as_latex()

The table rendered in the .tex code is:

\begin{table} \fontsize{12.0pt}{14.4pt}\selectfont \begin{tabular}{\linewidth}{@{\extracolsep{\fill}}rrrrrrr} \toprule Ozone & Solar.R & Wind & Temp & Month & Day & Year \ \midrule\addlinespace[2.5pt] 41 & 190 & 7.4 & 67 & 5 & 1 & 1973 \ 36 & 118 & 8.0 & 72 & 5 & 2 & 1973 \ 12 & 149 & 12.6 & 74 & 5 & 3 & 1973 \ 18 & 313 & 11.5 & 62 & 5 & 4 & 1973 \ NA & NA & 14.3 & 56 & 5 & 5 & 1973 \ 28 & NA & 14.9 & 66 & 5 & 6 & 1973 \ 23 & 299 & 8.6 & 65 & 5 & 7 & 1973 \ 19 & 99 & 13.8 & 59 & 5 & 8 & 1973 \ 8 & 19 & 20.1 & 61 & 5 & 9 & 1973 \ NA & 194 & 8.6 & 69 & 5 & 10 & 1973 \ \bottomrule \end{tabular} \end{table}


Running in R rather than qmd yields the following:

``` r
library(gt)

# Modify the `airquality` dataset by adding the year
# of the measurements (1973) and limiting to 10 rows
airquality_m <- 
  airquality |>
  dplyr::mutate(Year = 1973L) |>
  dplyr::slice(1:10)

# Create a display table using the `airquality`
# dataset; arrange columns into groups
gt_tbl <- 
  gt(airquality_m) 

gt_tbl |>
  tab_options(latex.tbl.pos = "h!", latex.use_longtable = FALSE) |>
  as_latex() |>
  cat()
#> \begin{table}[h!]
#> \fontsize{12.0pt}{14.4pt}\selectfont
#> \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}rrrrrrr}
#> \toprule
#> Ozone & Solar.R & Wind & Temp & Month & Day & Year \\ 
#> \midrule\addlinespace[2.5pt]
#> 41 & 190 & 7.4 & 67 & 5 & 1 & 1973 \\ 
#> 36 & 118 & 8.0 & 72 & 5 & 2 & 1973 \\ 
#> 12 & 149 & 12.6 & 74 & 5 & 3 & 1973 \\ 
#> 18 & 313 & 11.5 & 62 & 5 & 4 & 1973 \\ 
#> NA & NA & 14.3 & 56 & 5 & 5 & 1973 \\ 
#> 28 & NA & 14.9 & 66 & 5 & 6 & 1973 \\ 
#> 23 & 299 & 8.6 & 65 & 5 & 7 & 1973 \\ 
#> 19 & 99 & 13.8 & 59 & 5 & 8 & 1973 \\ 
#> 8 & 19 & 20.1 & 61 & 5 & 9 & 1973 \\ 
#> NA & 194 & 8.6 & 69 & 5 & 10 & 1973 \\ 
#> \bottomrule
#> \end{tabular*}
#> \end{table}

Created on 2024-08-21 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.4.0 (2024-04-24 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/New_York #> date 2024-08-21 #> pandoc 3.1.11 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.4.0) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.4.0) #> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.4.0) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.4.0) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.4.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.4.0) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.4.0) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.0) #> gt * 0.11.0.9000 2024-08-21 [1] Github (rstudio/gt@28de628) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0) #> knitr 1.46 2024-04-06 [1] CRAN (R 4.4.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.4.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.4.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.4.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.4.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.4.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.4.0) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.4.0) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.4.0) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.4.0) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> styler 1.10.3 2024-04-07 [1] CRAN (R 4.4.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.4.0) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.4.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.4.0) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.4.0) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.4.0) #> xfun 0.43 2024-03-25 [1] CRAN (R 4.4.0) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.4.0) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.4.0) #> #> [1] C:/Users/steph/AppData/Local/R/win-library/4.4 #> [2] C:/Program Files/R/R-4.4.0/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

Expected result

In the tex file, I would expect to see the same exact lines as when running in .R.

Session info

End the reproducible example with a call to sessionInfo() in the same session (e.g. reprex(session_info = TRUE)) and include the output.

szimmer commented 3 weeks ago

Noted to add this doesn't get removed in Rmd.

olivroy commented 3 weeks ago

Does the following work as expected?

---
title: "gt latex examp"
format: 
  pdf:
    keep-tex: true
    header-includes:
      - \usepackage{booktabs}
      - \usepackage{caption}
      - \usepackage{longtable}
      - \usepackage{colortbl}
      - \usepackage{array}
      - \usepackage{anyfontsize}
      - \usepackage{multirow}
---

## Example code

```{r}
#| label: make-table

library(gt)

# Modify the `airquality` dataset by adding the year
# of the measurements (1973) and limiting to 10 rows
airquality_m <- 
  airquality |>
  dplyr::mutate(Year = 1973L) |>
  dplyr::slice(1:10)

# Create a display table using the `airquality`
# dataset; arrange columns into groups
gt_tbl <- 
  gt(airquality_m) 
#| label: tabprint
#| tbl-pos: "h!"
gt_tbl
szimmer commented 3 weeks ago

Does the following work as expected?

---
title: "gt latex examp"
format: 
  pdf:
    keep-tex: true
    header-includes:
      - \usepackage{booktabs}
      - \usepackage{caption}
      - \usepackage{longtable}
      - \usepackage{colortbl}
      - \usepackage{array}
      - \usepackage{anyfontsize}
      - \usepackage{multirow}
---

## Example code

```{r}
#| label: make-table

library(gt)

# Modify the `airquality` dataset by adding the year
# of the measurements (1973) and limiting to 10 rows
airquality_m <- 
  airquality |>
  dplyr::mutate(Year = 1973L) |>
  dplyr::slice(1:10)

# Create a display table using the `airquality`
# dataset; arrange columns into groups
gt_tbl <- 
  gt(airquality_m) 
#| label: tabprint
#| tbl-pos: "h!"
gt_tbl

Unfortunately, this doesn't work. The position parameter is not in the tex file.

nielsbock commented 2 weeks ago

latex.tbl.pos is disabled in Quarto so it's not conflicting with the tbl-pos chunk option. The issue here is that table labels in Quarto should start with the prefix tbl-. This should preserve the latex table position in the .tex file in your first example:

#| label: tbl-tabprint
#| tbl-pos: h!
#| results: asis
gt_tbl |>
  as_latex()