Robin Donatello
Robin Donatello

Reputation: 491

merge columns every other row using Sweave/R/Latex

I am writing a conference abstract booklet using R/Sweave. I have already made the program booklet for printing that contains the id, author, title only.

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?

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. I could switch to markdown/html - and make it look closer to real conference program schedules that have full abstracts on them. 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. Note that it uses the R package lipsum which has to be installed via 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}

Upvotes: 0

Views: 202

Answers (2)

user3603486
user3603486

Reputation:

One approach using my package huxtable. I couldn't install lipsum for some reason, so just hacked it. This is in a .Rmd file with output 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 output

Upvotes: 0

Robin Donatello
Robin Donatello

Reputation: 491

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, "} \\\\"))

Upvotes: 1

Related Questions