问题描述
是否有可能在xtable中具有与表其余部分中使用的标头对齐不同的标头对齐?就我而言,我希望标题的中心对齐,但表本身应右对齐.
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.
推荐答案
要在LaTeX中做到这一点,您可以将标题粘贴到\multicolumn
内容中以指定所需的对齐方式:
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}
print.xtable
函数使用xtable
对象的名称作为标题.因此,如果您重命名xtable
对象:
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}")
这就是大多数工作,您只需打印它即可覆盖print.xtable
的消毒功能:
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}
否则会
& $\backslash$multicolumn\{1\}\{c\}\{x\} & $\backslash$multicolumn\{1\}\{c\}\{y\} \\
那怎么样?
这篇关于xtable和标头对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!