gt icon indicating copy to clipboard operation
gt copied to clipboard

No escape sign before % in tables generate error when knitting to PDF

Open rjazwiec opened this issue 1 year ago • 0 comments

(Rephrased it for clarity)

Prework

Description

I have problem with tables generated that contains %.

When I manually convert numbers to strings, and add % sign at the end of it - table is generated properly in html, docx and pdf (trough TinyTeX).

When trying to format numerical column with fmt_number(pattern = "{x}%") table is generated in html and docx but breaks in pdf.

Reproducible example

Code:

accuracy <- data.frame(HNK = 105.11, KET = 101.11, NKT = 96.14, DHNK = 96.69)

gt(accuracy) |>
  fmt_number(pattern = "{x}%")

generates LaTeX code:

\begin{longtable*}{rrrr}
\toprule
HNK & KET & NKT & DHNK \\ 
\midrule\addlinespace[2.5pt]
$105.11$% & $101.11$% & $96.14$% & $96.69$% \\ 
\bottomrule
\end{longtable*}

This code gives error mesage:

compilation failed- error
Misplaced \noalign.
\bottomrule ->\noalign 
                       {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
l.252 \bottomrule

When I manually add % sign to numbers it is like follows:

accuracy2 <- data.frame(HNK = "105.11%", KET = "101.11%", NKT = "96.14%", 
    DHNK = "96.69%")

gt(accuracy2)

to LaTeX:

\begin{longtable*}{rrrr}
\toprule
HNK & KET & NKT & DHNK \\ 
\midrule\addlinespace[2.5pt]
105.11\% & 101.11\% & 96.14\% & 96.69\% \\ 
\bottomrule
\end{longtable*}

This table is converted properly to pdf

Expected result

After manual edit of .tex file generated by quarto (after fmt_number) to look like this:

\begin{longtable*}{rrrr}
\toprule
HNK & KET & NKT & DHNK \\ 
\midrule\addlinespace[2.5pt]
$105.11$\% & $101.11$\% & $96.14$\% & $96.69$\% \\ 
\bottomrule
\end{longtable*}

TeXstudio generates proper pdf from it.

Session info

R version 4.3.2 (2023-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=Polish_Poland.utf8 LC_CTYPE=Polish_Poland.utf8 LC_MONETARY=Polish_Poland.utf8 [4] LC_NUMERIC=C LC_TIME=Polish_Poland.utf8

time zone: Europe/Warsaw tzcode source: internal

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

other attached packages: [1] ggfortify_0.4.16 ggtext_0.1.2 investr_1.4.2 wesanderson_0.3.7 hrbrthemes_0.8.0 gt_0.10.0
[7] janitor_2.2.0 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[13] readr_2.1.5 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0

loaded via a namespace (and not attached): [1] gtable_0.3.4 xfun_0.41 tzdb_0.4.0 vctrs_0.6.5 tools_4.3.2
[6] generics_0.1.3 curl_5.2.0 fansi_1.0.6 pkgconfig_2.0.3 lifecycle_1.0.4
[11] compiler_4.3.2 munsell_0.5.0 snakecase_0.11.1 httpuv_1.6.13 fontquiver_0.2.1
[16] fontLiberation_0.1.0 sass_0.4.8 htmltools_0.5.7 Rttf2pt1_1.3.12 pillar_1.9.0
[21] later_1.3.2 crayon_1.5.2 extrafontdb_1.0 ellipsis_0.3.2 gfonts_0.2.0
[26] mime_0.12 fontBitstreamVera_0.1.1 tidyselect_1.2.0 digest_0.6.34 stringi_1.8.3
[31] showtextdb_3.0 extrafont_0.19 fastmap_1.1.1 grid_4.3.2 colorspace_2.1-0
[36] cli_3.6.2 magrittr_2.0.3 crul_1.4.0 utf8_1.2.4 withr_2.5.2
[41] gdtools_0.3.5 scales_1.3.0 promises_1.2.1 bit64_4.0.5 showtext_0.9-6
[46] timechange_0.2.0 rmarkdown_2.25 sysfonts_0.8.8 bit_4.0.5 gridExtra_2.3
[51] hms_1.1.3 shiny_1.8.0 evaluate_0.23 knitr_1.45 rlang_1.1.3
[56] gridtext_0.1.5 Rcpp_1.0.12 xtable_1.8-4 glue_1.7.0 httpcode_0.3.0
[61] xml2_1.3.6 vroom_1.6.5 rstudioapi_0.15.0 jsonlite_1.8.8 R6_2.5.1
[66] systemfonts_1.0.5

rjazwiec avatar Feb 01 '24 14:02 rjazwiec