Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get magic_mirror() to handle complex alignment specs like "p{1cm}". #877

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 13 additions & 7 deletions R/magic_mirror.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ 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 All @@ -69,7 +75,7 @@ magic_mirror_latex <- function(kable_input){
table_info$valign2)
table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}")
# N of columns
table_info$ncol <- nchar(table_info$align)
table_info$ncol <- length(table_info$align_vector)
# Caption
if (str_detect(kable_input, "caption\\[")) {
caption_line <- str_match(kable_input, "\\\\caption(.*)\\n")[2]
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
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/bugfix.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Issue #876: complex alignment

Code
kbl(x = mtcars[1:2, 1:2], format = "latex", align = rep("p{2cm}", 2)) %>%
kable_styling(latex_options = "scale_down")
Output
\begin{table}
\centering
\resizebox{\ifdim\width>\linewidth\linewidth\else\width\fi}{!}{
\begin{tabular}[t]{l|p{2cm}|p{2cm}}
\hline
& mpg & cyl\\
\hline
Mazda RX4 & 21 & 6\\
\hline
Mazda RX4 Wag & 21 & 6\\
\hline
\end{tabular}}
\end{table}

# Issue #861: pack_rows with tabularx

Code
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/row_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
1 & 4\\
2 & 5\\
3 & 6\\
\midrule\\
\midrule
4 & 7\\
\bottomrule
\end{tabular}
Expand Down Expand Up @@ -147,7 +147,7 @@
\begin{tabular}[t]{>{\centering\arraybackslash}p{5em}cc}
\toprule
C2 & C3 & C4\\
\rowcolor{gray!6}\\
\rowcolor{gray!6}
\midrule
& 1 & 0\\

Expand All @@ -167,25 +167,25 @@
& 6 & 0\\

\rowcolor{gray!6}
\multirow{-7}{5em}{\centering\arraybackslash \textbf{c}} & 7 & 1\\
\multirow{-7}{5em}[\normalbaselineskip]{\centering\arraybackslash \textbf{c}} & 7 & 1\\

& 8 & 0\\

& 9 & 1\\

\multirow{-3}{5em}{\centering\arraybackslash \textbf{d}} & 10 & 0\\
\multirow{-3}{5em}[\normalbaselineskip]{\centering\arraybackslash \textbf{d}} & 10 & 0\\

\rowcolor{gray!6}
& 11 & 1\\

\rowcolor{gray!6}
\multirow{-2}{5em}{\centering\arraybackslash \textbf{c}} & 12 & 1\\
\multirow{-2}{5em}[\normalbaselineskip]{\centering\arraybackslash \textbf{c}} & 12 & 1\\

& 13 & 0\\

& 14 & 0\\

\multirow{-3}{5em}{\centering\arraybackslash \textbf{d}} & 15 & 1\\
\multirow{-3}{5em}[\normalbaselineskip]{\centering\arraybackslash \textbf{d}} & 15 & 1\\
\bottomrule
\end{tabular}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-bugfix.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
test_that("Issue #876: complex alignment", {
expect_snapshot(
kbl(x = mtcars[1:2,1:2],
format = "latex",
align = rep("p{2cm}",2)) %>%
kable_styling(latex_options = "scale_down")
)
})


test_that("Issue #861: pack_rows with tabularx", {
expect_snapshot(
kbl(mtcars, format="latex", tabular = "tabularx",
Expand Down
Loading