4

I found Boris's suggestion to use siunitx to achieve number formatting in Hmisc::latex tables to almost be exactly what I was looking for: R, Sweave, Hmisc: pretty-print numbers

However, if you look closely at the table, the number columns line up poorly with the column labels. This is worse when using multicolumn labels (e.g., cgroup). In looking at the documentation (page 15, table 9) for siunitx, I think this is because it is centering the numbers at the decimal point.

Is it possible to use the number formatting functions of siunitx but justify the columns as \raggedleft (right aligned) within a call to Hmisc::latex?

1
3

You can check out the option table-format on page 51 of the documentation.

I can illustrate with this example based on the answer you linked to.

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{siunitx,booktabs}
\sisetup{group-separator={.},group-minimum-digits={3},
  output-decimal-marker={,}}
\usepackage[noae]{Sweave}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<>>=
library("Hmisc")
dat <-  matrix(c(1000, 100, 10000, 10000,3.145,1700.42), 2)
@
<<echo=false,results=tex>>=
latex(dat,table=F,center='centering',file='',
  booktabs=T,numeric.dollar=F,colheads=c("Col A","Col B","Col C"),
      colnamesTexCmd="bfseries", col.just=c("S[table-format = 4]",
                                            "S[table-format = 5]",
                                            "S[table-format = 4.3]"))
@

\end{document}

1
  • Perfect! Thank you for the reference and the example. – penguinv22 Jul 31 '12 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.