Skip to content

as_latex is latex-escaping #1912

Closed
Closed

Description

Prework

Description

THe function as_latex is converting \ into \\textbackslash{}, { into \{ and does other latex-escaping that can't be avoided

Reproducible example

  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.
library(gt)
gtcars |>
  dplyr::select(mfr, model, msrp) |>
  dplyr::slice(1:5) |>
  gt() |>
  tab_header(
    title = "\\textbf{gtcars}",
    subtitle = md("`gtcars` is an R dataset")
  ) |>
  as_latex() |>
  as.character()

Actual result

"\\begin{table}[!t]\n\\caption*{\n{\\large \\textbackslash{}textbf\\{gtcars\\}} \\\\ \n{\\small \\texttt{gtcars} is an R dataset}\n} \n\\fontsize{12.0pt}{14.4pt}\\selectfont\n\\begin{tabular*}{\\linewidth}{@{\\extracolsep{\\fill}}llr}\n\\toprule\nmfr & model & msrp \\\\ \n\\midrule\\addlinespace[2.5pt]\nFord & GT & 447000 \\\\ \nFerrari & 458 Speciale & 291744 \\\\ \nFerrari & 458 Spider & 263553 \\\\ \nFerrari & 458 Italia & 233509 \\\\ \nFerrari & 488 GTB & 245400 \\\\ \n\\bottomrule\n\\end{tabular*}\n\\end{table}\n"

As you can see, \ was converted into \\textbackslash{}

Expected result

There should be a escape option, like there is for knitr::kable, that prevents escaping.

Yes, I know I can use md("**my title**"), but my aim is to output raw LaTeX code that do not have a corresponding Markdown code, in particular for newlines in cell outputs for example.

Session info

> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.12.0 
LAPACK: /usr/lib/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_DK.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Rome
tzcode source: system (glibc)

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

other attached packages:
 [1] gt_0.11.1            labelled_2.13.0      tmap_3.3-4           gtsummary_2.0.3.9003
 [5] magrittr_2.0.3       lubridate_1.9.3      forcats_1.0.0        stringr_1.5.1       
 [9] dplyr_1.1.4          purrr_1.0.2          readr_2.1.5          tidyr_1.3.1         
[13] tibble_3.2.1         ggplot2_3.5.1        tidyverse_2.0.0     

loaded via a namespace (and not attached):
  [1] DBI_1.2.3          httr2_1.0.5        tmaptools_3.1-1    s2_1.1.7           readxl_1.4.3      
  [6] rlang_1.1.4        e1071_1.7-16       compiler_4.4.1     roxygen2_7.3.2     png_0.1-8         
 [11] vctrs_0.6.5        wk_0.9.4           pkgconfig_2.0.3    fastmap_1.2.0      backports_1.5.0   
 [16] lwgeom_0.2-14      giscoR_0.6.0       leafem_0.2.3       ISOweek_0.6-2      geojsonsf_2.0.3   
 [21] utf8_1.2.4         rmarkdown_2.28     markdown_1.13      tzdb_0.4.0         haven_2.5.4       
 [26] xfun_0.48          jsonlite_1.8.9     RefManageR_1.4.0   minty_0.0.1        terra_1.7-83      
 [31] broom_1.0.7        parallel_4.4.1     R6_2.5.1           stringi_1.8.4      RColorBrewer_1.1-3
 [36] pkgload_1.4.0      cellranger_1.1.0   stars_0.6-6        assertthat_0.2.1   Rcpp_1.0.13       
 [41] knitr_1.48         base64enc_0.1-3    timechange_0.3.0   tidyselect_1.2.1   rstudioapi_0.17.1 
 [46] dichromat_2.0-0.1  abind_1.4-8        codetools_0.2-20   curl_5.2.3         lattice_0.22-6    
 [51] leafsync_0.1.0     plyr_1.8.9         withr_3.0.1        evaluate_1.0.1     sf_1.0-18         
 [56] units_0.8-5        proxy_0.4-27       zip_2.3.1          xml2_1.3.6         pillar_1.9.0      
 [61] eurostat_4.0.0     KernSmooth_2.23-24 generics_0.1.3     rprojroot_2.0.4    sp_2.1-4          
 [66] hms_1.1.3          munsell_0.5.1      regions_0.1.8      commonmark_1.9.2   scales_1.3.0      
 [71] readODS_2.3.0      class_7.3-22       glue_1.8.0         tools_4.4.1        data.table_1.16.2 
 [76] fs_1.6.4           XML_3.99-0.17      grid_4.4.1         bibtex_0.5.1       crosstalk_1.2.1   
 [81] cards_0.3.0        colorspace_2.1-1   raster_3.6-30      shrtcts_0.1.2      cardx_0.2.1       
 [86] cli_3.6.3          rappdirs_0.3.3     fansi_1.0.6        viridisLite_0.4.2  countrycode_1.6.0 
 [91] gtable_0.3.5       sass_0.4.9         digest_0.6.37      classInt_0.4-10    htmlwidgets_1.6.4 
 [96] htmltools_0.5.8.1  lifecycle_1.0.4    leaflet_2.2.2      here_1.0.1         httr_1.4.7

Activity

  1. olivroy commented on Oct 28, 2024

    @olivroy
  2. ghost closed this as completedon Oct 28, 2024
  3. ghost reopened this on Oct 28, 2024
  4. rich-iannone commented on Oct 28, 2024

    @rich-iannone
    Member

    I think it makes sense to have a latex() helper function that works similarly to html(), where escaping doesn’t happen for the targeted output table.

  5. added this to the v0.12.0 milestone on Oct 28, 2024
  6. moved this from Backlog to To discuss / To plan in R Markdown Team Projectson Oct 28, 2024
  7. egouldo commented on Dec 4, 2025

    @egouldo

    I think it makes sense to have a latex() helper function that works similarly to html(), where escaping doesn’t happen for the targeted output table.

    So, to clarify, does that mean wrapping target text in latex(), and then calling as_latex() on the gt table object?

  8. rich-iannone commented on Aug 25, 2026

    @rich-iannone
    Member

    This has been resolved in gt v1.2.0. Solve by wrapping raw LaTeX strings in latex() (same pattern as wrapping raw HTML strings in html()).

  9. moved this from To discuss / To plan to Done in R Markdown Team Projectson Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions