Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve caption of <gt_tbl> object when generating LaTeX code with gt::as_latex() #1610

Open
2 tasks done
ami-null opened this issue Mar 20, 2024 · 0 comments
Open
2 tasks done
Assignees

Comments

@ami-null
Copy link

ami-null commented Mar 20, 2024

Prework

Proposal

gt::as_latex() drops the caption in the gt_tbl object:

dt <- mtcars[1:2, 1:2]
gt::gt(data = dt, caption = "mtcars data") |> 
    gt::as_latex() |> 
    as.character()
#> [1] "\\begin{longtable}{rr}\n\\toprule\nmpg & cyl \\\\ \n\\midrule\\addlinespace[2.5pt]\n21 & 6 \\\\ \n21 & 6 \\\\ \n\\bottomrule\n\\end{longtable}\n"

gt::gt(data = dt) |> 
    gt::tab_caption("mtcars data") |> 
    gt::as_latex() |> 
    as.character()
#> [1] "\\begin{longtable}{rr}\n\\toprule\nmpg & cyl \\\\ \n\\midrule\\addlinespace[2.5pt]\n21 & 6 \\\\ \n21 & 6 \\\\ \n\\bottomrule\n\\end{longtable}\n"

As can be seen in the output, there is no \caption{} command in the LaTeX code that is generated. This is an issue because my workflow is saving the generated LaTeX code string to a file and then calling \input{} on that file in LaTeX, and then I end up getting a table with no captions. This also prevents cross-referencing tables in PDF documents generated using Rmarkdown or Quarto.

Interestingly enough, when the title and subtitle in gt::tab_header() are retained, but through an un-numbered \caption*{} in the LaTeX code:

gt::gt(data = dt) |> 
    gt::tab_header(
        title = gt::md("tab_header title"),
        subtitle = gt::md("tab_header subtitle")
    ) |> 
    gt::as_latex() |> 
    as.character()
#> [1] "\\begin{longtable}{rr}\n\\caption*{\n{\\large tab\\_header title} \\\\ \n{\\small tab\\_header subtitle}\n} \\\\ \n\\toprule\nmpg & cyl \\\\ \n\\midrule\\addlinespace[2.5pt]\n21 & 6 \\\\ \n21 & 6 \\\\ \n\\bottomrule\n\\end{longtable}\n"

I understand that this a possible duplicate of #818. But, I am opening this one seeing that #818 is almost 3 years old.

sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: 
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.2         gt_0.10.1         knitr_1.45       
#>  [5] rlang_1.1.3       xfun_0.42         purrr_1.0.2       styler_1.10.2    
#>  [9] generics_0.1.3    glue_1.7.0        markdown_1.12     htmltools_0.5.7  
#> [13] fansi_1.0.6       rmarkdown_2.26    R.cache_0.16.0    tibble_3.2.1     
#> [17] evaluate_0.23     fastmap_1.1.1     yaml_2.3.8        lifecycle_1.0.4  
#> [21] compiler_4.3.3    dplyr_1.1.4       fs_1.6.3          pkgconfig_2.0.3  
#> [25] rstudioapi_0.15.0 R.oo_1.26.0       R.utils_2.12.3    digest_0.6.34    
#> [29] R6_2.5.1          tidyselect_1.2.0  utf8_1.2.4        reprex_2.1.0     
#> [33] pillar_1.9.0      commonmark_1.9.1  magrittr_2.0.3    R.methodsS3_1.8.2
#> [37] tools_4.3.3       withr_3.0.0       xml2_1.3.6

Created on 2024-03-20 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants