1
votes

I am new to LaTeX, Knitr and xtable. Specifically, when constructing a table and then printing it with the xtable package, how can I:

  1. Bold an entire column, including the header. In my case, the last column of the table is a sum column and that's what I need to be bold. The most similar question I could find on here is: adding rows or boldify single row.names with print.xtables – add something in between rows?
  2. In Microsoft Word/Excel, it is possible to replace a negative value in the format of "-1" with "(1)". Is this possible in R?

Thank you for all your time and consideration. I have read into the Hmisc package, but don't really understand if it can help me with these problems.

Edit:

The R code chunk that I am working with looks like this.

<<echo=FALSE,results='asis'>>

thirdTable <- table.CalendarReturns(port_returns, digits=2)
tli.table <- xtable(thirdTable, align="rccccccccccccc")
print(tli.table, floating = FALSE, size="\\tiny", scalebox=1.57)

@

To clarify my problem, I would like to be able to format within that R chunk, so that my LaTeX output includes a bolded last column (including header) and negative values replaced with "()" instead of "-".

2
Please edit your question to spell the package name correctly.IRTFM
I've been searching for the answer to the second part and think I have made some progress, but am not getting the sense that you are still paying attention.IRTFM
Apologies, I posted my question as the last thing I did out the office door. Thank you for your helpful response. What do package name do you want me to edit?geoQuant

2 Answers

0
votes

Using the output of the first example in the ?xtable help page, add a format to the column specification. The{\bfseries} needs to have a leading >:

\documentclass{article}

\usepackage{booktabs,dcolumn}

\begin{document}

% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Tue Oct 29 18:22:49 2013

% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Tue Oct 29 18:22:49 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrl>{\bfseries}llr}
% Edit above line
  \hline
 & grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
1 & 6 & M & YES & HISPANIC & 43 \\ 
  2 & 7 & M & NO & BLACK & 88 \\ 
  3 & 5 & F & YES & HISPANIC & 34 \\ 
  4 & 3 & M & YES & HISPANIC & 65 \\ 
  5 & 8 & M & YES & WHITE & 75 \\ 
  6 & 5 & M & NO & BLACK & 74 \\ 
  7 & 8 & F & YES & HISPANIC & 72 \\ 
  8 & 4 & M & YES & BLACK & 79 \\ 
  9 & 6 & M & NO & WHITE & 88 \\ 
  10 & 7 & M & YES & HISPANIC & 87 \\ 
  11 & 3 & M & NO & WHITE & 79 \\ 
  12 & 6 & F & NO & WHITE & 84 \\ 
  13 & 8 & M & NO & WHITE & 90 \\ 
  14 & 5 & M & NO & WHITE & 73 \\ 
  15 & 8 & F & NO & WHITE & 72 \\ 
  16 & 6 & F & NO & BLACK & 82 \\ 
  17 & 4 & M & NO & WHITE & 69 \\ 
  18 & 3 & F & YES & HISPANIC & 17 \\ 
  19 & 3 & M & NO & HISPANIC & 37 \\ 
  20 & 5 & M & NO & WHITE & 70 \\ 
   \hline
\end{tabular}
\end{table}

\end{document}

It's possible that you need to have a \usepackage{array}, but this code seems to be succeeding for me in Texshop.app.

0
votes

You can change each column entry to be labeled bold individually.

thirdTable[,ncol(thirdTable)] = paste0('\\textbf{',thirdTable[,ncol(thirdTable)],'}')