Skip to content

Commit c37fbd6

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 12b50f5 commit c37fbd6

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
@@ -157,6 +157,7 @@ kable = function(
157157
x = cbind(' ' = rownames(x), x)
158158
if (!is.null(col.names)) col.names = c(' ', col.names)
159159
if (!is.null(align)) align = c('l', align) # left align row names
160+
isn = c(FALSE, isn)
160161
}
161162
n = nrow(x)
162163
x = replace_na(to_character(as.matrix(x)), is.na(x))
@@ -168,7 +169,7 @@ kable = function(
168169
attr(x, 'align') = align
169170
res = do.call(
170171
paste('kable', format, sep = '_'),
171-
list(x = x, caption = caption, escape = escape, ...)
172+
list(x = x, caption = caption, escape = escape, isn = isn,...)
172173
)
173174
structure(res, format = format, class = 'knitr_kable')
174175
}
@@ -227,7 +228,7 @@ kable_latex = function(
227228
midrule = getOption('knitr.table.midrule', if (booktabs) '\\midrule' else '\\hline'),
228229
linesep = if (booktabs) c('', '', '', '', '\\addlinespace') else '\\hline',
229230
caption = NULL, caption.short = '', table.envir = if (!is.null(caption)) 'table',
230-
escape = TRUE
231+
escape = TRUE, isn = logical(ncol(x))
231232
) {
232233
if (!is.null(align <- attr(x, 'align'))) {
233234
align = paste(align, collapse = vline)
@@ -254,6 +255,7 @@ kable_latex = function(
254255
linesep = ifelse(linesep == "", linesep, paste0('\n', linesep))
255256

256257
if (escape) x = escape_latex(x)
258+
x[, isn] = paste0('\\(', gsub(',', '{,}', x[, isn], fixed=TRUE), '\\)')
257259
if (!is.character(toprule)) toprule = NULL
258260
if (!is.character(bottomrule)) bottomrule = NULL
259261
tabular = if (longtable) 'longtable' else 'tabular'

0 commit comments

Comments
 (0)