【问题标题】:merge columns every other row using Sweave/R/Latex使用 Sweave/R/Latex 每隔一行合并列
【发布时间】:2017-05-01 05:11:37
【问题描述】:

我正在使用 R/Sweave 编写会议摘要小册子。我已经制作了仅包含 id、作者、标题的打印程序手册。

现在我想修改它以包含摘要(不用于打印)。但摘要冗长。我的想法是获取带有抽象信息的单元格,并将其显示在带有作者信息的行下方 - 在页面的整个宽度上展开。

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}

【问题讨论】:

标签: r latex sweave xtable


【解决方案1】:

手动从摘要中拆分数据

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

然后添加.to.row

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

【讨论】:

    【解决方案2】:

    使用我的包huxtable 的一种方法。由于某种原因,我无法安装lipsum,所以就破解了它。这是在带有输出pdf_document 的.Rmd 文件中。

    ```{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
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多