4

what is the easiest way to bold all column headers using the output of calling xtable, ideally under the print.xtable command. I am using knitr.

1 Answers1

7

Example

\documentclass[a5paper]{article}
\usepackage{booktabs,colortbl,xcolor}
\begin{document}

A simple raw \texttt{xtable}: 

<<before,results='asis',echo=F>>=
library(xtable)
df <- data.frame(
  One=c(101.000,22.345),
  Two=c(3.45,74.34),
  Three=c(65,6.1234))
print(xtable(df))
@

The same table with some formatting:

<<after,results='asis', echo=F>>=
bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')}
gray <- function(x) {paste('{\\textcolor{gray}{',x,'}}', sep ='')}
print(xtable(df,digits=0), 
      sanitize.rownames.function=gray, 
      sanitize.colnames.function=bold, 
      booktabs=T)
@
\end{document}
Fran
  • 80,769
  • 1
    I answered because it was easy for me make the example from the scratch in this case, but please always a MWE in your questions. – Fran Apr 24 '15 at 04:51
  • Thanks. That is what I wanted. That really should work too, but I am getting bad box errors when I try using the sanitize command. Not sure why...scratching my head :( – user3696708 Apr 25 '15 at 16:55