Search code examples
rr-markdownvtable

Why is my RMarkdown summary table only showing on one PDF page, where is the rest?


I'm trying to create a summary table with the package vtable. I do get a table but somehow the font size is super large and not the whole table is shown? I'm really at a loss here. I have spent hours on it, but could not find a solution... Can please someone tell me how to get a smaller table (font size and the whole table in a pdf document?

---
title: "Example"
date: "22 March 2021"
output: 
  pdf_document
---

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(vegan)
library(vtable)




{r dune}
data(dune.env)



{r plot1, echo = F,results='asis',out.width='10%',message=F, fig.show='hold'}
st(dune.env,summ=c('mean(x)','median(x)','min(x)','max(x)'),group="Management",group.long = TRUE,out="latex")

Solution

  • By default, sumtable with out = 'latex' applies a LaTeX \resizebox{\textwidth} to the table to make it fit horizontally on the page. When the table is very narrow and tall, though, like this one, the resizebox can scale it to be bigger (making the text big).

    You can control the width of the table with the fit.page option, which scales the horizontal width of the table. By default it's '\\textwidth', but you could do, for example, .6\\textwidth to only fill 60% of the horizontal space. This is what I get with fit.page='.5\\textwidth':

    Table filling 50% of horizontal space

    Now, this makes the text tiny, but it does fit. This is a consequence of LaTeX giving tall tables a hard time, and having no built-in support for multi-page tables. So it just fills up a page and the rest isn't shown. This is a LaTeX thing, not sumtable. There isn't currently support for multi-page LaTeX tables in sumtable/vtable. However, you could add keep_tex: true to your YAML, then after the fact go in and edit the TeX file to use the longtable LaTeX package, which adds support for multi-page tables, and turn the \begin{table} into \begin{longtable}, etc..