Skip to content

Commit

Permalink
Get magic_mirror() to handle complex alignment specs like "p{1cm}".
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurdoch committed Dec 7, 2024
1 parent d0c1b7c commit e89deda
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
16 changes: 10 additions & 6 deletions R/magic_mirror.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

kableExtra 1.4.0.9
kableExtra 1.4.0.10
--------------------------------------------------------------------------------

New Features:
Expand All @@ -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
Expand Down

0 comments on commit e89deda

Please sign in to comment.