Marcos Vinicius - 9 months ago 73
LaTeX Question

# Changing the decimal mark makes stargazer to put an extra space between numbers

I'm using the library

stargazer
to generate tables in
latex
(Rstudio/knitr/Sweave).

I need to change the decimal separator to comma (",") and the function
stargazer()
works most of the times. Only when the option
summary = false
is passed the results come with a extra space between numbers.

Anyone knows how to overcome this problem?

<<results='asis'>>=
library(stargazer)
set.seed(0)
x = matrix(rnorm(3),nrow = 1)
y = matrix(rnorm(9),nrow = 3)

stargazer(x,summary=FALSE)
stargazer(x,summary=FALSE,decimal.mark = ",")
stargazer(y,summary=TRUE,decimal.mark = ",")
@


Table 2 always shows a extra space between the comma and the first decimal place. Table 3 shows good results

Answer

When inspecting the generated TEX it becomes clear that stargazer does not add any spaces after the commas. The problem lies somewhere else:

The root of this problem is that LaTeX does not recognize , as a decimal separator by default. Therefore, when in math mode, LaTeX adds a space after each ,. This problem is described here on TEX.SE and the solution is to include the icomma package.

\documentclass{article}
\usepackage{icomma}
\begin{document}
<<results='asis', echo=FALSE, message = FALSE>>=
library(stargazer)
set.seed(0)
x = matrix(rnorm(3),nrow = 1)
y = matrix(rnorm(9),nrow = 3)

stargazer(x,summary=FALSE)
stargazer(x,summary=FALSE,decimal.mark = ",")
stargazer(y,summary=TRUE,decimal.mark = ",")
@
\end{document}


One question remains: Why was the problem not visible in Table 3 from the question? This is because stargazer uses inconsistent math markup in the tables. The second cell in Table 2 contains -$0,326$ whereas row 2, last column of Table 3 contains $-$0,006. In the first case, the number itself is in math mode, leading to the behavior described above. (And unfortunately, the - is in text mode, leading to bad typography.) In the second case, the number is in text mode, where no extra space is added.

To overcome the issue with the badly formatted minus signs, I recommend using xtable instead of stargazer for the simple (non-summary) tables. Combined with a custom column type that sets the column in math mode, the result is:

\documentclass{article}
\usepackage{icomma}
\usepackage{tabularx}
\newcolumntype{R}{>{$}r<{$}} % like an "r" column but in math mode
\begin{document}
<<results='asis', echo=FALSE>>=
library(xtable)
set.seed(0)
x = matrix(rnorm(3),nrow = 1)

print.xtable(
xtable(x, caption = "", align = rep("R", 4)),
include.rownames = FALSE,
include.colnames = FALSE,
format.args = list(decimal.mark = ","),
caption.placement = "top")
@
\end{document}