[英]merge columns every other row using Sweave/R/Latex
我正在使用R / Sweave编写会议摘要手册。 我已经制作了仅包含id,作者,标题的打印程序手册。
现在,我想对其进行修改以包括摘要(不适用于印刷)。 但是摘要是冗长的 。 我的想法是使用包含抽象信息的单元格,并将其显示在具有作者信息的行下方-扩展到页面的整个宽度。
身份证明-作者--------标题--------------------------------
抽象 - - - - - - - - - - - - - - - - - - - - - - - -
因此,每隔一行只有一列跨越整个表格的宽度。 有没有一种方法可以将multicolmn{x}
添加到其他每一行?
如果无法找到解决方案,则欢迎提供有关如何以一种很好的方式打印完整摘要的建议。 (除了“仅使用横向”或“调整列宽”以外的其他内容)
另外,它不必是PDF。 我可以切换到markdown / html-使其更接近具有完整摘要的真实会议计划。 再次,我弄清楚了如何打印一张表格,其中每隔一行只有一列,即整个表格的宽度。
如果您想尝试-这是我现在正在工作的完整MWE。 请注意,它使用R包lipsum
,该包必须通过devtools / github安装。
\documentclass{article}
\usepackage{booktabs, multicol, array}
\usepackage[margin=0.75in]{geometry}
%%%%%%%%%%% Let tables to span entire page
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
<<echo=FALSE, warning=FALSE, message=FALSE>>=
# devtools::install_github("coolbutuseless/lipsum")
library(lipsum)
library(xtable)
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
options(xtable.comment = FALSE)
tblalign <- "lL{0.5cm}|L{4cm}L{6cm}L{8cm}"
# fake data setup
dat <- data.frame(ID = c(1:3), author = substr(lipsum[1:3], 1, 40),
title = substr(lipsum[4:6], 1, 100),
abstract = lipsum[7:9])
names(dat)=c("\\multicolumn{1}{c}{\\textbf{\\large{ID}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Author List}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Title}}}",
"\\multicolumn{1}{c}{\\textbf{\\large{Abstract}}}")
@
\begin{document}
<<results='asis'>>=
print(
xtable(x = dat
, align = tblalign)
, table.placement = "H"
, sanitize.colnames.function=function(x){x}
, include.rownames = FALSE
, include.colnames = TRUE
, size = "small"
, floating = FALSE
, hline.after = c(0,1:nrow(dat))
)
@
\end{document}
手动从摘要中拆分数据
out <- dat[,-4]
ab.list <- dat$abstract
然后添加到行
, add.to.row = list(pos = as.list(1:nrow(out)),
command = paste0("\\multicolumn{3}{L{15cm}}{\\textbf{Abstract: }", ab.list, "} \\\\"))
一种使用我的包huxtable
。 由于某种原因,我无法安装Lipum,因此对其进行了破解。 该文件位于.Rmd文件中,输出为pdf_document
。
```{r, results = 'asis'}
lipsum <- rep(do.call(paste, list(rep('blah ', 100), collapse = '')), 10)
dat <- data.frame(ID = c(1:3), author = substr(lipsum[1:3], 1, 40),
title = substr(lipsum[4:6], 1, 100),
abstract = lipsum[7:9], stringsAsFactors = FALSE)
library(huxtable)
# shape data
datmat <- matrix(NA_character_, nrow(dat) * 2, 3)
datmat[seq(1, nrow(datmat), 2), ] <- as.matrix(dat[, c('ID', 'author', 'title')])
datmat[seq(2, nrow(datmat), 2), 1] <- dat$abstract
# print as PDF
ht <- as_huxtable(datmat)
colspan(ht)[seq(2, nrow(ht), 2), 1] <- 3
wrap(ht) <- TRUE
col_width(ht) <- c(.2, .2, .6)
number_format(ht) <- 0
ht
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.