Skip to content

Commit 8ea6180

Browse files
Add test for unicodes. unicode conversion is not exhaustive, so
1 parent 53c947b commit 8ea6180

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

R/helpers.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,6 +3311,7 @@ latex_unicode_chars <- c(
33113311
"\U03CD"="\\ensuremath{\\acute\\upsilon}", # GREEK SMALL LETTER UPSILON WITH TONOS [�\u008d]",
33123312
"\U03CE"="\\ensuremath{\\acute\\omega}", # GREEK SMALL LETTER OMEGA WITH TONOS [�\u008e]",
33133313

3314+
"\U0251"='\\ensuremath{\\alpha}', # Greek Small Letter Alpha",
33143315
"\U03B1"='\\ensuremath{\\alpha}', # Greek Small Letter Alpha",
33153316
"\U03B2"='\\ensuremath{\\beta}',
33163317
"\U03B3"='\\ensuremath{\\gamma}',
@@ -4090,8 +4091,8 @@ latex_unicode_chars <- c(
40904091
"\U2AFD"='\\ensuremath{{{/}\\!\\!{/}}}', # DOUBLE SOLIDUS OPERATOR [⫽]")
40914092

40924093
# CJK Symbols Punktuation (!) U+3000 : for \langle/\rangle
4093-
"\U3008"='\\ensuremath{\langle}',
4094-
"\U3009"='\\ensuremath{\rangle}'
4094+
"\U3008"='\\ensuremath{\\langle}',
4095+
"\U3009"='\\ensuremath{\\rangle}'
40954096
)
40964097

40974098
# escape_latex() ---------------------------------------------------------------
@@ -4142,7 +4143,7 @@ escape_latex <- function(text) {
41424143

41434144
regmatches(text[!na_text], m) <- escaped_chars
41444145

4145-
m2 <- gregexpr(paste0(names(latex_unicode_chars),collapse = "|"), text[!na_text], perl = TRUE)
4146+
m2 <- gregexpr(paste0("[",paste0(names(latex_unicode_chars), collapse = ""),"]"), text[!na_text], perl = TRUE)
41464147

41474148
unicode_chars <- regmatches(text[!na_text], m2)
41484149

tests/testthat/test-as_latex.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,36 @@ test_that("Table can apply font sizes", {
356356
expect_snapshot(gt_latex_font_size_10_subtitle_20)
357357

358358
})
359+
360+
test_that("Tables with unicode characters were acceptable and updated to latex compliant code", {
361+
362+
gt_tbl_unicode <- data.frame( unicode = c("ɑβΔ")) %>%
363+
gt() %>%
364+
tab_header(
365+
title = "≥ unicode text",
366+
subtitle = "≠ not equal"
367+
) %>%
368+
as_latex()
369+
370+
## title should be \\ensuremath{\\geq} unicode text
371+
expect_match(
372+
gt_tbl_unicode %>% as.character(),
373+
"\\ensuremath{\\geq} unicode text",
374+
fixed = TRUE
375+
)
376+
377+
## subtitle should be \\ensuremath{\\neq} not equal
378+
expect_match(
379+
gt_tbl_unicode %>% as.character(),
380+
"\\ensuremath{\\neq} not equal",
381+
fixed = TRUE
382+
)
383+
384+
## content should be \\\ensuremath{\\alpha}\\ensuremath{\\beta}\\ensuremath{\\Delta}
385+
expect_match(
386+
gt_tbl_unicode %>% as.character(),
387+
"\n\\ensuremath{\\alpha}\\ensuremath{\\beta}\\ensuremath{\\Delta} \\\\",
388+
fixed = TRUE
389+
)
390+
391+
})

0 commit comments

Comments
 (0)