Skip to content

Commit

Permalink
fix #1595 by automatically adding {} before [
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Apr 21, 2021
1 parent 139d913 commit b09cbc1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.32.5
Version: 1.32.6
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## BUG FIXES

- Reverted the fix for #1595 since it caused problems in kableExtra (thanks, @bttomio, haozhu233/kableExtra#607).
- Reverted the fix for #1595 since it caused problems in **kableExtra** (thanks, @bttomio, haozhu233/kableExtra#607), and applied a different fix to the original problem (i.e., add `{}` before `[`).

## MINOR CHANGES

Expand Down
13 changes: 11 additions & 2 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ kable_latex = function(
} else rep('', nrow(x))
linesep = ifelse(linesep == "", linesep, paste0('\n', linesep))

if (escape) x = escape_latex(x)
x = escape_latex_table(x, escape, booktabs)
if (!is.character(toprule)) toprule = NULL
if (!is.character(bottomrule)) bottomrule = NULL
tabular = if (longtable) 'longtable' else 'tabular'
Expand All @@ -318,7 +318,7 @@ kable_latex = function(
if (longtable && cap != '') c(cap, '\\\\'),
sprintf('\n%s', toprule), '\n',
if (!is.null(cn <- colnames(x))) {
if (escape) cn = escape_latex(cn)
cn = escape_latex_table(cn, escape, booktabs)
paste0(paste(cn, collapse = ' & '), sprintf('\\\\\n%s\n', midrule))
},
one_string(apply(x, 1, paste, collapse = ' & '), sprintf('\\\\%s', linesep), sep = ''),
Expand All @@ -328,6 +328,15 @@ kable_latex = function(
), collapse = '')
}

# when using booktabs, add {} before [ so that the content in [] won't be
# treated as parameters of booktabs commands like \midrule:
# https://github.com/yihui/knitr/issues/1595
escape_latex_table = function(x, escape = TRUE, brackets = TRUE) {
if (escape) x = escape_latex(x)
if (brackets) x = gsub('^(\\s*)(\\[)', '\\1{}\\2', x)
x
}

kable_latex_caption = function(x, caption) {
paste(c(
'\\begin{table}\n', sprintf('\\caption{%s}\n', caption), x, '\n\\end{table}'
Expand Down
13 changes: 13 additions & 0 deletions tests/testit/test-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ x & col_name\\\\
)
})

assert('kable() adds {} before [] when booktabs = TRUE', {
(kable2(data.frame(x = c('[0, 1]', '(1, 2]'), y = c(35, 62)), 'latex', booktabs = TRUE) %==% '
\\begin{tabular}{lr}
\\toprule
x & y\\\\
\\midrule
{}[0, 1] & 35\\\\
(1, 2] & 62\\\\
\\bottomrule
\\end{tabular}'
)
})

assert('kable(format = "latex", linesep = ...) works', {
(kable2(data.frame(x = 1:4), 'latex', linesep = c('', '', '\\midrule')) %==% '
\\begin{tabular}{r}
Expand Down

0 comments on commit b09cbc1

Please sign in to comment.