From e89deda095427443b261b38467c5a31dc859aaa0 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Sat, 7 Dec 2024 05:52:54 -0500 Subject: [PATCH] Get magic_mirror() to handle complex alignment specs like "p{1cm}". --- DESCRIPTION | 2 +- R/magic_mirror.R | 16 ++++++++++------ inst/NEWS.md | 4 +++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index efe88dc..659ba49 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: kableExtra Type: Package Title: Construct Complex Table with 'kable' and Pipe Syntax -Version: 1.4.0.9 +Version: 1.4.0.10 Authors@R: c( person('Hao', 'Zhu', email = 'haozhu233@gmail.com', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-3386-6076')), diff --git a/R/magic_mirror.R b/R/magic_mirror.R index edfd56b..d830157 100644 --- a/R/magic_mirror.R +++ b/R/magic_mirror.R @@ -52,15 +52,19 @@ magic_mirror_latex <- function(kable_input){ # Booktabs table_info$booktabs <- grepl(toprule_regexp, kable_input) - # Align - table_info$align <- gsub("\\|", "", str_match( - kable_input, paste0("\\\\begin\\{", - table_info$tabular,"\\}.*\\{(.*?)\\}"))[2]) - table_info$align_vector <- unlist(strsplit(table_info$align, "")) + # Alignment is a sequence with each element being a single letter, or a + # single letter followed by a measurement in braces, e.g. "p{1cm}" + align1 <- "[[:alpha:]](?:\\{[^{}]*\\})?\\|*" + align_pattern <- paste0("(?:", align1, ")*") + align <- str_match(kable_input, + paste0("\\\\begin\\{", + table_info$tabular,"\\}[^{]*\\{(", align_pattern, ")\\}"))[1,2] + table_info$align <- gsub("\\|", "", align) + table_info$align_vector <- regmatches(table_info$align, gregexpr("[[:alpha:]](\\{[^{}]*\\})?", table_info$align))[[1]] table_info$align_vector_origin <- table_info$align_vector # valign table_info$valign <- gsub("\\|", "", str_match( - kable_input, paste0("\\\\begin\\{", table_info$tabular,"\\}(.*)\\{.*?\\}"))[2]) + kable_input, paste0("\\\\begin\\{", table_info$tabular,"\\}([^{]*)\\{.*?\\}"))[2]) table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign) table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2) table_info$valign3 <- sub("\\[", "", table_info$valign) diff --git a/inst/NEWS.md b/inst/NEWS.md index 2a7b677..5626943 100644 --- a/inst/NEWS.md +++ b/inst/NEWS.md @@ -1,5 +1,5 @@ -kableExtra 1.4.0.9 +kableExtra 1.4.0.10 -------------------------------------------------------------------------------- New Features: @@ -24,6 +24,8 @@ documents (#836). in the wrong directory if an error occurred (#865). * Fixed a bug in `magic_mirror_latex()` which stopped it from working with `tabularx` tables (#861). +* Complex alignment options like `p{1cm}` were not +handled properly by `kable_styling()` (#876). kableExtra 1.4.0