3

这是我之前发布的问题的延续。我在 RStudio 中有一个文件,我使用命令code.Rnw将它编织成一个文件。code.texknit("code.Rnw")

我有一个使用 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 方面的限制,这怎么可能?

4

1 回答 1

3
\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),然后\clines在需要的地方添加使用(受此问题add.to.row的启发)。

\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}
于 2015-07-06T15:26:56.903 回答