Is it possible to have a header alignment in xtable which is different from the alignment used in the rest of the table? In my case, I want my header to be center aligned, but the table itself should be right aligned.
To left-align the content, force the alignment of <th> elements to be left-aligned, with the text-align: left property: The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>. By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).
Additional arguments. (Currently ignored.) For most xtable methods, an object of class "xtable" which inherits the data.frame class and contains several additional attributes specifying the table formatting options. This function extracts tabular information from x and returns an object of class "xtable".
Vertical Alignment The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>. By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements). The following example sets the vertical text alignment to bottom for <td> elements:
xtable (x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) An R object of class found among methods (xtable). See below on how to write additional method functions for xtable. Character vector of length 1 or 2 containing the table's caption or title.
To do that in LaTeX you stick your headers into a \multicolumn
thing to specify the alignment you want:
\begin{tabular}{rrr}
\hline
& \multicolumn{1}{c}{x} &\multicolumn{1}{c}{y} \\
\hline
1 & 1 & 0.17 \\
2 & 2 & 0.63 \\
3 & 3 & 0.95 \\
4 & 4 & 0.57 \\
5 & 5 & 0.65 \\
\hline
\end{tabular}
The print.xtable
function uses the names of the xtable
object as the headers. So if you rename your xtable
object:
> d=data.frame(x=1:5,y=runif(5)) # sample data frame
> dx=xtable(d) # make an xtable
> names(dx)=c("\\multicolumn{1}{c}{x}","\\multicolumn{1}{c}{y}")
then that's most of the work done, you just have to print it overriding the sanitization function of print.xtable
:
> print.xtable(dx,sanitize.colnames.function=function(x){x})
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Thu Feb 21 15:28:11 2013
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& \multicolumn{1}{c}{x} & \multicolumn{1}{c}{y} \\
\hline
1 & 1 & 0.78 \\
2 & 2 & 0.34 \\
3 & 3 & 0.88 \\
4 & 4 & 0.45 \\
5 & 5 & 0.54 \\
\hline
\end{tabular}
\end{center}
\end{table}
otherwise it does
& $\backslash$multicolumn\{1\}\{c\}\{x\} & $\backslash$multicolumn\{1\}\{c\}\{y\} \\
How's that?
Just to follow up on the answer by Spacedman (could not add a comment as I am reputationless ;)
Instead of doing sanitize.colnames.function=function(x){x}
you can do:
sanitize.colnames.function=function(x){paste0("\\multicolumn{1}{c}{",x,"}")}
This way you can skip renaming step. If you do/want to do other header "beautifications," they should be done before paste0
or between the commas (if short)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With