I have a report with many summary tables of regression output and converted them into tables using pander in the Rmarkdown document. Even though all tables are less than one page, they sometimes break and continue on two pages, which is hard to read.
I created the .tex file from .Rmd and converted longtabs to booktabs as a way but couldn't compile the document. Also, since there are so many tables, adding \pagebreak or \newpage is not an optimal solution either.
Here is a minimal example of the .Rmd file. The code here is to be pasted in a .Rmd file. How can I make the table be on the same page?
---
title: "Put tables on the same page"
author: "author"
date: May 16, 2020
output:
pdf_document:
extra_dependencies: ["lipsum"]
number_sections: yes
toc: yes
geometry: margin=1in
fontsize: 11pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
cache = TRUE,
#include = FALSE,
message = FALSE,
warning = FALSE,
error = TRUE,
fig.cap = " ",
number_sections = TRUE)
```
```{r}
library(tidyverse)
library(pander)
library(rmarkdown)
#panderOptions('keep.trailing.zeros', TRUE)
```
\lipsum[3-5]
```{r}
m5 = lm(log(Volume)~log(Girth)*log(Height)+Girth^2+Girth^3+Girth^4 + Height^2+Height^3+rnorm(31)+rpois(31, 1),data=trees)
pander(m5)
```
I don't know how to keep all the chunks intact in this code here. Sorry if that causes confusion. It is converting the markdown code to formatting.
% relevant lines for longtable package in the preamble of generated tex document and a sample table which breaks onto two pages but is short enough to be on one page.
\usepackage{longtable,booktabs}
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\begin{longtable}[]{@{}cccccl@{}}
\caption{Overall Model}\tabularnewline
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endfirsthead
\toprule
\begin{minipage}[b]{0.24\columnwidth}\centering
~\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Estimate\strut
\end{minipage} & \begin{minipage}[b]{0.14\columnwidth}\centering
Std. Error\strut
\end{minipage} & \begin{minipage}[b]{0.11\columnwidth}\centering
z value\strut
\end{minipage} & \begin{minipage}[b]{0.12\columnwidth}\centering
Pr(\textgreater\textbar z\textbar)\strut
\end{minipage} & \begin{minipage}[b]{0.09\columnwidth}\raggedright
\strut
\end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{(Intercept)}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-14.89\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.038\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-393.9\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var1}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.084\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.035\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-2.383\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.017\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
*\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var2}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.016\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
11.93\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var10}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.158\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.051\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
3.096\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.002\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var3}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.058\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.013\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
4.296\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var4}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
-0.169\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.053\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
-3.209\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.001\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var5}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.28\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.054\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
5.199\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var11}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.167\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.026\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.467\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\begin{minipage}[t]{0.24\columnwidth}\centering
\textbf{var6}\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0.245\strut
\end{minipage} & \begin{minipage}[t]{0.14\columnwidth}\centering
0.036\strut
\end{minipage} & \begin{minipage}[t]{0.11\columnwidth}\centering
6.792\strut
\end{minipage} & \begin{minipage}[t]{0.12\columnwidth}\centering
0\strut
\end{minipage} & \begin{minipage}[t]{0.09\columnwidth}\raggedright
* * *\strut
\end{minipage}\tabularnewline
\bottomrule
\end{longtable}
longtable
. The only reason for usinglongtable
is to allow a table to be split over multiple pages.