Skip to content

Commit

Permalink
For LaTeX output, kable now typesets numbers in math mode for better-…
Browse files Browse the repository at this point in the history
…looking minus signs. Commas, if present (as decimal or thousands separator), are wrapped in braces to prevent additional space being added after. Improves #1709.
  • Loading branch information
krivit committed Feb 4, 2021
1 parent 074e0ba commit 2091587
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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')
}
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down

0 comments on commit 2091587

Please sign in to comment.