8

我有数据表(d1 和 d2),我想用它们自己的标题并排或在乳胶中彼此叠加打印。是否可以直接使用xtable()? 这两个表应该是不同的,即我们可以称它们为Table x(a)Table x(b),但它们应该是相邻的或堆叠的。

4

2 回答 2

16

我建议将结果保存为不同文件中的两个单独的表格(请参阅file=选项print.xtable()),然后input使用您认为适合您的布局的任何命令(tabularsubfloatminipage等)将它们保存到您的 LaTeX 文档中。这就是我通常所做的,尽管我通常依赖Hmisc包中的 LaTeX 工具。如果您只想将它​​们打印为独立的 PDF,请standalone为您的文档使用该类。

所以,这里有一个例子:

data(tli)
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
print(xtable(fm1), file="ta.tex", floating=FALSE)
print(xtable(head(tli, n=5)), file="tb.tex", floating=FALSE)

然后,一个快速的 tex 包装器(用 编译pdflatex):

\documentclass{article}
\usepackage{subfig}
\usepackage{graphicx}

\begin{document}

\begin{table}[ht]
\centering
\subfloat[Table x(a)]{\label{tab:tab1a}\scalebox{.5}{\input{./ta}}}\quad
\subfloat[Table x(b)]{\label{tab:tab1b}\scalebox{.5}{\input{./tb}}}
\caption{Caption about here}
\label{tab:tab1}
\end{table}

\end{document}

结果如下:

在此处输入图像描述

删除\scalebox默认(堆叠)布局的命令,除非它们足够窄以适应默认大小,如 @David 所述。

在此处输入图像描述

于 2011-06-07T11:53:56.660 回答
10

请参阅Alan Munn 在 tex.stackexchange.com 上对类似问题的回答

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}
\title{Side-by-side xtables}
\author{}
\date{}
\begin{document}
\maketitle
First some R code to create some data.
<<>>=
myData <- matrix(c(19,89,23,23,74,44,16,39,67),ncol=3,byrow=TRUE)
colnames(myData) <- c("A","B","C")
rownames(myData) <- c("1","2","3")
myData2 <- myData * 2
@

Now we place the data in two side-by-side tables:

\begin{table}[htb]
\begin{minipage}{.45\textwidth}
\centering
<<echo=FALSE,results=tex>>=
library("xtable")
print(xtable(myData),
  floating=FALSE,
  hline.after=NULL,
  add.to.row=list(pos=list(-1,0, nrow(myData)),
  command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\captionof{table}{The first table}
\end{minipage}
\begin{minipage}{.45\textwidth}
\centering
<<echo=FALSE,results=tex>>=
print(xtable(myData2),
  floating=FALSE,
  hline.after=NULL,
  add.to.row=list(pos=list(-1,0, nrow(myData2)),
  command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\captionof{table}{The second table}
\end{minipage}
\end{table}
\end{document}

代码输出

于 2011-11-28T05:02:52.643 回答