Search code examples
rlatexmarkdownxtable

Rmarkdown table pushed to two pages when it can fit in one


I'm trying to create separate Rmd files for different tables where each table gets rendered to separate pdfs. Because of some complex formatting issues, I'm using xtable to try and pull this off. I have some tables that I anticipate will fill up a full 8.5x11.0 page with 1 inch margins. But when I render them the first page of the pdf is blank and the second page has the entire, properly formatted table. I'm looking for help to get this to work right. Here's my minimal workable example...

---
output: pdf_document
tables: true
geometry: margin=1.0in
---

```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
require(xtable)

# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)

strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"

# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
      size = "normalsize",
      include.rownames = FALSE,
      include.colnames = TRUE,
      caption.placement = "top",
      comment=FALSE
      )
```

This gets saved as test.Rd and I render using...

library(rmarkdown)
render("test.Rmd"

If I change the margin sizes, it seems to only affect the left and right margin. If I shrink the size of the font, it will fit on one page, but I'd like to keep the font size as is and get rid of the blank first page. Ideas? I'm a bit of a latex newbie so apologies for missing something obvious.


Solution

  • Thanks to the posting from ted-dallas, I was able to figure out that the table.placement option did what I want without having to turn off the floating...

    ---
    output: pdf_document
    tables: true
    geometry: margin=1.0in
    ---
    
    ```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
    require(xtable)
    
    # set up data frame
    df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
    
    strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
    
    # set up xtable output
    print(xtable(df, caption = strCaption, label = ""),
          size = "normalsize",
          include.rownames = FALSE,
          include.colnames = TRUE,
          caption.placement = "top",
          comment=FALSE,
          table.placement = "!ht"
          )
    ```
    

    This generates the output I was looking for.