简体   繁体   English

使用Sweave / R / Latex将每隔一行合并列

[英]merge columns every other row using Sweave/R/Latex

I am writing a conference abstract booklet using R/Sweave. 我正在使用R / Sweave编写会议摘要手册。 I have already made the program booklet for printing that contains the id, author, title only. 我已经制作了仅包含id,作者,标题的打印程序手册。

Now I want to modify it to include the abstract (not for print). 现在,我想对其进行修改以包括摘要(不适用于印刷)。 But abstracts are lengthy . 但是摘要是冗长的 My thought is to take the cell with the abstract info, and have it display below the row with the author info - expanded across the full width of the page. 我的想法是使用包含抽象信息的单元格,并将其显示在具有作者信息的行下方-扩展到页面的整个宽度。

ID--author--------title-------------------------------- 身份证明-作者--------标题--------------------------------
abstract----------------------------------------------- 抽象 - - - - - - - - - - - - - - - - - - - - - - - -

So every other row has only one column spanning the width of the entire table. 因此,每隔一行只有一列跨越整个表格的宽度。 Is there a way to add multicolmn{x} to every other row? 有没有一种方法可以将multicolmn{x}添加到其他每一行?

If a solution can't be figured out, advice for how to print full abstracts in a nice way would be welcome. 如果无法找到解决方案,则欢迎提供有关如何以一种很好的方式打印完整摘要的建议。 (Something other than "just use landscape" or "adjust column widths") (除了“仅使用横向”或“调整列宽”以外的其他内容)

Also, it doesn't have to be PDF. 另外,它不必是PDF。 I could switch to markdown/html - and make it look closer to real conference program schedules that have full abstracts on them. 我可以切换到markdown / html-使其更接近具有完整摘要的真实会议计划。 Again, one I figure out how to print a table where every other row has only one column that is the width of the entire table. 再次,我弄清楚了如何打印一张表格,其中每隔一行只有一列,即整个表格的宽度。

If you want to try - Here is a complete MWE for what I have working now. 如果您想尝试-这是我现在正在工作的完整MWE。 Note that it uses the R package lipsum which has to be installed via devtools/github. 请注意,它使用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}

Split data from abstract manually 手动从摘要中拆分数据

out <- dat[,-4]
ab.list <- dat$abstract

then add.to.row 然后添加到行

  , add.to.row = list(pos = as.list(1:nrow(out)),
                      command = paste0("\\multicolumn{3}{L{15cm}}{\\textbf{Abstract: }", ab.list, "} \\\\"))

One approach using my package huxtable . 一种使用我的包huxtable I couldn't install lipsum for some reason, so just hacked it. 由于某种原因,我无法安装Lipum,因此对其进行了破解。 This is in a .Rmd file with output pdf_document . 该文件位于.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
```

PDF输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM