Skip to content

Commit b91b9a5

Browse files
committed
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 #1709.
1 parent b2ce792 commit b91b9a5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

R/table.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ kable = function(
144144
x = cbind(' ' = rownames(x), x)
145145
if (!is.null(col.names)) col.names = c(' ', col.names)
146146
if (!is.null(align)) align = c('l', align) # left align row names
147+
isn = c(FALSE, isn)
147148
}
148149
n = nrow(x)
149150
x = replace_na(to_character(x), is.na(x))
@@ -155,7 +156,7 @@ kable = function(
155156
attr(x, 'align') = align
156157
res = do.call(
157158
paste('kable', format, sep = '_'),
158-
list(x = x, caption = caption, escape = escape, ...)
159+
list(x = x, caption = caption, escape = escape, isn = isn,...)
159160
)
160161
structure(res, format = format, class = 'knitr_kable')
161162
}
@@ -279,7 +280,7 @@ kable_latex = function(
279280
midrule = getOption('knitr.table.midrule', if (booktabs) '\\midrule{}' else '\\hline'),
280281
linesep = if (booktabs) c('', '', '', '', '\\addlinespace') else '\\hline',
281282
caption = NULL, caption.short = '', table.envir = if (!is.null(caption)) 'table',
282-
escape = TRUE
283+
escape = TRUE, isn = logical(ncol(x))
283284
) {
284285
if (!is.null(align <- attr(x, 'align'))) {
285286
align = paste(align, collapse = vline)
@@ -306,6 +307,7 @@ kable_latex = function(
306307
linesep = ifelse(linesep == "", linesep, paste0('\n', linesep))
307308

308309
if (escape) x = escape_latex(x)
310+
x[, isn] = paste0('\\(', gsub(',', '{,}', x[, isn], fixed=TRUE), '\\)')
309311
if (!is.character(toprule)) toprule = NULL
310312
if (!is.character(bottomrule)) bottomrule = NULL
311313
tabular = if (longtable) 'longtable' else 'tabular'

0 commit comments

Comments
 (0)