Sizing tables in PDF documents using knitr and kableExtra

I am producing a set of Latex tables with knitr and kableExtra. I cannot control the width of the tables. Would appreciate any suggestions.

Here is my code:

 tabLATEX <- kable(m.race, col.names = names_spaced,
                      caption="Race Trend", row.names=FALSE, align=c("l",rep("r",3)),
                      format="latex", booktabs=TRUE)  %>%
      kable_styling(latex_options="scale_down") %>%
      column_spec(1,width = "3in") %>%
      column_spec(2,width = "0.4in") %>%
      column_spec(3,width = "0.4in") %>%
      column_spec(4,width = "0.4in") %>%
      add_indent(c(3:9)) %>%
      add_header_above(header=tblHead) %>%
      footnote(c("Source; 2000 Census",
                 "Source: 2010 Census",
                 captionSrc("ACS",ACS)))

and here is my output:

image

The problem I am having is with all of the white space to the right of the last column. I have tried adding "full_width= FALSE" to the kable_styling call. Nothing seems to work.

Any suggestions?

Thanks
AB

1 Like

The table width is being determined by the last footnote, which is wider than the table itself, so latex adds more space to the right when creating the table. Adding \n within the footnote text to create a linebreak doesn't work in the latex conversion. Also, kableExtra has a linebreak function, which is intended for generating linebreaks in latex tables, but I wasn't able to get that to work within the footnote function either.

After looking at the latex vignette for kableExtra, I see there's an option for putting footnotes in a ThreePartTable frame. Below is a complete rmarkdown example document. Note that I've commented out latex_options="scale_down", which will actually "scale up" the size of your table if its default width is less than the full width of the margins.

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r}
library(knitr)
library(kableExtra)
```

```{r}
kable(mtcars[1:9, 1:4], #col.names = names_spaced,
      caption="Without ThreePartTable", row.names=FALSE, align=c("l",rep("r",3)),
      format="latex", booktabs=TRUE) %>%
  #kable_styling(latex_options="scale_down") %>%
  column_spec(1,width = "1in") %>%
  column_spec(2,width = "0.4in") %>%
  column_spec(3,width = "0.4in") %>%
  column_spec(4,width = "0.4in") %>%
  add_indent(c(3:9)) %>%
  add_header_above(header="Header") %>%
  footnote(c("Source; 2000 Census",
             "Source: 2010 Census",
             "Really long footnote that increases the width of the table."))
```

```{r}
kable(mtcars[1:9, 1:4], #col.names = names_spaced,
      caption="With ThreePartTable", row.names=FALSE, align=c("l",rep("r",3)),
      format="latex", booktabs=TRUE) %>%
  #kable_styling(latex_options="scale_down") %>%
  column_spec(1,width = "1in") %>%
  column_spec(2,width = "0.4in") %>%
  column_spec(3,width = "0.4in") %>%
  column_spec(4,width = "0.4in") %>%
  add_indent(c(3:9)) %>%
  add_header_above(header="Header") %>%
  footnote(c("Source; 2000 Census",
             "Source: 2010 Census",
             "Really long footnote that increases the width of the table."),
           threeparttable=T)
```

And the output document:

04%20PM

19%20PM

5 Likes

Thanks for the tips joels. They worked well. I'm not convinced about the utility of letting a footnote control the width of a table, but so it goes. And scale_down increases the width of the table? Sheesh...
AB

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.