Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kable typesets negative numbers incorrectly in tables #1709

Selbosh opened this issue May 3, 2019 · 2 comments


None yet
2 participants
Copy link

commented May 3, 2019

The current version of kable generates incorrectly typeset numeric columns, especially in LaTeX output, wherever they contain negative numbers.


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

|x   |     y|
|Foo |  1.23|
|Bar | -6.54|
|Baz |  1.00|

which renders as follows here on GitHub:

x y
Foo 1.23
Bar -6.54
Baz 1.00

Negative numbers and minus signs

Looks OK, right? Well, not quite. The symbol before the 6 is a hyphen-minus (-, U+002D), not an explicit minus sign (−, U+2212).

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
$-123$ or \(-123\), as a hyphen-minus entered in math mode is interpreted and correctly rendered as a minus symbol, with the correct kerning.

Compare these two columns, where column y is rendered by the default kable settings from a numeric column, and column z has been wrapped in math tags.

|x   |     y|       z|
|Foo |  1.23|  $1.23$|
|Bar | -6.54| $-6.54$|
|Baz |  1.00|  $1.00$|

Numeric columns in text mode and in math mode

Possible fix

One way to accomplish this might be to replace the line


Line 145 in 00ffce2

if (is_numeric(x[, j])) x[, j] = round(x[, j], digits[j])


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?

Another solution

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!


This comment was marked as off-topic.

Copy link

commented May 16, 2019

run following code, your get some surprise:

cl <- data.frame(x=c('-','±'),y=c(20,50),z=c('normal','abnormal'))
knitr::kable(cl,booktabs = TRUE)

This comment has been minimized.

Copy link

commented May 16, 2019

@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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.