这是 question 的延续我之前发过。我在 RStudio 中有一个 code.Rnw
文件,我使用 knit("code.Rnw")
命令编织成一个 code.tex
文件。
我有一个正在使用 xtable 命令打印的数据框。在下面的示例中,它是 20 行。但是,为了节省空间,我将其打印为两列,每列 10 行。
下面是我的代码:
\documentclass[11pt, a4paper]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{longtable}
\begin{document}
<<echo=FALSE,results='asis'>>=
library(xtable)
set.seed(1)
spaceCol = rep(" ",10)
df1 = data.frame(student = letters[1:10], vals=runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))
dfFull = data.frame(df1,spaceCol,df2)
names(dfFull) = c(" ","% Correct"," "," ", "% Correct")
row.names(dfFull) = NULL
x.big <- xtable(dfFull, label ='tabtwo',caption ='Caption for table with student scores')
print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
@
\end{document}
输出结果如下:
我喜欢这种输出的美感,尤其是因为在 longtable 格式中,这种输出会在需要时自动分页。但是,我正在努力改进的是,让这个输出实际上是两个不同的列变得更容易可视化。
为此,我想在两列之间添加一个空格,这样输出看起来更像下面这样:
但是,如果这被证明是不可能的,那么我可以考虑添加一条垂直线来区分两列,如下所示:
鉴于我在使用 xtable 方面的限制,这怎么可能?
最佳答案
\documentclass[11pt, a4paper]{article}
\usepackage{subfig}
\begin{document}
<<echo = FALSE>>=
library(xtable)
opts_chunk$set(
echo = FALSE,
results = 'asis'
)
set.seed(1)
mynames <- c("", "% Correct")
df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))
colnames(df1) <- mynames
colnames(df2) <- mynames
@
\begin{table}\centering
\subfloat{
<<>>=
print(xtable(df1), floating = FALSE, include.rownames = FALSE)
@
} \hspace{2cm}
\subfloat{
<<>>=
print(xtable(df2), floating = FALSE, include.rownames = FALSE)
@
}
\caption{Caption for table with student scores} \label{tabtwo}
\end{table}
\end{document}
唯一的缺点是,您不能通过这种方法使用 longtable
。
更新:这是一个使用longtable
的替代方法。诀窍是使用 xtable
仅用于表格的 contents
并手动构建标题,因此您可以完全控制所有行等。我决定使用一个空的列的空间,因为使第 2 列变宽会使水平线看起来很难看。
\documentclass{article}
\usepackage{longtable}
\begin{document}
\thispagestyle{empty}
<<echo = FALSE>>=
library(xtable)
opts_chunk$set(
echo = FALSE,
results = 'asis'
)
set.seed(1)
df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))
dfFull <- cbind(df1, NA, df2)
@
\begin{longtable}{lrl@{\hskip 2cm}lr} \cline{1-2} \cline{4-5}
& \% Correct & & & \% Correct \\ \cline{1-2} \cline{4-5}
<<>>=
print(xtable(dfFull), only.contents = TRUE, include.rownames = FALSE, include.colnames = FALSE, hline.after = NULL)
@
\cline{1-2} \cline{4-5}
\caption{Caption for table with studen scores} \label{tabtwo}
\end{longtable}
\end{document}
UPDATE2:最后,一个使用 longtable
且不涉及“手动”创建表的一半的解决方案。诀窍是删除所有水平线 (hline.after = NULL
) 并在需要时使用 add.to.row
添加 \clines
(灵感来自 this question )。
\documentclass{article}
\usepackage{longtable}
\begin{document}
\thispagestyle{empty}
<<echo = FALSE, results = 'asis'>>=
library(xtable)
set.seed(1)
df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(letters[11:20], runif(10, 1, 10))
dfFull <- cbind(df1, NA, df2)
# To test "longtable", rbind data several times:
multiply <- 5
dfFull <- do.call("rbind", replicate(multiply, dfFull, simplify = FALSE))
colnames(dfFull) <- c("", "% Correct", "", "", "% Correct")
print(xtable(dfFull,
caption = "Caption for table with student scores",
label = "tabtwo",
align = c("l", # ignored (would apply to colnames)
"l", "r",
"l@{\\hskip 2cm}", # space between blocks
"l", "r")),
include.rownames = FALSE,
include.colnames = TRUE,
hline.after = NULL, # Remove all default lines. A line after the very last row remains, which is automatically added when using "longtable".
tabular.environment = "longtable",
floating = FALSE,
add.to.row = list(
pos = list(-1, 0),
command = rep("\\cline{1-2} \\cline{4-5}", 2))
)
@
\end{document}
关于r - 使用 longtable 自定义的 Xtable 两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247757/