Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Kable typesets negative numbers incorrectly in tables #1709
The current version of
Consider this minimal example.
df <- data.frame(x = c('Foo', 'Bar', 'Baz') y = c(1.2345, -6.5432, 1.0001)) kable(df, digits = 2)
The output is
which renders as follows here on GitHub:
Negative numbers and minus signs
Looks OK, right? Well, not quite. The symbol before the
In HTML output the difference is not so noticeable to some, but in LaTeX-PDF output it looks extremely dodgy as far as kerning is concerned, because the numbers are incorrectly typeset in text mode and therefore the '-' symbol is misinterpreted as a hyphen rather than a minus sign.
The correct output, at least for LaTeX, should be to wrap all numbers in in-line math tags, like
Compare these two columns, where column
|x | y| z| |:---|-----:|-------:| |Foo | 1.23| $1.23$| |Bar | -6.54| $-6.54$| |Baz | 1.00| $1.00$|
One way to accomplish this might be to replace the line
if (is_numeric(x[, j])) x[, j] = sprintf(sprintf('$%%.%sf$', digits[j]), x[, j])`
however this may only appropriate for LaTeX output, as though it should look fine on HTML, it would be an unwelcome surprise for people not using MathJax to suddenly need it. Maybe we can make it an option, disabled by default?
Slightly less heavy-handed: replace all hyphens in numeric columns with double hyphens, which should get typeset as en-dashes. Not strictly correct, and could cause kerning issues in LaTeX, but looks a bit better.
if (is_numeric(x[, j])) x[, j] = gsub('-', '--', round(x[, j], digits[j]))`
Of course one can pre-process the data before it goes into kable, but then the columns aren't numeric any more so you have to set the alignment manually. And I personally think good typography should be the default!
@wikithink I don't understand what you mean. The first symbol (a hyphen-minus) is rendered as a hyphen in text mode, and there is no second meaning for a plus-minus symbol.
cl2 <- data.frame(x = c('$-$', '$\\pm$'), y = c(20, 50), z = c('minus', 'plus-minus')) knitr::kable(cl2, booktabs = TRUE)