【问题标题】:xtable and header alignmentxtable 和标题对齐
【发布时间】:2013-02-20 21:19:05
【问题描述】:

是否可以在 xtable 中使用与表格其余部分中使用的对齐方式不同的标题对齐方式?就我而言,我希望我的标题居中对齐,但表格本身应该右对齐。

【问题讨论】:

  • 未提供示例。也许你应该看看表格包。

标签: r latex sweave xtable


【解决方案1】:

要在 LaTeX 中做到这一点,您需要将标题粘贴到 \multicolumn 中以指定您想要的对齐方式:

\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 对象:

> 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的清理功能:

> 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\} \\ 

怎么样?

【讨论】:

  • 很好的答案。 function(x){x} 可以替换为identity,这样可以节省一些击键次数。
【解决方案2】:

只是跟进 Spacedman 的回答(无法添加评论,因为我没有名气;)

您可以这样做,而不是 sanitize.colnames.function=function(x){x}

sanitize.colnames.function=function(x){paste0("\\multicolumn{1}{c}{",x,"}")}

这样您可以跳过重命名步骤。如果你想要/想要做其他标题“美化”,它们应该在paste0 之前或逗号之间(如果短)完成

【讨论】:

    猜你喜欢
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多