From 2091587a6ecf54c1f534577d34a48a3939bc421f Mon Sep 17 00:00:00 2001
From: "Pavel N. Krivitsky"
Date: Tue, 19 May 2020 15:57:45 +1000
Subject: [PATCH] For LaTeX output, kable now typesets numbers in math mode for
better-looking minus signs. Commas, if present (as decimal or thousands
separator), are wrapped in braces to prevent additional space being added
after. Improves yihui#1709.
---
R/table.R | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/R/table.R b/R/table.R
index a6a6c81edc..a604d9d2d2 100644
--- a/R/table.R
+++ b/R/table.R
@@ -144,6 +144,7 @@ kable = function(
x = cbind(' ' = rownames(x), x)
if (!is.null(col.names)) col.names = c(' ', col.names)
if (!is.null(align)) align = c('l', align) # left align row names
+ isn = c(FALSE, isn)
}
n = nrow(x)
x = replace_na(to_character(x), is.na(x))
@@ -155,7 +156,7 @@ kable = function(
attr(x, 'align') = align
res = do.call(
paste('kable', format, sep = '_'),
- list(x = x, caption = caption, escape = escape, ...)
+ list(x = x, caption = caption, escape = escape, isn = isn,...)
)
structure(res, format = format, class = 'knitr_kable')
}
@@ -279,7 +280,7 @@ kable_latex = function(
midrule = getOption('knitr.table.midrule', if (booktabs) '\\midrule{}' else '\\hline'),
linesep = if (booktabs) c('', '', '', '', '\\addlinespace') else '\\hline',
caption = NULL, caption.short = '', table.envir = if (!is.null(caption)) 'table',
- escape = TRUE
+ escape = TRUE, isn = logical(ncol(x))
) {
if (!is.null(align <- attr(x, 'align'))) {
align = paste(align, collapse = vline)
@@ -306,6 +307,7 @@ kable_latex = function(
linesep = ifelse(linesep == "", linesep, paste0('\n', linesep))
if (escape) x = escape_latex(x)
+ x[, isn] = paste0('\\(', gsub(',', '{,}', x[, isn], fixed=TRUE), '\\)')
if (!is.character(toprule)) toprule = NULL
if (!is.character(bottomrule)) bottomrule = NULL
tabular = if (longtable) 'longtable' else 'tabular'